/******
 *
 * Savistores default init file
 * http://savistores.com
 *
 * Copyright 2010, Savisites, LLC
 * May not be copied or redistributed without permission
 *
 ******/

/**
 * @function  popWin()  Generic helper for creating Strict valued popup windows
 *
 * @param  url     The Url to feed the new window
 * @param  target  The window target (window name or '_parent', etc) - default is '_blank'
 * @author Eric Falkner
 */
 
function popWin(url, target) {
	if(!target) {target = '_blank';}
	blankWin = window.open(url,target);
};

	
$(document).ready(function(){
	$("li.has-submenu").hoverIntent(function(){
		$(this).addClass("activeMenu");
		$(".submenu:first", $(this)).show();
	},function(){
		$(this).removeClass("activeMenu");
		$(".submenu:first", $(this)).hide();
	});

//	$( ".datePicker" ).livequery(function(){
//		$(this).datePicker({
//			showOn: "button",
//			buttonImage: "/css/images/calendar.gif",
//			buttonImageOnly: true
//		});
//	});

	//Fadeout notices
	setTimeout(function(){
		$('.error:not(.nofade)').slideUp('fast');
		$('.notice:not(.nofade)').slideUp('fast');
		$('.success:not(.nofade)').slideUp('fast');
		$('.info:not(.nofade)').slideUp('fast');
	}, 4000);

	//Popup for confirming
	$('.rusure').livequery('click', function(eve){
		eve.preventDefault();
		var el = $(this);
		msg = el.attr('title');
		if(msg == '') {
			msg = "Are You Sure?";
		}
		jConfirm(msg,'Please Confirm', function(r){
			if(r)
			{
				window.location.href = el.attr('href');
			}
		});
	});
	
	//Ajax Color Box modded by EF to allow custom widths using the REL tag
	$('.colorbox').livequery('click', function(eve){
		eve.preventDefault();
		if ($(this).attr('rel')) {
			rel_val = $(this).attr('rel');
		} else {
			rel_val = 800;
		}
		$(this).colorbox({width:rel_val+"px", height:"75%"});
	});
	
//	$('a.imgbox').livequery('click', function(eve){
//		eve.preventDefault();
//		$.colorbox({transition:"fade", opacity:0.5, rel:"book-images"});
//	});

	$("a.imgbox").livequery(function(){$(this).colorbox({transition:"fade", opacity:0.5});});

	$('.look-inside').livequery('click', function(eve){
		eve.preventDefault();
		window.open($(this).attr('href'),'interactive','width=1000,height=800,menubar=no,status=no,location=no,toolbar=no,resizable=no'); 
	});
	
	//checks if an item has custom fields and if so, it pops up a colorbox
	//otherwise it just forwards to the cart add
	$('.add-item-cart').livequery('click', function(eve){
		eve.preventDefault();
		$('body').addClass('working');
		var el = $(this);
		var link = el.attr('href');
		var encode = el.attr('rel');
		var old_html = el.html();
		//el.html('loading');
		//el.attr('href', '#');
		el.removeAttr('href');

		$.get(link+encode, function(data){
			//alert(data);
			$('body').removeClass('working');
			if(data == 1) {
				el.attr('href', link);
				$.fn.colorbox({href:"/cart/fields/"+encode, width:"800px", height:"85%"});
			} else {
				window.location.href = '/cart/process/add/'+encode;
			}
		});
	});
	
	$('.popup').livequery('click', function(event){
		event.preventDefault();
		var link = $(this).attr('href');
		popWin(link);
	});
	
	$('.product-photo-secondary').livequery('click', function(event){
		event.preventDefault();
		var file_name = $(this).attr('rel');
		$('#product-photo-primary').attr('src', '/img/size/o/'+file_name+'/w/185/h/245/m/height.jpg');
		$('#product-photo-primary-noshadow').attr('src', '/img/size/o/'+file_name+'/w/185/h/245/m/height.jpg');
		$('#product-photo-primary-outline').attr('src', '/img/size/o/'+file_name+'/w/185/h/245/m/height.jpg');
	});
	
	$('.product-photo-alternate').livequery('click', function(event){
		event.preventDefault();
		var file_name = $(this).attr('rel');
		var file_title = $(this).attr('rel');
		$('#product-photo-primary').attr('src', '/img/size/o/'+file_name+'/w/350/h/350/m/width.jpg');
		$('#product-photo-primary-noshadow').attr('src', '/img/size/o/'+file_name+'/w/350/h/350/m/height.jpg');
		$('#product-photo-primary-outline').attr('src', '/img/size/o/'+file_name+'/w/350/h/350/m/height.jpg');
		if($('#product-photo-primary-link').hasClass('imgbox'))
		{
			$('#product-photo-primary-link').attr('href', '/img/size/o/'+file_title+'/w/800/h/600/m/height.jpg');
		}
		
		//fixes bug with img popup
		$('.imgboxfirst').addClass('imgbox');
	});
	
	SimpleOverSetup();
	
	//Expander
	// $('div.expandable p').expander({
	//     	slicePoint:       400,  // default is 100
	//     	expandText:         'read more...', // default is 'read more...'
	//     	collapseTimer:    5000, // re-collapses after 5 seconds; default is 0, so no re-collapsing
	//     	userCollapseText: '<br />[ hide more ]'  // default is '[collapse expanded text]'
	//   	});
	
	//Tabs
	$('.tabs').tabs();
	
	//make the search box words disappear
	$('#search_box').focus(function() {
		var input_val = $(this).val();
		if (input_val == 'search...') {
			$(this).val('');
			$(this).css('color', '#333');
		}			
	});
	
	$(".overflow-related").hide();
	$('#related-toggle').live('click', function(eve){
		eve.preventDefault();
		$(".overflow-related").slideToggle();
		var cur = $('#related-showhide').html();
		if(cur == 'Show')
		{
			$('#related-showhide').html("Hide");
		}
		else
		{
			$('#related-showhide').html("Show");
		}
	});
	
	
	//Functions for shopping-cart page and account page
	$('.table-divider').last().hide();
	//for shopping cart + button
	$('.add-item-plus-cart').livequery('click', function(eve){
		eve.preventDefault();
		var el = $(this);
		var link = el.attr('href');
		var encode = el.attr('rel');
		el.attr('href', '#');

		$.get(link+encode, function(data){
			//alert(data);
			if(data == 1) {
				$.colorbox({href:"/cart/fields/"+encode, width:"800px", height:"85%"});
			} else {
				window.location.href = '/cart/process/add/'+encode;
			}
		});
	});

	
	$('.show-fields').livequery('click', function(eve){
		eve.preventDefault();
		var el = $(this);
		var table = el.attr('rel');
		if (el.html() == 'show / edit personalized fields') {
			el.html('hide personalized fields');
		} else {
			el.html('show / edit personalized fields');
		}

		$('#'+table).slideToggle('slow');

	});
	
	$('h4').each(function() {
		var html_content = $(this).text();
		$(this).html('<span>'+html_content+'</span>')
	});
	
	//Fix YouTube iFrame  
	
	$('iframe').each(function(){
		var src = $(this).attr('src');
		if(src.indexOf('youtube') > -1)
		{
			$(this).attr('src', $('iframe').attr('src')+'?wmode=opaque'); 	
		}
	});
	var wmode = $('<param>'); 
	$(wmode).attr('name', 'wmode'); 
	$(wmode).val('opaque'); 
	$('object').append($(wmode));
	
	$('#product-content-tabs').tabs({ cache: false });
	
	$('#stickycart').ready(function()
	{
		if( $('#stickycart').hasClass('shown') )
		{
			setTimeout(function(){
				$('#stickycart').stop().animate({'marginLeft':'0px'},'medium','easeInQuart');
			},3000);
			
		}
	
	});

	$('#stickycart').hover(
		function () {
			$(this).stop().animate({'marginLeft':'-265px'},350);
		},
		function () {
			$(this).stop().animate({'marginLeft':'0px'},270);
		}
	);

});
