jQuery(document).ready(function(){

//minicart popup

    jQuery(".quick-access .block-cart").hover (function(){
        if ((jQuery(".mini-products-list#cart-sidebar li").length == 0) || (jQuery(this).hasClass("opened"))) {
           jQuery(this).removeClass("opened");
           var height = 22;
        } else {
            jQuery(this).addClass("opened");

                var height = parseInt(jQuery(".quick-access .block-cart .block-content").height())+23;
            
        }
        jQuery(".quick-access .block-cart").stop().animate({height:String(height)+'px'} , 300);
    })

// product page, images
    function swap_image(obj) {
                jQuery('p.product-image img#image').attr('src', jQuery(obj).attr('href'));
                jQuery('p.product-image a.MagicZoomPlus').attr('href', jQuery(obj).attr('rel'));
                jQuery('p.product-image a.MagicZoomPlus').attr('title', jQuery(obj).attr('title'));
                MagicZoomPlus.update('zoom', jQuery(obj).attr('rel'), jQuery(obj).attr('href'));
    }

    jQuery('.more-views a.small_image').click( function(e){
	 e.preventDefault();
	 swap_image(this);
    });

// policy popup
    var loader = popup_loaderText + ' <br /> <img src="' + popup_loadingImage + '" class="v-middle"/>';

    var popupWindow = jQuery('<div></div>');

    var printIco = jQuery('<span class="ui-icon ui-icon-print">Print</span>');//.button();
    
    jQuery('.ui-icon-print').live('click', function() {
        popupWindow.printElement();
    });

    jQuery(".popup").live("click", function(e) {
	e.preventDefault();
        var link = jQuery(this).attr('href');

        popupWindow.html(loader);
        popupWindow.dialog({modal: true, width: 950, title: printIco, height: 500, close:function(event, ui) {jQuery(".ui-dialog").remove();}});
        popupWindow.load(link+" .col-main");
        
        return false;
    })

    jQuery('.ui-widget-overlay').live("click", function() {
	popupWindow.dialog('close');
    });    
//size chart popup

    jQuery("body").click(function(e){
        if (jQuery(e.target).parents('.size_container .size').length == 0 && jQuery(e.target).parents('.size_container').length == 0) {
		jQuery(".size_container .size").hide();
        }
    })


    jQuery(".size_container a").live('click',
	function(e){
	    e.preventDefault();
	    if (jQuery(".size_container .size").css('display') === 'none')
		jQuery(".size_container .size").show(200);
            else
		jQuery(".size_container .size").hide();
    });


    if (jQuery("#product-options-wrapper dd.d_size").length >0) {
	var sizing = jQuery(".size_container").clone();
	jQuery(".size_container").remove();
	jQuery("#product-options-wrapper dd.d_size").addClass("size_chart").append(sizing);
    }

// store selector
    jQuery("body").click(function(e){
	if (jQuery(e.target).parents('#select-store').length === 0) {
	    jQuery("#select-store ul").removeClass('active');
	}
    })


    jQuery("#select-store .title").click(function() {
	var parent = jQuery(this).parent('#select-store');
	var ul = jQuery('ul', parent);
	if (ul.hasClass('active')) {
	    ul.removeClass('active');
	} else {
	   ul.addClass('active');
	}
    })

    jQuery("#select-store a").click(function(e) {
	if (jQuery(this).hasClass('selected')) 
	    e.preventDefault();
	else
	    jQuery(this).parent('li').parent('ul').removeClass('active');
    })

// press pages

    var loc = jQuery.trim(window.location);
    jQuery(".cont-left-menu").find("a").each(function(){
	if (loc.indexOf(jQuery.trim(jQuery(this).attr('href'))) == 0 ) jQuery(this).addClass("active")
    }) 


// top menu quick fix

	jQuery("#nav li").click(function() {
		var loc = jQuery('a', this).attr('href');
		if (loc) self.location = loc;
	});

// checkout menu
	var checkout_length = jQuery("#checkoutSteps li.section").length;
	if (checkout_length > 0 && checkout_length  == 5) {
		jQuery("#checkoutSteps").addClass('loggedin');
	}


//send to friend

	jQuery(".like-buttons a.mail").click(function(e) {
		e.preventDefault();
		var link = jQuery(this).attr('href');
		var loader = popup_loaderText + ' <br /> <img src="' + popup_loadingImage + '" class="v-middle"/>';
		var popupWindow = jQuery('<div></div>');

        	popupWindow.html(loader);
	        popupWindow.dialog({modal: true, width: 355, title: '', height: 435, dialogClass:'s2f', close:function(event, ui) {jQuery(".ui-dialog").remove();}});
	        popupWindow.load(link+" .col-main .send-friend", function() {
			var productSendtofriendForm = new VarienForm('product_sendtofriend_form');
			productSendtofriendForm.submit = function() { if(this.validator.validate()) { this.form.submit(); } }.bind(productSendtofriendForm); 
		});
	        return false;
    })

})

// minicart ajax effects
jQuery(window).load(function(){
	    if (jQuery("#messages_product_view .success-msg, .mb-category-products .success-msg, .mb-search-result .success-msg").length > 0) {
            jQuery('.quick-access .block-cart').addClass("opened");
            var height = parseInt(jQuery(".quick-access .block-cart .block-content").height())+23;
            jQuery(".quick-access .block-cart").stop().animate({height:String(height)+'px'} , 300).delay(5000).animate({height:'22px'} , 300).removeClass("opened");
    }
})

