$(document).ready(function() {
    
    Cufon.replace('ul#header_nav, #header_links li a, .block_heading > h4, .block > h4, .block > h1, .block .block_heading a, .products_block ul li .price, #quick_contact_block h3, #quick_contact_block span, #footer h2, .our_price_display, p#old_price, p#product_reference span, #more_info_block > ul > li, .block_heading h2, .block_heading h1, #product_list li .price, .page_options h3 span.big, ul.address li.address_title, table.std thead th, #cart_voucher h4, #login_info h1, #login_info h2, #cms .rte h2, #cms .rte h1, .sitemap_block h3, .categTree h3, , #payment .icon');
    
    Cufon.replace('#bottomBar h2',{ hover: { color: '#93d7f5'}});    
    Cufon.replace('#header_user_log a, #cart_block p#cart-buttons a',{ hover: { color: '#19242f'}});
    Cufon.replace('ul#category_block > li > a',{ hover: { color: '#87939f'}});
    Cufon.replace('ul#category_block .sub ul li h2 a',{ hover: { color: '#1f2a36'}});
    Cufon.replace('.payment_module a',{ hover: { color: '#1f2a36'}});
    
    showIeInfo(); 
    
    $(function() { $("form.jqtransform").jqTransform(); });
    
    
    $("#featured-products_block_center .products_carousel").jCarouselLite({
      btnNext: "#featured-products_block_center .next",
      btnPrev: "#featured-products_block_center .prev",
      circular: true,
      visible: 5,
      speed: 1000,
      scroll: 2
    });
    
    $("#new-products_block_center .products_carousel").jCarouselLite({
      btnNext: "#new-products_block_center .next",
      btnPrev: "#new-products_block_center .prev",
      circular: true,
      visible: 5,
      speed: 1000,
      scroll: 2
    });
    
    $("#productscategory_list .products_carousel").jCarouselLite({
      btnNext: "#productscategory_list .next",
      btnPrev: "#productscategory_list .prev",
      circular: true,
      visible: 5,
      speed: 1000,
      scroll: 2
    });
    
    clickable(); 
    slideshowInit();
    searchBlock();
    dropDownInit(); 
    productImageCycle();
    myaccountMenu();     
});

function myaccountMenu(){	
  $("#my_account_menu").click( function() {
			var imgPath = $(".open_close").find("img").attr("src");
			imgPath = imgPath.substring(0,imgPath.lastIndexOf("/"));			
			//alert(imgPath);
			if ($("#openCloseIdentifier").is(":hidden")) {
				$("#my_account_menu").animate({ 
					bottom: "60px"
					}, 500 );
        $(".open_close").html('<img src="'+imgPath+'/close.png" alt="close" />');  
				$("#openCloseIdentifier").show();
			} else {
				$("#my_account_menu").animate({ 
					bottom: "-10px"
					}, 500 );
        $(".open_close").html('<img src="'+imgPath+'/open.png" alt="open" />');
				$("#openCloseIdentifier").hide();
			}
		});  
}

function showIeInfo(){
  $('#ie_info').hide();
  $('#ie_info').delay('1500').slideDown
    (
    1500, 
    function() 
      {
        $('#ie_info p').fadeIn('slow'); 
      } 
    );
   
   $("#ie_info span").click(function(){
      $('#ie_info').slideUp(1000); 
   }); 
}

function clickable() {
  $(".clickable .item").click(function(){
    	window.location=$(this).find("a.link").attr("href");return false;
  });
}

function slideshowInit() {
     $('#banner')
      .after('<div id="banner_nav">') 
      .cycle({
    		fx:     'fade', 
        speed:  500, 

        timeout: 5000,
        pager:  '#banner_nav',
        pause:   1  
        });
}

function searchBlock() { 
    $ ( '#search_block_top form' ).hide(); 

    $ ( '#search_block_buttonOpen' ).click
			(
				function ()
				{
				  $ ( '#search_block_buttonOpen' ).hide();
					$ ( '#search_block_top form' ).show
					(
						'slide',
						{
							direction: 'right',
							easing: 'easeOutCubic'
						},
						400,
						function ()
						{
							$ ( '#search_block_top form input[type=text]' ).focus ();
						}
					);
					return false;
				}
			);
			
			$ ( '#search_block_buttonClose' ).click
			(
				function ()
				{
				  $ ( '#search_block_buttonOpen' ).show();
					$ ( '#search_block_top form input[type=text]' ).blur ();
					$ ( '#search_block_top form' ).hide
					(
						'slide',
						{
							direction: 'right',
							easing: 'easeOutCubic'
						},
						300
					);
					return false;
				}
			);
}

function megaHoverOver(){
    $(this).find(".sub").stop().fadeTo('fast', 1).show(); //Find sub and fade it in
    (function($) {
        //Function to calculate total width of all ul's
        jQuery.fn.calcSubWidth = function() {
            rowWidth = 0;
            //Calculate row
            $(this).find("ul").each(function() { //for each ul...
                rowWidth += $(this).width(); //Add each ul's width together
            });
        };
    })(jQuery); 

    if ( $(this).find(".row").length > 0 ) { //If row exists...

        var biggestRow = 0;	

        $(this).find(".row").each(function() {	//for each row...
            $(this).calcSubWidth(); //Call function to calculate width of all ul's
            //Find biggest row
            if(rowWidth > biggestRow) {
                biggestRow = rowWidth;
            }
        });

        $(this).find(".sub").css({'width' :biggestRow}); //Set width
        $(this).find(".row:last").css({'margin':'0'});  //Kill last row's margin

    } else { //If row does not exist...

        $(this).calcSubWidth();  //Call function to calculate width of all ul's
        $(this).find(".sub").css({'width' : rowWidth}); //Set Width

    }
}
//On Hover Out
function megaHoverOut(){
  $(this).find(".sub").stop().fadeTo('fast', 0, function() { //Fade to 0 opactiy
      $(this).hide();  //after fading, hide it
  });
}

function dropDownInit(){ 
  var config = {
       sensitivity: 2, 
       interval: 0,
       over: megaHoverOver, 
       timeout: 100, 
       out: megaHoverOut 
  };
  
  $("ul#category_block li .sub").css({'opacity':'0'});
  $("ul#category_block li .sub").parent().addClass('sub_trigger')
  $("ul#category_block li").hoverIntent(config); 
}

function productImageCycle(){ 
  $('#image-block')
  .after('<div id="views_block"><div class="carousel"><ul>')
  .cycle({ 
    fx:     'fade', 
    speed:  'fast', 
    timeout: 0, 
    pager:  '#views_block .carousel ul', 
    pagerAnchorBuilder: function(idx, slide) { 
        var img = $(slide).children().eq(0).attr("href");
        return '<li><img src="' + img + '" alt="product" /></li>'; 
    }
     
});
  
  
  
  $("#views_block .carousel").jCarouselLite({
      btnNext: "#views_block .next",
      btnPrev: "#views_block .prev",
      circular: false,
      visible: 5,
      speed: 1000,
      scroll: 1
    });
  
  
}


