$(document).ready(function(){
    
        //check if chatbutton is offline, if so hide
    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
	if(showNavDropdown){
		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();
	}
	
//	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(){
	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);
	});
}

//javascript pagination
/*
var currentPaginationPage = 1;
function setupPaginationPages(){
	//initial setup
	for(i=2; i <= pageAmount; i++){
		$('#page_number_'+i).hide();
	}
	$('.category_navigation').show();
	
	//page number links
	$('.pagination_page').click(function(e){
		e.preventDefault();
		$('.pagination_page').not(this).removeClass('current');
		$(this).addClass('current');
		var page = $(this).html();
		$('#page_number_'+currentPaginationPage).hide();
		$('#page_number_'+page).show();
		currentPaginationPage = page;
	});
	$('#pagination_next_button').click(function(e){
		e.preventDefault();
		if(currentPaginationPage == 1){
			$('#pagination_prev_button').show();
		}
		page = currentPaginationPage + 1;
		$('.pagination_page').not(this).removeClass('current');
		$('#page_link_number_'+page).addClass('current');
		$('#page_number_'+currentPaginationPage).hide();
		$('#page_number_'+page).show();
		currentPaginationPage = page;
		if(page == pageAmount){
			$('#pagination_next_button').hide();
		}
	});
	$('#pagination_prev_button').click(function(e){
		e.preventDefault();
		if(currentPaginationPage == pageAmount){
			$('#pagination_next_button').show();
		}
		page = currentPaginationPage - 1;
		$('.pagination_page').not(this).removeClass('current');
		$('#page_link_number_'+page).addClass('current');
		$('#page_number_'+currentPaginationPage).hide();
		$('#page_number_'+page).show();
		currentPaginationPage = page;
		if(page == 1){
			$('#pagination_prev_button').hide();
		}
	});
	$('#view_all_pagination').click(function(e){
		e.preventDefault();
		for(i=1; i <= pageAmount; i++){
			$('#page_number_'+i).show();
		}
		$('.category_navigation').fadeOut();
		
	});
}
*/
