var search2dropdown = false;
var search2form = false;
var curdropbox = null;
var bagpopup = false;
var wishlistpopup = false;
var overide = false;  
var popupcleartimer = null;


function validateShippingAddress(){
	return $('#shipping_address-form').valid();
}

function setPaymentContainerOriginalHeight(identifier){
	if($('#'+identifier).data('original_height') == undefined){
		original_height = $('#'+identifier).css('height');
		original_height_value = parseInt(original_height.replace('px', ''));
		$('#'+identifier).data('original_height', original_height_value);
	}
}
function adjustPaymentContainerHeight(identifier, height){
	original_height_value = parseInt($('#'+identifier).data('original_height'));
	new_height = original_height_value + parseInt(height);
	$('#'+identifier).css('height', new_height);
}
$(document).ready(function()
{
	$("#shipping_address-form").validate({
		rules: {
			shipping_address: {
				required: true,
				number: true
			}
		}, 
		messages: {
			shipping_address: {
				required: 'This field is required.',
				number: 'This field is required.'
			}
		},
		submitHandler: function(form) {
			$.facebox({ div: '#loading' }, 'facebox-loading');
			handleShippingForm();
		}
	});
	
	$("#placeorder-form").validate();
	$("#voucher-placeorder-form").validate();
	
	$('#eftordernow').bind('click', function(event){
		if(validateShippingAddress()){
			$('#placeorder-form').submit();
		}else{
			 $('html,body').animate({scrollTop: $('#shipping_address-form').offset().top},'slow');
		}
	});
	
	$("#ccconfirm-form").validate({
		submitHandler: function() {
			$('#safeshop_form').submit();		
		}			
	});
	$('#creditcardpaynow').bind('click', function(event){
		if(validateShippingAddress()){
			$('#ccconfirm-form').submit();
		}else{
			 $('html,body').animate({scrollTop: $('#shipping_address-form').offset().top},'slow');
		}
	});

	$('#voucherordernow').bind('click', function(event){
	    $('#voucher-placeorder-form').submit();
	});
	
	$('.counter_control .counterbuttons .up').bind('click', counter_value_change).parent().children('.down').bind('click', counter_value_change);
	
    $(".provinces_accordion_container").children().accordion();
    
	//$('#mainmenu .ulmenu li .selected').css('opacity',1);
	$('.closebag').bind('click',function()
	{
		bagpopup = false;
		wishlistpopup = false;
		$(this).parent().parent().parent().parent().parent().animate({opacity:0},500,function()
		{
			$(this).hide();
		});
	});
	$('#mainmenu .ulmenu > li').bind('mouseover',function()
	{
		$(this).addClass('hover');
	});
	$('#mainmenu .ulmenu > li').bind('mouseout',function()
	{
		$(this).removeClass('hover');
	});
	$('#bagbtnover').bind('click',toggle_bagpopup);
	$('#wishbtnover').bind('click',toggle_wishpopup);
	$('#search1rel').bind('click',function()
	{
		$('#search1').focus();
	});
	$('#search2rel').bind('click',function()
	{
		$('#search2').focus();
	});
	$('#toggleadvsearchcon').bind('click',toggle_advanced_search);
	$('#search2rel, #asearchdropbtn').bind('click',toggle_search1_dropdown);
	$('.inputblock div.text').bind('click',function()
	{
		$(this).children('input').focus();
	});
	$('div.textinput').bind('click',function()
	{
		$(this).children('input').focus();
	});
	$('.inputtable .text').bind('click',function()
	{
		$(this).children('input').focus();
	});
	$('.inputblock div.select').bind('click',function()
	{
		var t = s = $(this);
		if ($(this)!=curdropbox)
		{
			$(this).parent().children(".abs").css('display','block').animate({opacity:1},500,function()
			{
				$(this).children("ul").children("li").bind("click",binddroplistelement);
			});
			if (curdropbox)
			{
				var t = curdropbox.parent().children(".abs");
				if (t.css("display")=="block"&&t.css("opacity")==1)
				{
					t.children("ul").children("li").unbind();
					t.animate({opacity:0},500,function()
					{
						$(this).hide();
					});
					curdropbox = null;
				}
			}
		}
		else
		{
			return false;
		}
		curdropbox = s;
		var n = $(this).next();
		n.offset({left:Math.ceil(s.offset().left),top:Math.ceil(s.offset().top+s.outerHeight()-3)});
	});
	$(document).bind("click",function()
	{
		if (curdropbox)
		{
			var t = curdropbox.parent().children(".abs");
			if (t.css("display")=="block"&&t.css("opacity")==1)
			{
				t.children("ul").children("li").unbind();
				t.animate({opacity:0},500,function()
				{
					$(this).hide();
				});
				curdropbox = null;
			}
		}
		if (bagpopup)
		{
			toggle_bagpopup();
		}
		if (wishlistpopup)
		{
			toggle_wishpopup();
		}
	});
	$('#socialicons').children().bind('mouseover',function()
	{
		$(this).animate({top:0},350);
	}).bind('mouseout',function()
	{
		$(this).animate({top:'18px'},350);
	});
	$.facebox.settings.opacity = 0.7;
	$('a[rel=facebox]').facebox();
	$(document).bind('afterReveal.facebox',function()
	{  
		$('#facebox').delegate('#expando', 'click',function()
		{				 
			var t = $(this).parent().parent().parent();
			t.children('#faceboxoverlay').show();
			t.children('#faceboxexpando').scrollLeft(178);
			t.animate({'width':'338px'},500);
			t.children('#faceboxexpando').animate({width:'178px',scrollLeft:0},500);
		});
		$('#facebox').children().children().children().children('.abs').children('dl').children('dd').children('div').bind('click',function()
		{
			$(this).children('input').focus();
		});
		$('#facebox').children().children().children().children('#faceboxexpando').children('dl').children('dd').children('div').bind('click',function()
		{
			$(this).children('input').focus();
		});
	});
	$('#popupprint').facebox();
	$('#mapc area').bind('mouseover',function()
	{
		var c = $(this).attr('class');
		$('.provincescontainer div.'+c).addClass('hover');
	}).bind('mouseout',function()
	{
		var c = $(this).attr('class');
		$('.provincescontainer div.'+c).removeClass('hover');
	}).bind('click',function()
	{
		var c = $(this).attr('class');
		$('.accordion_selected').removeClass('accordion_selected');
		$('.allaccordions .'+c).addClass('accordion_selected');
		$('.provincescontainer div.selected').removeClass('selected');
		$('.provincescontainer div.'+c).addClass('selected');
	});
	$('#products_review').bind('click',toggle_write_comment);
        //jCarousel
        jQuery('#mycarousel').jcarousel({
            // Configuration goes here
        });
        jQuery('#mycarousel2').jcarousel({
            // Configuration goes here
        });
        jQuery("#mycarousel3").jcarousel({
        scroll: 1,
        auto: 5,
        wrap: 'circular',
        animation: 2000,
        itemFirstInCallback:  mycarousel_itemFirstInCallback,
        initCallback: mycarousel_initCallback,
        // This tells jCarousel NOT to autobuild prev/next buttons
        buttonNextHTML: null,
        buttonPrevHTML: null
        });
        jQuery('#jcc1').css('backgroundImage','url('+site_url+'images/controll_single_jcc1.png)');
        //accordion
        
        
        $(".navigatorwidget").accordion({ header: 'ul.categories > li > a' });
        $(".faq_container #accordion").accordion();
        $(".newsletter_container #accordion").accordion();
        $('.promo_item').hover(show_promo_buttons, hide_promo_buttons);
});

