$(function() {
	
	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 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 eraseCookie(name) {
		createCookie(name,"",-1);
	}

	
	//--- Top Bar ----------------------------------------------
	var currentPromoName = 'promoNovember';
	function showTopBar() {
		$('#topBar').slideDown('fast');
	}

	function hideTopBar() {
		$('#topBar').hide();
	}
	
	// close button
	$('#topBar a.close').click(function() {
		createCookie(currentPromoName, 1);
		hideTopBar();
		return false;
	});
	
	// show top-bar if it's not closed by user once
	if (!readCookie(currentPromoName)) {
		setTimeout(showTopBar, 2000);
	}
	
	//-----------------------------------------------------------

    $('#discountForm input').focus(function() {
        this.select();
    });
    var hash = window.location.hash;
    if (hash == '#showDiscountForm') {
        $("#shDiscountForm").click();
    }

    //Gallery in product page

    $('#largeImg, a.magnifier').click(function(){
        $('#gallery li:first a').click();
    });
    $('.description_side dl:first').css({width:"170px"});
    $("#gallery a").colorbox({
        onLoad : function() {
            document.location.hash = this.href.substring(this.href.lastIndexOf("/") + 1);
        },
        onClosed : function() {
            document.location.hash = "";
        }
    });

	$("#gallerybig a").colorbox({
        onLoad : function() {
            document.location.hash = this.href.substring(this.href.lastIndexOf("/") + 1);
        },
        onClosed : function() {
            document.location.hash = "";
        }
    });
    var r = $("div a[href$=" + document.location.hash.substring(1) + "]");
    if (r.length > 0) $(r[0]).click();

    $("#largeImg>img").load(function() {
        $("#largeImg>img:hidden").fadeIn("slow");
    });

    $('#largeImg, a.magnifier').click(function(){
        $('#gallery li:first a').click();
    });

});


