$(document).ready(function(){

    if(liveperson_active){
		var tmp = $('#liverperson_header_button').find('img').attr('src');
		if(tmp.match(/repoffline/g) == 'repoffline' ){
			$('#liverperson_header_button').hide();
		}
		//hide the powered by liveperson text
		$('.lpPoweredBy').hide();
    }

	//sign in overlay
	$('#header_signin').bind('click mouseover', function(){
		showSigninOverlay = true;
		showSigninForm();
	});
	$('#signin_tab').bind('click mouseover', function(){
		showSigninOverlay = true;
	});
	$('#signin_tab').bind('mouseleave', function(event){
		var relativeY = event.pageY - this.offsetTop;
		showSigninOverlay = false;
		hideSigninFormWithDelay();
	});
	$('#signin_box').bind('click mouseover', function(){
		showSigninOverlay = true;
	});
	$('#signin_box').bind('mouseleave', function(){
		showSigninOverlay = false;
		hideSigninFormWithDelay();
	});
	
	//mini basket overlay
	$('#header_mini_basket').bind('click mouseover', function(){
		showMinibasketOverlay = true;
		showMinibasket();
	});
	$('#header_mini_basket_icon').bind('click mouseover', function(){
		showMinibasketOverlay = true;
		showMinibasket();
	});
	$('#minibasket_container').bind('mouseleave', function(){
		showMinibasketOverlay = false;
		hideMinibasketWithDelay();
	});
	$('#minibasket_container').bind('mouseover', function(){
		showMinibasketOverlay = true;
	});
	
	//top navigation animation
	var tab, box;
	$('.header_navigation_item').bind('mouseover', function(){
		if(!$(this).hasClass('no_dropdown')){
			hideSigninForm();
			hideMinibasket();
			tab = $(this).next('.navigation_dropdowns_tabs');
			box = $(tab).next('.navigation_dropdowns');
			//hide all other tabs and boxes
			$('.navigation_dropdowns_tabs').not(tab).hide();
			$('.navigation_dropdowns').not(box).hide();
			//show this navigation tab and box
			tab.show();
			box.show();			
			showNav = true;
		}
	});
	$('.navigation_dropdowns_tabs').bind('mouseover', function(event){
		showNav = true;
	});	
	$('.navigation_dropdowns_tabs').bind('mouseleave', function(event){
		var relativeY = event.pageY - this.offsetTop;
		showNav = false
		hideNavigationWithDelay();
	});
	$('.navigation_dropdowns').bind('mouseover', function(event){
		showNav = true;
	});
	$('.navigation_dropdowns').bind('mouseleave', function(e){
			showNav = false
			hideNavigationWithDelay();
	});
	
	
	applyBasketCarousel(false);
	
	//login script
	
	$("#header_signin_form").validate();
	
//	$('#top_register_link').click(function(e){
//		e.preventDefault();
//		var tmp = $('#header_signin_form').attr('action');
//		$('#header_signin_form').attr('action', tmp+'register/');
//		$('#header_signin_form').submit();
//	});
	/*
	//left navigation accordion
	$('.left_accordion_content').hide();
	if(activeLeftMenuItem != ""){
		$('#left_navigation_content_'+activeLeftMenuItem).show();
	}
	$('.left_accordion_header').click(function(e){
		var tmp = $(this).children('a').attr('href');
		if(tmp == '#'){
			e.preventDefault();
			$('.left_accordion_content').not($(this).next('.left_accordion_content')).slideUp();
			$(this).next('.left_accordion_content').slideToggle();
		}
	});
	*/
	//select box script
	//first check if the browser is iphone or ipad and only apply this if it's not
	var Apple = {};  
	Apple.UA = navigator.userAgent;  
	Apple.Device = false;  
	Apple.Types = ["iPhone", "iPod", "iPad"];  
	for (var d = 0; d < Apple.Types.length; d++) {  
	    var t = Apple.Types[d];  
	    Apple[t] = !!Apple.UA.match(new RegExp(t, "i"));  
	    Apple.Device = Apple.Device || Apple[t];  
	} 
	if(!Apple.Device){
		$('.custom_select_box').selectmenu(
				{style: "dropdown",
					maxHeight: 150}
		);
		//apply the checkbox and radiobutton script
		$('input').customInput();
	}
	
	//footer signup box
	$('#bottom_newsletter_submit').bind("click",function(e){
		e.preventDefault();
		gender = $('#bottom_newsletter_gender option:selected').val();
		newsletterSignupFooter(gender);
	});
	//add js to form elements with default value
	$('.default_title').focus(function(){
		if($(this).val() == $(this).attr('title')){
			$(this).val('');
		}
	});
	$('.default_title').blur(function(){
		if($(this).val() == ""){
			$(this).val($(this).attr('title'));
		}
	});
//	setInterval(
//		function() {
//			if ( !is_basket_updated && !is_animation_in_progress && $('#minibasket_container').css('display')!='none' ) {
//				hideMinibasket(true);
//			}
//		},
//		1000
//	);
	
});

