$(document).ready(function(){	$('.dropdown').each(function(i,item){		$(item).bind('mouseover', function(event){			$(this).addClass('hover');		});		$(item).bind('mouseout', function(event){			$(this).removeClass('hover');		});				var links = $('a', item);		if (links.length) {			links = links[0];			$(links).bind('focus', function(event){				$(item).addClass('hover');			});					}	});});

(function($){
	$.preloadImage = function(image, callback) {
		var self = this;
		this.file = image;
		this.callback = callback;
		this.loaded = false;
		this.aborted = false;
		this.error = false;
		this.image = new Image();
		
		this.cancelEvent = function() {
			if (this.image && this.image.onload) {
				this.image.onload = null;
			}
			if (this.image && this.image.onabort) {
				this.image.onabort = null;
			}
			if (this.image && this.image.onerror) {
				this.image.onerror = null;
			}
		};
		
		this.on_load = function() {
			self.loaded = true;
			if (self.callback && typeof self.callback == "function") {
				self.callback.apply(self, ["loaded"]);
			}
			self.cancelEvent();
		};
		
		this.on_error = function() {
			self.error = true;
			if (self.callback && typeof self.callback == "function") {
				self.callback.apply(self, ["error"]);
			}
			self.cancelEvent();
		};
		
		this.on_abort = function() {
			self.aborted = true;
			if (self.callback && typeof self.callback == "function") {
				self.callback.apply(self, ["aborted"]);
			}
			self.cancelEvent();
		};
		
		this.image.src = this.file;
		
		this.autoCheckIntervalId = 0;
		this.autoCheck = function() {
			if (self.loaded || self.aborted || self.error) {
				window.clearInterval(self.autoCheckIntervalId);
				return;
			}
			
			if (self.image && self.image.complete) {
				window.clearInterval(self.autoCheckIntervalId);
				self.loaded = true;
				if (self.callback && typeof self.callback == "function") {
					self.callback.apply(self, ["loaded"]);
				}
				self.cancelEvent();
				return;
			}
			
			if (self.image && (self.image.width || self.image.height)) {
				window.clearInterval(self.autoCheckIntervalId);
				self.loaded = true;
				if (self.callback && typeof self.callback == "function") {
					self.callback.apply(self, ["loaded"]);
				}
				self.cancelEvent();
				return;
			}
		};
		
		this.autoCheckIntervalId = window.setInterval( self.autoCheck, 500 );
	};
	
	$.preloadImages = function(images, callback) {
		if (typeof images == "string") {
			return new $.preloadImage(images, callback);
		}
		
		var self = this;
		this.images = images;
		this.callback = callback;
		this.all_complete = false;
		this.preloader = [];
		
		this.checkComplete = function() {
			if (self.preloader.length < self.images.length) return;
			if (this.all_complete) return;
			
			var all_complete = true;
			var j = 0;
			for (var i=0, n = self.preloader.length; i < n; i++) {
				if ( self.preloader[i] && self.preloader[i].loaded ) {
					j++;
				} else {
					all_complete = false;
				}
			}
			
			if (all_complete) {
				this.all_complete = true;
				if (self.callback && typeof self.callback == "function") {
					self.callback.apply(self);
				}
			}
		};
		
		for (var i=0, n = images.length; i < n; i++) {
			this.preloader[i] = new $.preloadImage(this.images[i], function(){
				self.checkComplete();
			});
		}
		
		this.autoCheckIntervalId = 0;
		this.autoCheck = function() {
			if (this.all_complete) {
				window.clearInterval(self.autoCheckIntervalId);
			}
			
			self.checkComplete();
		};
		
		this.autoCheckIntervalId = window.setInterval( self.autoCheck, 500 );
	};
	
	$.fn.carousel = function(options) {
		var isMethodCall = (typeof options == "string") || false;
		var args = arguments;
		
		$(this).each(function() {
			var carousel = $(this).data("carousel");
			if (carousel && isMethodCall) {
				if (carousel[options] && $.isFunction(carousel[options])) {
					carousel[options].apply(carousel, $.makeArray(args).slice(1));
				}
			} else if ( !isMethodCall && !carousel ) {
				carousel = new $.carousel(this, options);
				$(this).data("carousel", carousel);
			}
		});
		
		return this;
	};
	
	$.carousel = function(elem, options) {
		this.elem = elem;
		this.options = $.extend( {}, $.carousel.defaults, options );
		this.init();
	};
	
	$.extend( $.carousel, {
		defaults: {
			duration:1000,
			autoMode:1,
			autoMode_waitBeforeStart:8000,
			autoMode_delay:6000
		},
		prototype: {
			init:function(){
				var self = this;
				this.ready = false;
				
				this.slides = $(".carousel_contents ul li", this.elem);
				if (!this.slides.length) return;
				
				this.baseurl = $('script[src*=js/carousel.js]:first').attr('src');
				this.baseurl = this.baseurl.substring(0, this.baseurl.indexOf("js/carousel.js"));
				
				this.loader = $(".carousel_loader", this.elem);
				
				this.autoModeTimeout = 0;
				
				this.preloadImages();
			},
			preloadImages: function(){
				var to_preload = [];
				var self = this;
				
				$(".carousel_contents img", this.elem).each(function(){
					to_preload.push(this.src);
				});
				
				var preloader = new $.preloadImages(to_preload, function() {
					self.loaded.apply(self,[]);
				});
				
			},
			loaded: function(){
				var self = this;
				
				$(".carousel_loader", this.elem).animate({opacity:0},{duration:500,queue:false,complete:function(){
					$(this).hide().css("opacity","");
				}});
				$(".carousel_container", this.elem).css({opacity:0,display:"block"}).animate({opacity:1},{duration:750,queue:false,complete:function(){
					$(this).css("opacity","");
				}});
				
				this.slide_width = $(this.slides[0]).innerWidth();
				this.slide_height = $(this.slides[0]).innerHeight();
				
				this.slideContainer = $(".carousel_contents ul", this.elem);
				this.slideContainer.css({width: (this.slide_width * this.slides.length)+"px", height: this.slide_height+"px"});
				this.slideContainer.parent().css({height: this.slide_height+"px"});
				this.menu = $(".carousel_menu a", this.elem);
				$(this.menu).each(function(){
					$(this).bind("click", function(){
						self.resetAutoMode();
						self.moveTo.apply(self, [this]);
						return false;
					});
				});
				
				this.slideContainer.css({left:0});
				this.menu.removeClass("selected").filter(":eq(0)").addClass("selected");
				
				this.resetAutoMode();
			},
			moveTo: function(a) {
				if ($(a).is(".selected")) return;
				if (a.hash && a.hash.replace('#','') != "") {
					var target = $(a.hash);
					if (target.length) {
						var target_idx = this.slides.index(target);
						if (target_idx >= 0) {
							this.slideContainer.stop();
							this.slideContainer.animate({left: -(target_idx * this.slide_width)+"px"},{
								duration:this.options.duration,
								easing:"easeInOutExpo",
								queue:false
							});
						}
					}
				}
					
				this.menu.not(a).removeClass("selected");
				$(a).addClass("selected");
			},
			clearAutoMode: function(){
				if (this.autoModeTimeout > 0) {
					window.clearTimeout(this.autoModeTimeout);
				}
				this.autoModeTimeout = 0;
			},
			resetAutoMode: function(){
				var self = this;
				this.clearAutoMode();
				this.autoModeTimeout = window.setTimeout(function(){
					self.autoNext();
				}, this.options.autoMode_waitBeforeStart);
			},
			autoNext: function(){
				var cur, cur_next, self = this;
				
				this.clearAutoMode();
				cur = $(".carousel_menu a.selected", this.elem);
				if (!cur.length) {
					cur = $(".carousel_menu a:eq(0)", this.elem);
				}
				
				if (cur) {
					cur = $(cur[0]);
					cur_next = $(cur).parents("li").next();
					if (!cur_next) {
						cur_next = $(cur).parents("ul").find("li:eq(0)");
					}
				}
				
				if (!cur_next) return;
				
				cur_next = $($("a", cur_next[0])[0]);
				
				if (cur) {
					cur.removeClass("selected");
				}
				
				this.moveTo($(cur_next).get(0));
				this.autoModeTimeout = window.setTimeout(function(){
					self.autoNext();
				}, this.options.autoMode_delay);
			}
		}
	});
	
	$().ready(function(){
		$('.carousel').carousel();
	});
})(jQuery);

