// ***************************************************************************
//   Capital IT Inc. Copyright (C) 2009. All Rights Reserved  $Revision: 1.47 $
// ***************************************************************************

// Add js css hook
document.documentElement.className += " js ";

// Globals
var base = null;
function timestamp(){
	return +new Date;
}
// "inlined" jQuery color animation support
(function($){$.each(["backgroundColor","borderBottomColor","borderLeftColor","borderRightColor","borderTopColor","color","outlineColor"],function(e,d){$.fx.step[d]=function(f){if(f.state==0){f.start=b(f.elem,d);f.end=a(f.end)}f.elem.style[d]="rgb("+[Math.max(Math.min(parseInt((f.pos*(f.end[0]-f.start[0]))+f.start[0]),255),0),Math.max(Math.min(parseInt((f.pos*(f.end[1]-f.start[1]))+f.start[1]),255),0),Math.max(Math.min(parseInt((f.pos*(f.end[2]-f.start[2]))+f.start[2]),255),0)].join(",")+")"}});function a(e){var d;if(e&&e.constructor==Array&&e.length==3){return e}if(d=/rgb\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*\)/.exec(e)){return[parseInt(d[1]),parseInt(d[2]),parseInt(d[3])]}if(d=/rgb\(\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*\)/.exec(e)){return[parseFloat(d[1])*2.55,parseFloat(d[2])*2.55,parseFloat(d[3])*2.55]}if(d=/#([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})/.exec(e)){return[parseInt(d[1],16),parseInt(d[2],16),parseInt(d[3],16)]}if(d=/#([a-fA-F0-9])([a-fA-F0-9])([a-fA-F0-9])/.exec(e)){return[parseInt(d[1]+d[1],16),parseInt(d[2]+d[2],16),parseInt(d[3]+d[3],16)]}}function b(f,d){var e;do{e=$.curCSS(f,d);if(e!=""&&e!="transparent"||$.nodeName(f,"body")){break}d="backgroundColor"}while(f=f.parentNode);return a(e)}})(jQuery);
// "inlined" jQuery input hint support
(function($){$.fn.inputHint=function(options){options=$.extend({hintClass:"hint"},options||{});function showHint(){if($(this).val()==""){$(this).addClass(options.hintClass).val($(this).attr("hint"))}}function removeHint(){if($(this).hasClass(options.hintClass)){$(this).removeClass(options.hintClass).val("")}}this.focus(removeHint).blur(showHint).blur();var $form=this.parents("form:eq(0)");return this.each(function(){var self=this;$form.submit(function(){removeHint.apply(self)})})}})(jQuery);
// "inlined" jQuery progress bar support
(function($){$.fn.reportprogress=function(c,d){var b=100;if(d){b=d}return this.each(function(){var g=$(this);var f=g.find(".progress");if(f.length!=1){f=$("<div class='progress'></div>");g.append("<div class='text'>&nbsp;</div>");$("<span class='text'>&nbsp;</span>").css("width",g.width()).appendTo(f);g.append(f)}var e=Math.round(c/b*100);f.css("width",e+"%");g.find(".text").html(e+" %")})}})(jQuery);
// "inlined" jQuery cookie support
(function($){$.cookie=function(b,j,m){if(typeof j!="undefined"){m=m||{};if(j===null){j="";m.expires=-1}var e="";if(m.expires&&(typeof m.expires=="number"||m.expires.toUTCString)){var f;if(typeof m.expires=="number"){f=new Date();f.setTime(f.getTime()+(m.expires*24*60*60*1000))}else{f=m.expires}e="; expires="+f.toUTCString()}var l=m.path?"; path="+(m.path):"";var g=m.domain?"; domain="+(m.domain):"";var a=m.secure?"; secure":"";document.cookie=[b,"=",encodeURIComponent(j),e,l,g,a].join("")}else{var d=null;if(document.cookie&&document.cookie!=""){var k=document.cookie.split(";");for(var h=0;h<k.length;h++){var c=jQuery.trim(k[h]);if(c.substring(0,b.length+1)==(b+"=")){d=decodeURIComponent(c.substring(b.length+1));break}}}return d}};})(jQuery);

// Custom country / state province plugin
(function($){
	$.fn.country = function(options){
		return this.each(function(){
			var $country = $(this)
			var matches = $country.attr("id").match(/^(.*)-/);
			var prefix = matches ? matches[1] + "-" : "";
			var $stateProvinceName = $("#" + prefix + "state-province-name");
			$stateProvinceName.bind("change, keyup", function(){
				if ($country.val() == "null") {
					alert("Please select a country first")
					this.value = ""
				}
			});
			return $country.change(function(){
				var $stateProvince = $("#" + prefix + "state-province")
				var $stateProvinceName = $("#" + prefix + "state-province-name");
				var $indicator = $stateProvince.nextAll(".state-province-indicator");
				if (this.value == "null") {
					$stateProvince.val(this.value).hide()
					$stateProvinceName.val("");
					$stateProvinceName.show();
					$indicator.hide();
					return true;
				}
				$indicator.fadeIn();
				$.get(base + "/country/stateProvinces", {id: this.value}, function(html){
					$indicator.fadeOut();
					var $html = $(html);
					if ($html.is("select")) {
						$stateProvinceName.val("").hide();
						var $options = $(html).children();
						$stateProvince.empty().append($options).find("option:first").attr("selected","selected").end().show().focus();
					} else {
						$stateProvince.val("null").hide()
						$stateProvinceName.show();
					}
				})
			});
		});
	};
})(jQuery);

// Custom image upload plugin (uses jquery.form)
(function($){
	$.fn.imageUpload = function(options){
		var $file = this;
		var $form = $file.closest("form");
		var action = $form.attr("action");
		var url = action + "/upload";
		$file.change(function(){
			if ($file.data("locked")) {
				return false;
			}
			$file.data("locked", true);
			$file.blur();
			var $indicator = $("#picture-indicator").fadeIn("fast");
			var $picture = $("#picture");
			$("#progress-bar").reportprogress(0).slideDown();
			var timeout = setInterval(function(){
				$.get(base + "/image/progress", {}, function(json){
					// $("#image-status").text(+json.percentDone + "%");
					$("#progress-bar").reportprogress(json.percentDone);
				}, "json");
			}, 2000)
			$form.ajaxSubmit({
				url: url,
				dataType: "json",
				success: function(json){
					$("#progress-bar").slideUp();
					clearTimeout(timeout);
					if (json.success) {
						var src = action + "/thumbnail/" + "?" + timestamp();
						$picture.attr("src", src);
					} else {
						alert(json.message);
						$indicator.fadeOut("fast");
					}
				},
				error: function (XMLHttpRequest, textStatus, errorThrown) {
					$("#progress-bar").slideUp();
					clearTimeout(timeout);
					$file.attr("disabled", false);
					$indicator.hide();
					alert("There was an error uploading your image");
				}
			});
			$file.data("locked", false);
		});
		$("#picture").load(function(){
			$(this).fadeIn("slow", function(){
				$("#picture-indicator").fadeOut("slow");
			});
		});
	};
})(jQuery);

//Custom url plugin
(function($){
	$.fn.url = function(options){
		this.focus(function(){
			if ($.trim(this.value) == "") {
				this.value = "http://";
				$(this).select();
			}
		}).blur(function(){
			if ($.trim(this.value) == "http://") {
				this.value = "";
			}
		})
	};
})(jQuery);

// Custom dialog plugin (uses jquery.simplemodal)
(function($){
	$.fn.dialog = function(options){
		return this.each(function(){
			$.get(options.url, function(html){
				var speed= 200;
				var $html = $(html)
				if (options.callback) {
					options.callback($html);
				}
				$html.modal({
					containerCss: options.containerCss || {},
					opacity: 20,
					onOpen: function(dialog){
						dialog.overlay.fadeIn(speed, function (){
							if (options.onOpen) {
								options.onOpen(dialog);
							}
							$html.show();
							dialog.container.css({height: "auto", backgroundColor: "#d5d6e9"}).fadeIn(speed);
						});
					},
					onClose: function(dialog){
						dialog.container.fadeOut(speed, function () {
							dialog.overlay.fadeOut(speed, function (){
								$.modal.close();
								if (options.onClose) {
									options.onClose(dialog);
								}
							});
						});
					}
				});
			})
		});
	};
})(jQuery);

// Custom yellow fade technique plugin (uses jquery.color)
(function($){
	$.fn.yellowFade = function() {
		this.css({backgroundColor: "#f4f4f9"})
			.animate({backgroundColor: "#ffffff"}, 1500);
	};
})(jQuery);

// Custom image preloader and easing
(function($){
	$.preload = function() {
		$.each (arguments, function(e){
			$ ('<img>').attr("src",  base + "/images/" + this);
		});
	};
	$.easing.easeOutQuart = function (x, t, b, c, d) {
		return -c * ((t=t/d-1)*t*t*t - 1) + b;
	};	
})(jQuery);

// Establish base path for js
base = $("meta[name='base']").attr("content");
//$.preload("btn-iwantapet.gif", "btn-iwantapet-selected.gif","btn-iamabreeder.gif", "btn-iamabreeder-selected.gif","btn-ihaveapet.gif", "btn-ihaveapet-selected.gif");
$(function(){
	$.ajaxSetup({cache:false});
	
	// Survey behavior
	if (!$.cookie("survey")) {
		setTimeout(function(){
			$("<a>").attr({id: "survey-link", href: base + "/survey"}).css({opacity: 0.0}).appendTo(".mainbox-header").animate({right: "-30px", opacity: 1.0},1000, function(){
				var $link = $(this);
				var count = 6;
				var id = setInterval(function(){
					$link.toggleClass("hover");
					if (--count == 0) {
						clearTimeout(id);
					}
				}, 50);
			});
			$.cookie("survey", 1, {path: "/", expires: 7});
		}, 2*1000);
	}
	
	// Search form behavior
	$("#query").inputHint();
});
