/* crosssellmouseovers() controls the mouseovers in the cross sell section of the product page */
(function($){   
 $.fn.crosssellmouseovers = function() {  
    var defaults = {                
    };   
    var options = $.extend(defaults, options);  
    return this.each(function() {
        var selfref = $(this); 
        var cells=$('td',selfref);
        var items_count=cells.length;
        var items_per_row = $('td',$($('tr',selfref)[0])).length;
        var last_row_starts=(Math.floor((items_count-1)/items_per_row) * items_per_row);
        for(i=0;i<items_count;i++){
            var this_cell=$(cells[i]);
            var my_img = $('.cross_sell_image',this_cell);
            var cls="col_";
            var pointer_src="/images/icon_prod_arrowup.gif";
            if(i >= last_row_starts){
                cls="col_last_"
                pointer_src="/images/icon_prod_arrowdown.gif";
            }
            var mod = i%items_per_row;
            if(mod==0){
                cls+="left";
            }else if (mod==items_per_row-1){
                cls+="right";
            }else{
                cls+="center";
            }
           this_cell.addClass(cls);           
           this_cell.append('<div class="cross_sell_pointer" style="position:absolute;display:none;width:11px;height:11px;"><img src="'+pointer_src+'"  alt=""/></div>');
                       
        }        
        $('.cross_sell_item',selfref).each(function(){
            var item = $(this);
            var myLnk= $('.cross_sell_image a',item);
            var myImg = $('.cross_sell_image img',item);
            var myTeaser = $('.cross_sell_teaser',item);
            var myPointer = $('.cross_sell_pointer',item);
            myLnk.attr('alt','').attr('title','');
			myImg.attr('alt','').attr('title','');
            function ov(){              
                var baseL =  myLnk.offset().left;
                var baseT =  myLnk.offset().top;
                if($.browser.msie && $.browser.version <7 && $('body').outerWidth() > 984){
                    baseL -= ($('body').outerWidth() - 984)/2;
                }
                var teaser_l = baseL; 
                var teaser_t = baseT + myLnk.outerHeight()+ myPointer.outerHeight();
                
                var pointer_l = baseL + Math.floor(myPointer.outerWidth()/2);
                var pointer_t = baseT + myLnk.outerHeight()+1;
                
                if(item.hasClass('col_last_center') || item.hasClass('col_center')){
                    teaser_l -= Math.floor((myTeaser.outerWidth()-myLnk.outerWidth())/2);
                    pointer_l = baseL + Math.floor((myLnk.outerWidth() - myPointer.outerWidth())/2);
                    
                }else if(item.hasClass('col_last_right') || item.hasClass('col_right')){
                    teaser_l -= Math.floor((myTeaser.outerWidth()-myLnk.outerWidth()));
                    pointer_l = baseL + myLnk.outerWidth() - (Math.floor(myPointer.outerWidth()*1.5));
                }
                if(item.hasClass('col_last_center') || item.hasClass('col_last_right') || item.hasClass('col_last_left')){
                    teaser_t = baseT - myPointer.outerHeight() - myTeaser.outerHeight() + 1;
                    pointer_t = baseT - myPointer.outerHeight();
                }
                myTeaser.css({
                    'position':'absolute',
                    'top':teaser_t,
                    'left':teaser_l,
                    'display':'block',
                    'z-index':'99'                    
                });
                myPointer.css({
                    'position':'absolute',
                    'top':pointer_t,
                    'left':pointer_l,
                    'display':'block',
                    'z-index':'100'                     
                });
				return false;
            }
             function ou(){
                myTeaser.css('display','none');
                myPointer.css('display','none');
				return false;
            }            
            myLnk.hover(ov,ou);
        });    
    });   
 };   
})(jQuery);

var sendtobottomfunction;
/* sendtobottom() ensures that the next/previous controls are at the bottom of their container */
(function($){   
 $.fn.sendtobottom = function(opts) {  
    var defaults = {  
    };   
    var options = $.extend(defaults, opts);  
    return this.each(function() { 
        var selfref=$(this);
        var pusher=$('.next_prev_spacer img',selfref);        
        function reposition_me(v){
            pusher.css('height','1px');           
           var cols_to_match=$('.column1, .column3');
           var myCol = selfref.parent();           
            myCol.css('height','auto');
             var maxH = $('.maincontainer').innerHeight() - $('.maincontainer').offset().top - myCol.offset().top;			 
             if(myCol.outerHeight() > maxH){
                maxH = myCol.outerHeight();
             }
            var myColH = myCol.outerHeight();
            cols_to_match.each(function(){
                var colH=$(this).outerHeight();
                if(colH > maxH){
                   maxH = colH; 
                }
            });
            if(myColH<maxH){                
                 pusher.css('width','1px');
            	pusher.css('height',(maxH-myColH+1)+'px');
            }
        }
        reposition_me();
 	    if($.browser.msie){
       $('.leftcontainer,.column1,.column3').bind('resize',function(){
         reposition_me();          
       });
	   }else{
		  $('.column1 img,.column3 img').bind('load',function(){
            reposition_me();
          });
          $('.Colour a, a.more_link ').bind('click',function(){
            reposition_me();
          });  
	$('.accordion_content ').bind('change',function(){
				reposition_me();          
		    });
       }
		accordion_resized = function(v){reposition_me(v);}
    });   
 };   
})(jQuery);