function show_promo_buttons()
{
    $(this).children('.promo_details').animate({top: 280}, 500);
    $(this).children().children('.view_details_button').animate({top: 355}, 500);
}

function hide_promo_buttons()
{
    $(this).children('.promo_details').animate({top: 400}, 500);
    $(this).children().children('.view_details_button').animate({top: 475}, 500);
}

/**
 * We use the initCallback callback
 * to assign functionality to the controls
 */
function mycarousel_initCallback(carousel) {
    jQuery('.jcarousel-control li').bind('click', function() {
        carousel.scroll(jQuery.jcarousel.intval(jQuery(this).attr("id").substr(3)));
        jQuery('.jcarousel-control li').css('backgroundImage','url('+site_url+'images/controll_single_jcc0.png)');
        jQuery(this).css('backgroundImage','url('+site_url+'images/controll_single_'+jQuery(this).attr("id")+'.png)');
        carousel.startAuto(0);
        return false;
    });
};

function mycarousel_itemFirstInCallback(carousel, item, idx, state) {
    jQuery('.jcarousel-control li').css('backgroundImage','url('+site_url+'images/controll_single_jcc0.png)');
    jQuery('#jcc'+idx).css('backgroundImage','url('+site_url+'images/controll_single_jcc'+idx+'.png)');
};

