var enter_a_value_text = 'Please enter quantity before adding product to basket';

jQuery(document).ready(function()
{
	jQuery('#nav li').hover(function(){jQuery(this).find('ul').show()},function(){jQuery(this).find('ul').hide()});
	
	jQuery('.hidden').hide();
	
	// Run the default switch...
	set_switch('.infoSwitcher');
	
	set_switch('.opt_switcher');
	
	// info switcher show hide...
	jQuery('.show').click(function(ev){
		ev.preventDefault();
		jQuery('.show').each(function(){$(this).parent('li').removeClass('selected')});
		jQuery(this).parent('li').addClass('selected');	
		set_switch('.infoSwitcher');
	});	
	
	// info switcher show hide...
	jQuery('.opt_show').click(function(ev){
		ev.preventDefault();
		jQuery('.opt_show').each(function(){$(this).parent('li').removeClass('selected')});
		jQuery(this).parent('li').addClass('selected');	
		set_switch('.opt_switcher');
	});	
	
	/*
	
	// AJAX forms handler...
	$('form.ajax').append('<input type="hidden" name="ajax" value="true" />');
	
	
	$('form.ajax').submit(function() {
	  	// Serialise the form data
		var postdata = $(this).serializeArray();
		var action = $(this).attr('action');
		var loader = $(this).parents('.data');
		
		$(loader).load(action,postdata,function(data){
			
		});
		
		// Don't submit the form
	 	return false;
	});
	
	*/
	
	init_ajax_form('form.ajax');
	
	if($('#promo_popup').length){
		var popup = '<div class="popup"><div class="window"><a class="close" href="#">No thanks</a><div class="data"><form id="popup_content"></form></div></div></div>';
		var popup_html = $('#promo_popup').html();
		
		$('body').append(popup);
		$('#popup_content').append(popup_html);
		
		$('.popup a.close').click(function(ev){$('#promo_popup').hide()});
		
		$('#popup_content .addtobasket').click(function(ev){
			ev.preventDefault();
			if($('#popup_content .qty').attr('value') == ""){
				alert(enter_a_value_text);
			}else{
				popup_html = $('#popup_content').html();
				$('#promo_popup').html('');
				popup_data = $('#popup_content').serializeArray();
				$('.popup').fadeOut('slow')
				
				for(var i in popup_data){
					var obj = popup_data[i];
					$('#basket_form').append('<input type="hidden"	name="'+obj.name+'" value="'+obj.value+'" />');
				}
				$('#submit_basket').click();
			}
		});
	}
	
	
	// Popup styling
	var screenWidth = $(window).width();
	var docHeight = $(document).height();
	var screenHeight = $(window).height();
	var scrollTop = $(document).scrollTop();
	
	
	$(".popup").width(screenWidth);
	$(".popup").height(docHeight);
	//$('.popup').css('opacity',0);
	$('.popup').css('display','block');

	$('.popup').css('position','absolute');
	$('.popup').css('top',0);
	$('.popup').css('left',0);
	$('.popup').css('padding',0);
	$('.popup').css('margin',0);
	
	var libraryHeight = $(".window").outerHeight();
	var libraryWidth = $(".window").outerWidth();
	
	var negHeight = (screenHeight-scrollTop)-libraryHeight;
	var negWidth = screenWidth-libraryWidth;
	
	negHeight = Math.round(negHeight/2);
	negWidth = Math.round(negWidth/2);
	
	if(negHeight < 0) negHeight = 0;
	if(negWidth < 0) negWidth = 0;
	
	$(".window").css('position','absolute');
	$(".window").css('top',negHeight);
	$(".window").css('left',negWidth);
	$('.window').css('margin',0);
	
	//$('.popup').animate({'opacity':1},'slow');
	$('popup').show();
	$('.popup a.close').click(function(ev){$('.popup').fadeOut('slow');ev.preventDefault()});
	
	
	/* Add button validation */
	add_button_validation()
	
	
	
});

/* SET SWITCH
--------------------------------------
- hide the .hiddens
- get the href of the clicked on link
- get an element by ID which matches that HREF
- show that element

*/

function set_switch(container)
{
	jQuery(container+' .hidden').hide();
	var showme = jQuery(container+' li.selected a').attr('href');
	if(showme) jQuery(showme).show();
}

/* INIT AJAX FORM
---------------------------------------
*/
function init_ajax_form(selector)
{
	$(selector).append('<input type="hidden" name="ajax" value="true" />');
	
	
	$(selector).submit(function() {
	  	// Serialise the form data
		var postdata = $(this).serializeArray();
		var action = $(this).attr('action');
		var loader = $(this).closest('.data');
		var form = $(this);
		
		$(loader).load(action,postdata,function(data){
			init_ajax_form(selector);
		});
		
		// Don't submit the form
	 	return false;
	});	
}

/* ADD BUTTON VALIDATION
------------------------------------------
- Look for empty boxes (if they have the class 'value-required'
*/

function add_button_validation(){
	$('.add-button').click(function(ev){
		if(($('.value-required').length) && (!$('.value-required[value!=""]').length)){
			alert(enter_a_value_text);
			ev.preventDefault();
		}
	});	
}


