//*** Place any JavaScript to be loaded in the <head> area here. ***
$(document).ready(function() {

	//Append the InputFocus() and InputBlur() functions to any elements with the class "clearfocus"
	$(".clearpassword").focus(function() { PasswordFocus(this); });
	$(".clearpassword").blur(function() { PasswordBlur(this); });
	$(".clearfocus").focus(function() { InputFocus(this); });
	$(".clearfocus").blur(function() { InputBlur(this); });
	$(".resetfield").each(function() { $(this).val( this.defaultValue ); });

	//preload CSS images
	$.preloadCssImages();
	
	//rotate logo on mouse over for fun
	$(".rotate-image").rotate({
		bind: {
			mouseenter: function(){
				if ($(this).attr('rotating') != 1) {
					var element = $(this);
					element.attr('rotating', '1');
					$(this).rotate({
						angle:0,
						animateTo:360,
						duration: 6200,
						easing: $.easing.easeOutElastic,
						callback: function() {
							element.attr('rotating', '0');
						}
					});
				}
			}
		}
	});
	
	//expand the white background if sidebar is too tall
	FixContentHeight();
	
	//cycle slideshow rotator with nav for featured images
	$('.featured .rotator').cycle({ 
		fx:     'fade', 
		speed:  350, 
		timeout: 7500,
		pause: 1,
		pager:  '#featured-nav' 
	});
	
	//Custom CSS for apple devices because they render the elements strangely with 1px lines
	if (isAppleDevice() == true) {
		$('.hero').css('margin-top', '-1px');
		$('.sidebar-left').css('margin-left', '-1px');
		$('.middle').css('margin-bottom', '-1px').css('margin-top', '-1px');
		$('.content .tab').css('margin-top', '-1px').css('margin-bottom', '-1px').css('height', '42px');
		$('.block.featured').css('margin-right', '-2px');
		$('.content').css('margin-right', '-2px');
	}
	
	//fix sidebar h2 height stuff
	$('.sidebar-left h2').wrapInner('<div class="inner" />');
	
    //jquery validate defaultInvalid method
	jQuery.validator.addMethod("defaultInvalid", function(value, element) {
		if (element.value == element.defaultValue) {return false;}
		return true;
	}, "This field is required.");
	
});

//Window load - functions to be performed when all page assets (e.g. images) are loaded
$(window).load(function() {
	//expand the white background if sidebar is too tall
	FixContentHeight();
});


//*** Custom Functions ***
function FixContentHeight() {
	$('.middle .expander').height(0);
	if ($('.sidebar-left').height() > $('.content').height()) {
		var difference = $('.sidebar-left').height() - $('.content').height();
		$('.middle .expander').height(difference);
	}
}

function isAppleDevice(){
    return (
        //Detect iPhone
        (navigator.platform.indexOf("iPhone") != -1) ||
        //Detect iPod
        (navigator.platform.indexOf("iPod") != -1) ||
        //Detect iPad
        (navigator.platform.indexOf("iPad") != -1)
    );
}

function ShowGallery(image_array) {
	$.fancybox(image_array, {
		'padding'			: 0,
		'transitionIn'		: 'elastic',
		'transitionOut'		: 'elastic',
		'type'              : 'image'
	});
	return false;
}


//*** Basic Functions ***

function InputFocus(element) {
	if (element.value == element.defaultValue) {
		element.value = "";
		$(element).addClass('filled');
	}
}
function InputBlur(element) {
	if (element.value == "") {
		$(element).removeClass('filled');
		element.value = element.defaultValue;
	}
}
function PasswordFocus(element) {
	if (element.value == element.defaultValue) {
		if ( ! $.browser.msie) { element.type = "password"; }
		element.value = "";
		$(element).addClass('filled');
	}
}
function PasswordBlur(element) {
	if (element.value.length == 0) {
		$(element).removeClass('filled');
		if ( ! $.browser.msie) { element.type = "text"; }
		element.value = element.defaultValue;
	}
}

//Function to make an element, e.g. content area, fill the visible space so a footer appears at the bottom. Wrapper can be a container or body element.
function MakeElementFillVisibleSpace(element_to_adjust, wrapper_element) {
	if (wrapper_element == undefined) { wrapper_element = 'body'; }
	$(element_to_adjust).height('auto');
	if ( $(wrapper_element).height() < $(window).height() ) {
		height_difference = $(window).height() - $(wrapper_element).height();
		new_height = $(element_to_adjust).height() + height_difference;
		$(element_to_adjust).height(new_height);
	}
}

//Make single element the same height as another single element - good for having two columns the same height
function MatchHeight(elementToAdjust, elementToCompare, adjust_offset, compare_offset) {
	if (isNaN(adjust_offset)) {
		adjust_offset=$(elementToAdjust).outerHeight(true) - $(elementToAdjust).height();
	};
	if (isNaN(compare_offset)) {
		compare_offset=$(elementToCompare).outerHeight(true) - $(elementToCompare).height();
	};
	if ( $(elementToAdjust).height()+adjust_offset < $(elementToCompare).height()+compare_offset ) {
		$(elementToAdjust).height( $(elementToCompare).height()+compare_offset-adjust_offset );	//adjust the height of the adjust element to the compare element
	}
}

//get a querystring by name
function querySt(ji) {
	hu = window.location.search.substring(1);
	gy = hu.split("&");
	for (i=0;i<gy.length;i++) {
		ft = gy[i].split("=");
		if (ft[0] == ji) {
			return ft[1];
		}
	}
}