function counter_value_change()
{
	if ($(this).hasClass('up'))
	{
		var change = 1;
	}
	else
	{
		var change = -1;
	}
	var counter = $(this).parent().parent().children('span');
	var counterInput = $(this).parent().parent().children('input');
	var newValue = (counter.html()/1)+change;
	if (newValue < 1){
		newValue = 1
	}
	counter.html(newValue);
	counterInput.val(newValue);
}

function toggle_bagpopup()
{
	if (wishlistpopup)
	{
		toggle_wishpopup();
	}
	if (!bagpopup)
	{
		bagpopup = true;
		$('#popupbagholder').css('opacity',0);
		$('#popupbagholder').show();
		$('#popupbagholder').animate({opacity:1},500,function()
		{
			if ($.browser.msie&&$.browser.version.slice(0,1)<'9')
			{
				$(this).css('opacity','');
			}
		});
	}
	else
	{
		bagpopup = false;
		$('#popupbagholder').animate({opacity:0},500,function()
		{
			$('#popupbagholder').hide();
		});
	}
	return false;
}

function toggle_wishpopup()
{
	if (bagpopup)
	{
		toggle_bagpopup();
	}
	if (!wishlistpopup)
	{
		wishlistpopup = true;
		$('#popupwishholder').css('opacity',0);
		$('#popupwishholder').show();
		$('#popupwishholder').animate({opacity:1},500,function()
		{
			if ($.browser.msie&&$.browser.version.slice(0,1)<'9')
			{
				$(this).css('opacity','');
			}
		});
	}
	else
	{
		wishlistpopup = false;
		$('#popupwishholder').animate({opacity:0},500,function()
		{
			$('#popupwishholder').hide();
		});
	}
	return false;
}

function binddroplistelement()
{
	if ($(this).hasClass('opennewaddresssection'))
	{
		$('.newaddresssection').show();
	}
	if ($(this).hasClass('closenewaddresssection'))
	{
		$('.newaddresssection').hide();
	}
	if ($(this).hasClass('hidepaymentslider'))
	{
		$('#paymentslideout').animate({scrollTop:406,height:'0px'}, 1200);
	}
	$(this).parent().children("li").each(function (){
		$('#'+$(this).attr('sliderid')).animate({scrollTop:406,height:'0px'}, 1200);
	});
	if ($(this).hasClass('showpaymentslider'))
	{
		setPaymentContainerOriginalHeight('sitecenter');
		height = $('#'+$(this).attr('sliderid')).attr('scrollheight');
		$('#'+$(this).attr('sliderid')).animate({scrollTop:height, height:height+'px'}, {
		    duration: 1200,
		    complete: function() {
			adjustPaymentContainerHeight('sitecenter', height);
		}});

		
	}
	$(this).parent().parent().parent().children(".select").children('span').html($(this).html());
	$(this).parent().children("li").unbind();
}

