/*  
 Author: Chelsea Fenton & Daniela Kirsch
 Date  : 20090506
  
 Purpose: Functions used for the bulk quote request and shopping cart mini popups
*/

// Global variables should start with a capital letter, to show that they are global; remember that JavaScript is case sensitive
var Carturl = "go180w.pgm";
var Bulkurl = "gb100w.pgm";
var Timerpopup, Timer;
var Pageurl = "";

// TimeToLive represents the delay before closing the cart popup
var TimeToLive;


if (typeof jQuery !== "undefined") {
    jQuery(document).ready(loadcartbulk);
}


function loadcartbulk() {
//jQuery(document).ready(function () {
    getlang();
        
	// KAB 20100217 - changed type from POST to GET for performance improvement
    jQuery.ajaxSetup({
        type: "GET",
        cache: false
    });
    
    setupminis();          
//});
}

function getlang() {
    /*var ulang = window.location.href;
    var pos = ulang.lastIndexOf("/");
    Pageurl = ulang.substr(pos + 1, 6);
    Pageurl = "&page=" + Pageurl;
    ulang = ulang.substr(0, pos);
    pos = ulang.lastIndexOf("/");
    ulang = ulang.substr(pos, ulang.length - pos);*/
    //ulang = "/" + ulang + "/";
    Carturl = "/" + ulang + "/" + Carturl;
    Bulkurl = "/" + ulang + "/" + Bulkurl;
}

function closepopup() {        
    if (typeof Timerpopup !== "undefined") {
        window.clearTimeout(Timerpopup);
    }
    jQuery("#pop_up_content").html("");    
    jQuery("#pop_up").slideUp("slow");    
    jQuery("#tooltip").hide(1);    
    if ($.browser.msie && $.browser.version.substr(0, 1) < 7) {
        jQuery('#searchtype').css('display', '');
    }
}

function setupminis() {    
    getcartitems();
    getbulkitems();
    cartbulkhover();
    jQuery(".closediv").click(function () {
        //closepopup();
        
        window.clearTimeout(Timerpopup);
        // set the delay to a low value to close popup quickly
        TimeToLive = 10;
        
        // simulate mouseleave/out events instead of calling closepopup, so that the .hover events are in the proper state
        jQuery("#quotetab").mouseout();
        jQuery("#carttab").mouseout();
        jQuery("#pop_up").mouseout();
    });
}

function cartbulkhover() {

    jQuery("#pop_up").hover(
        function () {
            window.clearTimeout(Timerpopup);

            // reset the delay to a standard value to close smoothly after mouseout
            TimeToLive = 1500;
        },
        function (e) {
            window.clearTimeout(Timerpopup);
            Timerpopup = window.setTimeout("closepopup()", TimeToLive);
        }
    );

    jQuery("#carttab").hover(
        function () {        
            jQuery.ajax({
                url: Carturl,
                data: "task=getminicart" + Pageurl,
                success: function (carthtml) {
                    // clear the timer *right away*
                    window.clearTimeout(Timerpopup);
                    
                    jQuery('#headerbulk').css('display', 'none');            
                    jQuery('#headercart').css('display', '');                
                    showpopup(carthtml, "cart_style");

                    // reset the delay to a standard value to close smoothly after mouseout
                    TimeToLive = 1500;
                }
            });
        },
        function () {
            window.clearTimeout(Timerpopup);
            Timerpopup = window.setTimeout("closepopup()", TimeToLive);
        }
    
    );    
            
    jQuery("#quotetab").hover(
        function () {
            jQuery.ajax({
                url: Bulkurl,
                data: "task=getminibulk",
                success: function (bulkhtml) {
                    // clear the timer *right away*
                    window.clearTimeout(Timerpopup);
                    
                    jQuery('#headerbulk').css('display', '');            
                    jQuery('#headercart').css('display', 'none');                                                                
                    showpopup(bulkhtml, "bulkq_style");
                    if ($.browser.msie && $.browser.version.substr(0, 1) < 7) {
                        jQuery('#searchtype').css('display', 'none');
                    }

                    // reset the delay to a standard value to close smoothly after mouseout
                    TimeToLive = 1500;
                }                    
            });
        },
        function () {
            window.clearTimeout(Timerpopup);
            Timerpopup = window.setTimeout("closepopup()", TimeToLive);
        }
    );

}

function getcartitems() {
    jQuery.ajax({
        url: Carturl,
        data: "task=getcartitems" + Pageurl,
        success: function (items) {
            if (items == "0") {
                /*jQuery("#cart_items_span").css({
                    "background-image": "none",
                    "width": "0px"
                });
                jQuery("#cart_items_span a").html("");
                */
                jQuery("#carttab").html("");
                jQuery("#cartlist").css('display', 'none');
                
            } else {
                var carthtml = "";
                /*
                jQuery("#cart_items_span").css({
                    "background": "transparent url('/images/icon/Cart_light.gif') no-repeat center",
                    "width": "40px"
                });
                
                if(jQuery("#bulk_items_span").css("width") != "0px")
                {
                    jQuery("#bulk_items_span").css("margin-left", "4px");
                }
                
                jQuery("#cart_items_span a").html(items);
                */
                carthtml = "<div class=\"carttabdiv\">" +
                                "<div class=\"carttabtext\">" +
                                    "<span lang=\"Y\" key=\"cart\">Cart</span><br/>" + items;

                if (items > 1) {
                    carthtml = carthtml + " <span lang=\"Y\" key=\"items\">items</span>";
                    
                } else {
                    carthtml = carthtml + " <span lang=\"Y\" key=\"item\">item</span>";    
                }
                carthtml = carthtml + "</div></div>";
                
                
                jQuery("#carttab").html(carthtml);
                
                tran_SwitchLang(curLang, "#carttab");
                
                jQuery("#cartlist").css('display', '');
            }
        }
    });
}


