//=======================================================================================================
//-------------------------------------------------------------------------------------------------------
//	Custom Javascript functions
//-------------------------------------------------------------------------------------------------------
//=======================================================================================================
$(document).ready(function(){
	//	Init searchbar
	$('[placeholder]').focus(function() {
	  var input = $(this);
	  if (input.val() == input.attr('placeholder')) {
	    input.val('');
	    input.removeClass('placeholder');
	  }
	}).blur(function() {
	  var input = $(this);
	  if (input.val() == '' || input.val() == input.attr('placeholder')) {
	    input.addClass('placeholder');
	    input.val(input.attr('placeholder'));
	  }
	}).blur();
	
	$('form#search input[name="findtext"]').liveSearch({typeDelay: 10,url: '/site/quicksearch.php?findtext='});

	//	search tracking
	$('form#search input[name="findtext"]').change(function(event){
		_gaq.push(['_trackEvent', 'Search', 'Live Search', $(this).val()]);
	});

	//	Compare Form: Add items to compare session variable
	$('#compare-form input').change(function(event){
		var value = $(this).val();
		var checked = $(this).is(':checked');
		$.post("/search/add_to_compare.php", { add: checked, id: value } );
	});

	//	Menu
	$("#mainnav").menumaker({
		menu_speed:	50,
		constrain_to:	"center-x",
		offset_mode:	"left"
	})
	
	//	Tabs
	$("ul.tabs").tabs("div.panes > .pane",{
		onClick: function(event, tabIndex) {
			this.getPanes().find('.scrollable').scrollable({circular:false });
		}
	});
	
	//	Overlays
	$(".overlay-trigger").overlay();
	
	//	Scrollers
	$(".scrollable").scrollable({ circular:true });
	
	//	Toggle Div
	$('a.toggler').toggle(function(event) {
		event.preventDefault();
		var target = $(this).attr('rel');
		$(target).slideDown();
	}, function(event) {
		event.preventDefault();
		var target = $(this).attr('rel');
		$(target).slideUp();
	});

	//	Finishes page image toggle
	$('.finishes a').click(function(event){
		event.preventDefault();

		$('.finishes .show').addClass('hidden').removeClass('show');

		var id = $(this).attr('href');

		$(id).removeClass('hidden').addClass('show');
	});
	
	//	Load form action in div via ajax
	$('.ajax-form').submit(function(event){
		event.preventDefault();
		var action	= $(this).attr('action');
		var target	= $(this).attr('target');
		var data  	= $(this).serialize();

		$(target).load(action,data,function(){
			SetupPopUpForm();
			SetupSelectAll();
		});
	});
	
	//	Moved off to separate functions so we could initilize on ajax loads
	SetupPopUpForm();
	SetupSelectAll();

	//	Share/Email
	$("a.share-email").overlay({
		mask: 'darkred',
		onBeforeLoad: function() {
			// grab wrapper element inside content
			var wrap	= this.getOverlay().find(".contentWrap");
			var	url		= this.getTrigger().attr("href");
			wrap.empty().prepend('<iframe id="emailthis" src="'+url+'"></iframe>');
		}
	});

	//	Social Media Share - Open new window
	$("a#share-facebook,a#share-twitter,a#share-linkedin").click(function(event){

		//	Master category for all shares
		var	category	= 'Share';

		//	Share category
		var	subcategory	= $(this).attr("id");
		subcategory		= subcategory.split("-");
		subcategory		= subcategory[ ( subcategory.length - 1 ) ];

		//	Label based on title
		var	label		= $(document).attr("title");

		var	attributes	= "width=550,height=300,location=0,menubar=0,toolbar=0";
		window.open( $(this).attr("href"), "share", attributes, true );
		event.preventDefault();
	});
	
	//---------------------------------------------------------------------------------------------
	//	Input labels
	//---------------------------------------------------------------------------------------------
	$('.textfield').focus(function(event){
		$(this).closest('div').children('.inline').animate( { "opacity" : .5 }, 250 );
	});

	$('.textfield').keydown(function(event){
		$(this).closest('div').children('.inline').fadeOut();
	});
	
	$('.textfield').blur(function(event){
		if (!$(this).val()) {
			$(this).closest('div').children('.inline').animate( { "opacity" : 1 }, 250 );
		};
	});

});	

function SetupSelectAll() {
	$('.selectall').click(function() {
		if ($(this).attr('checked')) {

			$(this).closest('.selections').find('.selectall').attr('checked',true);

			$(this).closest('.selections').find('.download:checkbox').attr('checked',true).click(function(){
				$(this).closest('form').find('.selectall').attr('checked',false);
			});
		} else {
			$(this).closest('.selections').find('input:checkbox').attr('checked',false);
		}
	});
}

function SetupPopUpForm() {
	$('.popup-form').submit(function() {
		var w = 500;
		var h = 300;
		var winl = (screen.width-w)/2;
		var wint = (screen.height-h)/2;
		var settings='height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars=no,toolbar=no';
        window.open('', 'formpopup', settings);
        this.target = 'formpopup';
    });
}