function equalHeight(group) {
	tallest = 0;
	group.each(function() {
		thisHeight = $(this).height();
		if(thisHeight > tallest) {
			tallest = thisHeight;
		}
	});
	group.height(tallest);
}

$(document).ready(function() {
	equalHeight($("#homepage>.box"));
	 $('a[rel*=facebox]').facebox()
});


(function($){
	$().ready(function(){
		$(".post-tools>ul>li").each(function(){
			var elem = this;
			var warp = $(">.wrap", elem);
			var trigger = $(">.wrap a", elem);
			var menu = $(">.menu", elem);
			
			if (trigger.length && menu.length && warp.length) {
				$(trigger).bind("click.postTools", function(e){
					$(warp).addClass("wrap_open");
					$(menu).show();
					$(document).triggerHandler("click.postTools");
					$(document).unbind("click.postTools").bind("click.postTools", function(e){
						var b = false, li=e.target;
						while(1) {
							li = $(li).parents("li");
							if (!li.length) break;
							if ($(li).is("body")) break;
							li = li[0];
							if (li == elem) {
								b = true;
								break;
							}
						}
						if (b) {
							e.stopPropagation();
							//return false;
						} else {
							$(menu).hide();
							$(warp).removeClass("wrap_open");
							$(document).unbind("click.postTools");
						}
					});
					
					e.stopPropagation();
					this.blur();
					return false;
				});
			}
		});
		
		//share email form
		$(".share_email form").each(function(){
			var elem = this;
			var inpEmail = $(".inpShare_email", elem);
			var inpName = $(".inpShare_name", elem);
			var msgHolder = $(".share_email_msg", $(elem).parents(".share_email"));
			
			if (!msgHolder.length) {
				msgHolder = $("<div />").addClass("share_email_msg").hide();
				msgHolder.appendTo($(elem).parents(".share_email"));
			}
			
			var showError = function(elem, msg) {
				elem = $(elem).get(0);
				if (!elem._errShareForm) {
					elem._errShareForm = $("<label />").attr("for", elem.id).addClass("error");
					elem._errShareForm.appendTo(msgHolder);
				}
				elem._errShareForm.html(msg).show();
			}
			
			var hideError = function(elem) {
				elem = $(elem).get(0);
				if (elem._errShareForm) {
					elem._errShareForm.hide();
				}
			}
			
			if (inpEmail.length && inpName.length) {
				inpEmail = inpEmail[0]; inpName = inpName[0];
				inpEmail.defaultValue = inpEmail.defaultValue || "";
				inpName.defaultValue = inpName.defaultValue || "";
				
				$([inpEmail, inpName])
					.unbind("focus.shareForm")
					.bind("focus.shareForm", function(){
						if ($(this).val() == this.defaultValue) {
							$(this).val("");
						}
						$(this).addClass("has_value");
					})
					.unbind("blur.shareForm")
					.bind("blur.shareForm", function(){
						if ( $(this).val() == "" || $(this).val() == this.defaultValue ) {
							$(this).val(this.defaultValue);
							$(this).removeClass("has_value");
						} else {
							$(this).addClass("has_value");
						}
					});
					
				$(elem)
					.unbind("reset.shareForm")
					.bind("reset.shareForm", function(){
						$([inpEmail, inpName]).triggerHandler("change.shareForm");
					})
					.bind("submit", function(){
						var isSending = elem._isSending || false;
						if (isSending) return false;
						
						elem._isSending = true;
						
						var bValid = true;
						if ( $(inpEmail).val() == "" || $(inpEmail).val() == inpEmail.defaultValue ) {
							showError(inpEmail, "Email Address is required.");
							 bValid = false;
						} else if (!(/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i.test($(inpEmail).val()))) {
							showError(inpEmail, "Invalid Email Address.");
							bValid = false;
						} else {
							hideError(inpEmail);
						}
						
						if ( $(inpName).val() == "" || $(inpName).val() == inpName.defaultValue ) {
							showError(inpName, "Your Name is required.");
							bValid = false;
						} else {
							hideError(inpName);
						}
						
						if (bValid) {
							hideError(inpName);
							hideError(inpEmail);
							
							msgHolder.hide();
							$.ajax({
								url: $(elem).attr("action") || "",
								type: $(elem).attr("method") || "post",
								data: $(elem).serialize(),
								success: function(msg) {
									elem._isSending = false;
									$(document).triggerHandler("click.postTools");
								},
								error: function() {
									elem._isSending = false;
								}
							});
							
						} else {
							elem._isSending = false;
							msgHolder.show();
						}
						
						return false;
					});
				
				$(".share_email_msg", $(elem).parents(".share_email")).html("Email has been sent!").hide();
				$(".submit", elem).bind("click.shareForm",function(){
					this.blur();
					$(elem).submit();
					return false;
				});
				elem.reset();
				
				$([inpEmail, inpName]).each(function(){
					$(this).triggerHandler("blur");
				});
			}
		});
	});
})(jQuery);