//delay after which top nav overlays are hidden
var delay = 1000;
var showNav = false;
var showMinibasketOverlay = false;
var showSigninOverlay = false;
var is_basket_updated = false;
var is_animation_in_progress = false;
var returnUrl = "", username = "", password = "";

function hideNavigationWithDelay(){
	window.setTimeout(hideNavigation, delay);
}
function hideNavigation(){
	if(showNav == false){
		$('.navigation_dropdowns_tabs').hide();
		$('.navigation_dropdowns').hide();
	}
}
function showMinibasket(){
	showSigninOverlay = false;
	hideSigninForm();
	hideNavigation();
	$('#minibasket_container').show();
}
function hideMinibasketWithDelay(){
	window.setTimeout(hideMinibasket, delay);
}
function hideMinibasket(){
	if(showMinibasketOverlay == false){
		$('#minibasket_container').hide();
	}
}
function showSigninForm(){
	hideNavigation();
	hideMinibasket();
	$('#signin_tab').show();
	$('#signin_box').show();
}
function hideSigninFormWithDelay(){
	window.setTimeout(hideSigninForm, delay);
}

function hideSigninForm(){
	if(showSigninOverlay == false){
		$('#signin_tab').hide();
		$('#signin_box').hide();
	}
}

function minibasketShowAfterUpdate(msg) {
	is_basket_updated = true;
	$('#minibasket_container').html(msg);
	showMinibasket();
	applyBasketCarousel(true);
	if($.browser.webkit){
		//there's a bug in webkit based browsers that shows the newly added item x times in the basket
		//this will prevent it
		hideMinibasket();
		setTimeout(
			showMinibasket,
			500
		);
	}
	setTimeout(
		hideMinibasket,
		3000
	);
}


function applyBasketCarousel(productAddShow){
//	if($('.item_amount_0').length != 1){
		$('#minibasket_container').show();

		//basket slider
		jQuery('#minibasket_slider').jcarousel({
			vertical: true,
			scroll: 1,
			buttonNextHTML: '<div id="basket_bottom_arrow" ></div>',
			buttonPrevHTML: '<div id="basket_top_arrow"></div>'
	    });
		if(productAddShow == false){
			$('#minibasket_container').hide();
		}
//	}
}

function update_basket_total(){
    $.ajax({
        type: "POST",
        url: "/shopping_bag/",
        data: "&ajax=1&act=update_basket_total",
        success: function(msg){
            $('.header_basket_value').html(msg + ")");
        }
    });
}


function update_basket_count(){
    $.ajax({
        type: "POST",
        url: "/shopping_bag/",
        data: "&ajax=1&act=update_basket_count",
        success: function(msg){
            $('.header_basket_value').html(msg);
        }
    });
}

//function that changes the images and link on hover over the colour swatches
// for the category, offer, collection and search page
function applyColourSwatchLinkChange(){
	$('.colour_swatch').mouseover(function(){
		var img = $(this).parent().parent().prevAll('.product_image').find('.product_img_i');
		var productnameLink = $(this).parent().parent().nextAll('a');
		var link = $(this).parent().parent().prevAll('.product_image').children('a');
		var prod = $(this).attr('id');
		var newUrl = $(this).children('a').attr('href');
		$(img).attr('src', imageUrl+prod+"-1.jpg");
		$(link).attr('href', newUrl); 
		$(productnameLink).attr('href', newUrl);
	});
}
var firstName, email;
//does the footer newsletter signup
function newsletterSignupFooter(gender){
	firstName = $('#bottom_newsletter_name').val();
	email = $('#bottom_newsletter_email').val();
	if(firstName == "Name"){
		//error handling
		$('#bottom_newsletter_subscribe_error').html("Please enter your name");
		$('#bottom_newsletter_subscribe_error').fadeIn('fast');
		return;
	}
	if(email == "Email"){
		$('#bottom_newsletter_subscribe_error').html( "Please enter a valid email address.");
		$('#bottom_newsletter_subscribe_error').fadeIn('fast');
		return;
	}
	if(gender == ""){
		$('#bottom_newsletter_subscribe_error').html( "Please select a gender.");
		$('#bottom_newsletter_subscribe_error').fadeIn('fast');
		return;
	}
	
	$.getJSON( "/subscribe/", 
		{first_name: firstName,
		email: email,
		gender: gender,
		act: "submit",
		ajax: true},
		function(data){
			if(data.success){
				//add success handling
				$('#bottom_newsletter_form').fadeOut('400', function(){
					$('#bottom_newsletter_thank_you').fadeIn();
				});
			}else{
				if(data.msg != ""){
					$('#bottom_newsletter_subscribe_error').html(data.msg);
					$('#bottom_newsletter_subscribe_error').fadeIn('fast');
				}
			}
	});
}