function toggle_write_comment()
{
	if ($(this).css('backgroundImage').indexOf("advancedtoggle_btn.png")>-1)
	{
		$(this).css('backgroundImage','url('+site_url+'images/advancedtoggle_btnon.png)');
		$(".slidercontainer").scrollTop(385);
		$('.slidercontainer').animate({scrollTop:0,height:'385px'}, 800);
	}
	else
	{
		$(this).css('backgroundImage','url('+site_url+'images/advancedtoggle_btn.png)');
		$('.slidercontainer').animate({scrollTop:385,height:'0px'}, 800);
	}
}

function toggle_advanced_search()
{
	if (search2dropdown)
	{
		toggle_search1_dropdown();
	}
	if ($(this).children().css('backgroundImage').indexOf("advancedtoggle_btn.png")>-1)
	{
		$(this).children().css('backgroundImage',"url("+site_url+"images/advancedtoggle_btnon.png)");
		$("#s1h").show();
		if (search2form)
		{
			$("#s1h").scrollTop(418);
			$('#s1h').animate({scrollTop:0,height:'418px'}, 1200,function()
			{
				$('#s1h').css("overflow","visible");
			});
		}
		else
		{
			$("#s1h").scrollTop(92);
			$('#s1h').animate({scrollTop:0,height:'92px'}, 700);
		}
	}
	else
	{
		$(this).children().css('backgroundImage',"url("+site_url+"images/advancedtoggle_btn.png)");
		if (search2form)
		{
			if (search2dropdown)
			{
				toggle_search1_dropdown();
			}
			$('#s1h').css("overflow","hidden");
			$('#s1h').animate({scrollTop:418,height:'0'}, 1200,function()
			{
				$("#s1h").hide();
			});
		}
		else
		{
			$('#s1h').animate({scrollTop:92,height:'0'}, 700,function()
			{
				$("#s1h").hide();
			});
		}
	}
}

function toggle_search1_dropdown(entity, triggerclick, show_advanced_select)
{
	if(show_advanced_select == null){
		show_advanced_select = true;
	}
	
	if (search2dropdown)
	{
		$('#s1droplist > li').unbind("click",set_search1_dropform);
		$('#s1droprel').animate({opacity:0}, 500,function()
		{
			$('#s1dropabs').hide();
			search2dropdown=false;
			//$('[name=productstype]').val(entity);
			var advancedSearchFormId = '#advanced_search_' + entity;
			$('.advanced_search').hide();
			$(advancedSearchFormId).show();
			
			$('#advancedsearchsubmit' + entity).bind('click', function(){
				$('#advancedsearch' + entity).submit();
				return false;
			});			
			
		});
	}
	else
	{
		search2dropdown=true
		if(show_advanced_select == true){
			$('#s1dropabs').show();
		}
		$('#s1droprel').animate({opacity:1}, 500,function()
		{
			$('#s1droplist > li').bind("click",set_search1_dropform);
			if(triggerclick == true){
				$('#s1droplist li#'+entity).trigger('click');
			}
		});
	}
}
function set_search1_dropform()
{
	var selectedItem = $(this).html();
	var entity = $(this).attr('id');
	
	$('#search2rel').html(selectedItem);
	toggle_search1_dropdown(entity);
	$('.advancedsearch1').css('background-position','top left');
	if (!search2form)
	{
		search2form = true;
		$('#s1h').css('height',418);
		$('#s3h').show();
		$("#s3h").scrollTop(324);
		$('#s3h').animate({scrollTop:0,height:'326px'}, 1000,function()
		{
			$(this).css("overflow","visible");
			$('#s1h').css("overflow","visible");
		});
	}
}
