//  Inititalises jQuery in compatibility mode
var $jq = jQuery.noConflict();

//  Common items
$jq(document).ready(function() {

    $jq('.dropDownWrap').bgiframe();
    $jq('#shoppingbag_layer').bgiframe();

    $jq('.dropDownBox').show();

    //    -- IE6 Png fix
    $jq(function(){$jq(document).pngFix();});

    $jq('.dropDownBox').hide();

    $jq('.tln').hover(
	    function () {
		    $jq(this).addClass('hover');
		    $jq('#' + $jq(this).attr('id') + ' > .dropDownWrap').show();
		    $jq('#' + $jq(this).attr('id') + ' > .dropDownWrap .dropDownBox').fadeIn('normal');
		},
        function () {
            $jq(this).removeClass('hover');
            $jq('.dropDownWrap').hide();
            $jq('.dropDownBox').hide();
        }
	);
	  
    $jq('#contact_cs').hover(
        function () {
            $jq(this).addClass('over');
        },
        function () {
            $jq(this).removeClass('over');
        }
    );
      
    //    -- Shopping bag
    $jq('#utilnav_shoppingbag').mouseleave( function() {
        if ($jq('#cart_content:visible').length > 0) {
            /*
             * We only hide the shopping bag if the mouse leaves the area when the cart content is visible.
             * This ensures that we dont hide the bag when the mouse leaves the "loading..." image.
             */
            $jq('#shoppingbag_layer').hide();
            $jq('#shoppingbag_layer').removeAttr('style');
        }
    });
    
    $jq('#utilnav_shoppingbag').mouseenter( function() {
        if ($jq('#cart_content:visible').length > 0) {
            $jq('#shoppingbag_layer').show();
            $jq('#shoppingbag_layer').attr('style', "top:45px; margin-top: auto;");
        }
    });
    
    minibag_mouseleave();
    
    //    -- Footer
    $jq('#signup_email_address').focus(function () {
        if (this.value == $jq("#signup_email_address").data("default_value")){
            $jq('#signup_email_address').val('');
        }
        return false;
    });
    
    $jq('#signup_email_address').blur(function () {
        if (this.value == ''){
            $jq('#signup_email_address').val($jq("#signup_email_address").data("default_value"));
        }
        return false;
    });
    
    $jq('#country_select').change(function () {
        window.location = 'http://' + this.value;
    });
    
    //    -- Logo
    $jq('#el_logo').click( function() {
        location.href = "/index.tmpl";    
    });

    var popup_timeout;
    var email_sign_up_popover_div = document.getElementById('email_sign_up_popover_div');

    $jq('#email_sign_up_popover_div, #title_email_sign_up_popover_div' ).mouseover(function () {
	if (popup_timeout){ clearTimeout(popup_timeout) }	
		$jq('#email_sign_up_popover_div').css('display', 'block');
    });

    $jq('#email_sign_up_popover_div, #title_email_sign_up_popover_div').mouseout(function () {
	popup_timeout = setTimeout( function(){ 
	$jq('#email_sign_up_popover_div').css('display', 'none');
	$jq('#email_sign_up_popover_error_div').css('display', 'none');
	$jq('#signup_email_address_popover').val('Email Address')
	}, 
	500);
    });



    $jq(email_sign_up_popover_div).appendTo('main_container');
});

function minibag_mouseleave() {
    $jq('#shoppingbag_qtyfilter').mouseleave( function(event) {
        event.stopPropagation();
    });
}

var email_sign_up_popover_div = document.getElementById('email_sign_up_popover_div');
var popup_timeout;
						    
function show_signup(){
	if (popup_timeout){ clearTimeout(popup_timeout) }
	email_sign_up_popover_div.style.display='block';
}
						
function hide_signup(){
	popup_timeout = setTimeout( function(){ 
		email_sign_up_popover_div.style.display='none';   
		document.getElementById('email_sign_up_popover_error_div').style.display='none';
		document.getElementById('signup_email_address_popover').value='Email Address';
		}, 
	500);
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
    	var c = ca[i];
    	while (c.charAt(0)==' ') c = c.substring(1,c.length);
    	if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function showSpecialOffers() {
    $jq('#nav_special_offers').addClass('hover');
    $jq('#nav_special_offers > .dropDownWrap').show();
    $jq('#nav_special_offers > .dropDownWrap .dropDownBox').show();
}

function hideSpecialOffers() {
    $jq('#nav_special_offers').removeClass('hover');
    $jq('#nav_special_offers > .dropDownWrap .dropDownBox').fadeOut('slow');
    setTimeout(function(){ $jq('#nav_special_offers > .dropDownWrap').hide(); }, 1500 );
    createCookie('seen_offers',1,0);
}

/*
 *   We use (window).load here instead of (document).ready, to ensure that everything has loaded on the
 *   page before we show the special offers animation (otherwise it appears to load and hide very quickly,
 *   since the images have probably not been loaded yet, especially in IE).
 */

  
$jq(window).load(function() {
    hasViewed = readCookie('seen_offers');
    if (!hasViewed){
        showSpecialOffers();
        setTimeout(function(){ hideSpecialOffers(); }, 5000 );
    }
});