function getminicart() {
    window.clearTimeout(Timerpopup);
    closepopup();
        
    jQuery.ajax({
        url: Carturl,
        data: "task=getminicart" + Pageurl,
        success: function (carthtml) {            
            // CDF 20090602 changed to use new div popup
            jQuery('#headerbulk').css('display', 'none');
            jQuery('#headercart').css('display', '');
            showpopup(carthtml, "cart_style");
            Timerpopup = window.setTimeout("closepopup()", 2000);    /*    JEL 20090729 timeout increased to 2000 from 1000
                                                                        because cart popup was disappearing too quickly
                                                                        after new items were added */
        }
    });
}

/* JEL 20090902 doesn't seem to be in use, Timer is blank
function hidecart() {
    window.clearTimeout(Timer);
    jQuery("#cart_span").hide("slow");
}
*/

function getbulkitems() {
    jQuery.ajax({
        url: Bulkurl,
        data: "task=getbulkitems",
        success: function (items) {
            if (items == "0") {
                /*
                jQuery("#bulk_items_span").css({
                    "background-image": "none",
                    "width": "0px",
                    "margin-left": "0px"
                });
                jQuery("#bulk_items_span a").html("");
                
                if(jQuery("#cart_items_span").css("width") != "0px")
                {
                    jQuery("#cart_items_span").css("margin-left", "50px");
                }
                */
                jQuery("#quotetab").html("");
                jQuery("#quotelist").css('display', 'none');
                
            } else {
                var quotehtml;
                /*
                jQuery("#bulk_items_span").css({
                    "background": "transparent url('/images/icon/Quote_light.gif') no-repeat center",
                    "width": "40px",
                    "margin-left": "4px"
                });

                if(jQuery("#cart_items_span").css("width") != "0px")
                {
                    jQuery("#cart_items_span").css("margin-left", "0px");
                }
                else
                {
                    jQuery("#bulk_items_span").css("margin-left", "50px");
                }
                
                jQuery("#bulk_items_span a").html(items);
                */
                                
                quotehtml = "<div class=\"carttabdiv\">" +
                                "<div class=\"carttabtext\">" + 
                                    "<span lang=\"Y\" key=\"quote\">Quote</span><br/>" + items;                          
                if (items > 1) {
                    quotehtml = quotehtml + " <span lang=\"Y\" key=\"items\">items</span>";
                } else {
                    quotehtml = quotehtml + " <span lang=\"Y\" key=\"item\">item</span>";    
                }
                quotehtml = quotehtml + "</div></div>";                
                
                jQuery("#quotetab").html(quotehtml);
                
                tran_SwitchLang(curLang, "#quotetab");
                
                jQuery("#quotelist").css('display', '');
            }
        }
    });
}

function getminibulk() {        
    window.clearTimeout(Timerpopup);
    closepopup();
            
    jQuery.ajax({
        url: Bulkurl,
        data: "task=getminibulk",
        success: function (bulkhtml) {
            // CDF 20090602 changed to use new div popup
            jQuery('#headerbulk').css('display', '');            
            jQuery('#headercart').css('display', 'none');            
            showpopup(bulkhtml, "bulkq_style");    
            if ($.browser.msie && $.browser.version.substr(0, 1) < 7) {
                jQuery('#searchtype').css('display', 'none');
            }                    
            Timerpopup = window.setTimeout("closepopup()", 2000);                    
        }
    });
}

function showpopup(divhtml, style) {    
    jQuery("#pop_up").removeClass();
    jQuery("#pop_up").addClass(style);
    jQuery("#pop_up_content").html(divhtml);
    
    tran_SwitchLang(curLang, "#pop_up_content");
            
    jQuery("#pop_up").slideDown("slow");    
    
    Timerpopup = window.setTimeout("closepopup()", 9000);                    
    
    minicartfnc();
}

function minicartfnc() {
    var closedivtitle = jQuery("#closedivtitle").html();
    jQuery(".closediv img").attr("title", closedivtitle);
    jQuery(".closediv img").attr("alt", closedivtitle);
    
    jQuery('input[name="checkout"]').click(function () {
        window.location.href = Carturl;
    });
    
    jQuery(".productdesc").hover(
        function (e) {
            var desc = jQuery(this).parents("tr").find(".proddesc").html();
            if (desc != "") {
                jQuery("#tooltip").css({
                    "position": "absolute",
                    "left": e.pageX + 10,
                    "top": e.pageY,
                    "background-color": "rgb(255, 255, 225)",
                    "border": "1px solid #000000",
                    "z-index": "99"
                });
                jQuery("#tooltip").html(desc).show(1);
            } else {
                jQuery("#tooltip").hide(1);
            }
        },
        function (e) {
            hidetooltip();
        }
    );
}

function hidetooltip()
{
    //window.clearTimeout(Timer);
    jQuery("#tooltip").hide(1);
}