runOnLoad = function (){  	
    $('.accordion').accordion({header:'.accordion_heading', selectedClass:'accordion_open',  alternatingHeading1:'accordion_heading_odd',  alternatingHeading2:'accordion_heading_even'});  
    $(".cross_sell").crosssellmouseovers();
    $('.next_prev').sendtobottom();
}

// Additional Scripts (S. Frater - 12/06/2009)

// Ajax call to add item to cart 
function AddToCart() {
    // Get vars from hidden fields
    var size = $("#AddToCartForm input[name = 'szID']").val();
    var colour = $("#AddToCartForm input[name = 'clID']").val();
    var prod = $("#AddToCartForm input[name = 'pdID']").val();
    var cat = $("#AddToCartForm input[name = 'catID']").val();
    if (isNaN(cat)) {cat = 0;}
    var quantity = $("#AddToCartForm input[name = 'qty']").val();

    if (quantity && size && colour && quantity > 0 && size >= 0 && colour >= 0) {
        $.ajax({
            type: "GET",
            url: "/default.aspx",
            cache: false,
            data: { z: "c", action: "addspecify", qs: "8", pdID: prod, catID: cat, szID: size, clID: colour, qty: quantity, addpopup: "1" },
           success: ShowAddToCartPopup
        });
    } else {
        alert("Please select your preference and quantity you require before adding this item to your cart.");
    }
    return false; // Prevent form submit
}


/*

function ShowEmailAFriendForm() {
    var eventObj = this;
    alert('in email a friend script');
    var prod = parseInt($("input[name = 'pdID']").val());
    if (prod > 0) {
        $.get(
            "/support.aspx",
            { iSAMS: "emailafriend", pdID: prod }, function(data) {
                $("#email-a-friend").html(data);
                popup(2, 'email-a-friend', eventObj);
                InitEmailAFriendForm();
            }
        );
    }
    return false;
}

function InitEmailAFriendForm() {
    $("div.email_a_friend form").submit(function() {
        $("#email-a-friend-progress").css("visibility", "visible");
        $("#email-a-friend-messages").hide();
        $.post(
        "/", $("div.email_a_friend form").serializeArray(), function(data) {
            $("#email-a-friend-progress").css("visibility", "hidden");
            $("#email-a-friend-messages").html(data);
            $("#email-a-friend-messages").slideDown();
        }
        );
        return false;
    });
}
*/

function popup(popuptype, identifier, clicked) {
    clicked.href = 'javascript:void(0)';
    if (popuptype == 1) {
        // type 1 = zoom image
        var w = dhtmlwindow.open('popupbox_zoom', 'ajax', identifier, undefined, 'width=628px,height=660px,center=1,resize=0,scrolling=0');
        $('.drag-contentarea', w).css({ 'height': 'auto', 'overflow': 'auto' });
    } else if (popuptype == 2) {
        //type 2 = special offer, write/read review
        var w = dhtmlwindow.open('popupbox_' + identifier, 'div', identifier, undefined, 'width=628px,height=600px,center=1,resize=0,scrolling=0');
        $('.popup_heading h1, .product_preview .ItemName', w).sifr({ path: '/images/', font: 'Gill_Sans_MT_Light', textAlign: 'left' });
        $('h1.products, .email_a_friend .ItemName', w).sifr({ path: '/images/', font: 'Gill_Sans_MT_Light_nospace', textAlign: 'left' });
        $(' .product_preview .ItemBrand, .email_a_friend .ItemBrand', w).sifr({ path: '/images/', font: 'Gill_Sans_MT', textAlign: 'left' });
        $('.drag-contentarea', w).css({ 'height': 'auto', 'overflow': 'auto' });
    } else if (popuptype == 3) {
        //type 3 = product preview
        var w = dhtmlwindow.open('popupbox_product', 'ajax', identifier, undefined, 'width=628px,height=660px,center=1,resize=0,scrolling=0');
        //dhtmlwindow code has been modified to add the sifr functions when the ajax has loaded
        $('.drag-contentarea', w).css({ 'height': 'auto', 'overflow': 'auto' });
    }
    return false;
}
