
var filterTimer;
var browserIE6 = false;
var zoomMultiplier = 1.5;
var zoomLevel = 0;
var maxZoom = 1;
var galleryCounter = 0;
var galleryTimer;
var galleryIndex = 0;



function addGallery() {
    galleryTimer = setTimeout("showNextImg()", 3000);
    $(".show > a").css("display", "none");
    $(".show > a:eq(0)").css("display", "block");
    $(".show .controls a:eq(0)").addClass("active");
    $(".controls a").click(function() {
        clearTimeout(galleryTimer);
        anchorIndex = $(this).parent().children("a").index(this);
        $(".controls a").removeClass("active");
        $(this).addClass("active");
        $(".show > a").css("display", "none");
        $(".show > a:eq(" + anchorIndex + ")").fadeIn("slow");
        galleryTimer = setTimeout("showNextImg()", 3000);
        return false;
    });
};

function showNextImg() {
    galleryIndex += 1;
    if (galleryIndex == $(".show > a").length) {
        galleryIndex = 0;
    };
    $(".show > a").css("display", "none");
    $(".controls a").removeClass("active");
    $(".controls a:eq(" + galleryIndex + ")").addClass("active");
    $(".show > a:eq(" + galleryIndex + ")").fadeIn("slow");
    galleryTimer = setTimeout("showNextImg()", 3000);
};

function displayBox() {
    $(".favouritePopUp .favPopContent img").attr("src", imgSrc);
    $(".favouritePopUp .favPopContent img").attr("height", imgHeight);
    $(".favouritePopUp .favPopContent img").attr("width", imgWidth);
    // calculate height and width of the middle box
    imgWidth = imgWidth + 32; // allow for padding on image
    imgHeight = imgHeight + 32; // allow for padding on image
    midHeight = imgHeight + 110;
    midWidth = imgWidth + 60;
    // claculate dimensions of popup
    $(".favouritePopUp .favPopML").height(midHeight);
    $(".favouritePopUp .favPopMR").height(midHeight);
    $(".favouritePopUp .favPopMLPane").height(midHeight);
    $(".favouritePopUp .favPopMRPane").height(midHeight);
    $(".favouritePopUp .favPopContent").height(midHeight);
    $(".favouritePopUp .favPopContent").width(imgWidth);
    $(".favouritePopUp .favPopContent .infoBox").width(imgWidth);
    $(".favouritePopUp .imgPane").height(imgHeight);
    $(".favouritePopUp .imgPane").width(imgWidth);
    $(".favouritePopUp").width(midWidth + 3); // crappy little ie6 hack +3
    $(".favouritePopUp .favPopTop").width(midWidth);
    $(".favouritePopUp .favPopBot").width(midWidth);
    $(".favouritePopUp .favPopContent img").css("padding", "16px");
    // calculate absolute position of the box
    boxWidth = $(".favouritePopUp").width();
    boxHeight = $(".favouritePopUp").height();
    pageWidth = 990;
    //console.log(boxWidth);
    leftPos = (pageWidth - boxWidth) / 2;
    $(".favouritePopUp").css("left", leftPos);
    //console.log(leftPos);
    // calculate the top position of the box
    winY = $(window).height();
    winX = $(window).width();
    scrY = $(window).scrollTop();
    scrX = $(window).scrollLeft();
    //alert("winY: "+winY+", winX: "+winX+", scrY: "+scrY+", scrX: "+scrX+", boxHeight: "+boxHeight);
    if (scrY > 90) {
        //alert("positioning box");
        if (boxHeight > winY) {
            $(".favouritePopUp").css("top", (scrY - 190) + "px");
        }
        else {
            var boxTop = ((winY - boxHeight) / 2) + scrY;
            $(".favouritePopUp").css("top", (boxTop - 190) + "px");
        };

    };
    $(".favouritePopUp").fadeIn("slow");
};

function closePopUps() {
    $(".favouritePopUp").fadeOut("slow");
    if ($.browser.msie) {
        $(".favouritePopUp .favPopContent img").css("padding", "0px");
    }
    $(".sendForm").fadeOut("slow");
};

function addImagePopUp() {

    // open popup
    $(".popup").click(function() {
        imgSrc = $(this).attr("name");
        var popUpImg = new Image();
        popUpImg.onload = function() {
            imgHeight = popUpImg.height / zoomMultiplier;
            imgWidth = popUpImg.width / zoomMultiplier;
            displayBox();
        };
        popUpImg.src = imgSrc;
        return false;
    });

    // close popup
    $(".favPopContent .popClose").click(function() {
        closePopUps();
        return false;
    });

    //Press Escape event! - note: not working on IE6, maybe won't include it
    $(document).keyup(function(e) {
        if (e.keyCode == 27) {
            closePopUps();
            // close sendToFriend popup as well
            $(".sendToFriend").fadeOut("slow");
        };
    });
};


function addFiltersNav() {

    $(".finderNav ul").each(function() {
        $(this).children("li:not(:eq(0))").addClass("subItem");
        $(".subItem").css("display", "none");
        $(this).children("li:eq(0)").click(function() {
            $(this).addClass("active");
            $(this).siblings(".subItem").css("display", "block");
            $(this).siblings(".subItem").css("display", "block");
            $(this).parent("ul").siblings("ul").children(".subItem").css("display", "none");
            setTimer();
            return false;
        });
        $(this).children("li:eq(0)").hover(function() {
            $(this).addClass("hover");
        },
		function() {
		    $(this).removeClass("hover");
		});
    });

    if (filterTimer) {
        $(".subItem").hover(function() {
            clearTimeout(filterTimer);
        });
    };
    $(".subItem").mouseout(function() {
        setTimer();
    });
};

function hideFilters() {
    $(".finderNav ul .subItem").css("display", "none");
    $(".finderNav ul li").removeClass("active");
};

function setTimer() {
    clearTimeout(filterTimer);
    filterTimer = setTimeout("hideFilters()", 2000);
};

function addTransparency() {
    $(".h_menu").addClass("plug");
    $(".h_menu_page").addClass("plug");
    $(".tophead").addClass("plug");
	$(".tophead_page .logo img").addClass("plug");
    $(".tophead_page").addClass("plug");
    $(".logo img").addClass("plug");
    $(".productcat_sofa").addClass("plug");
	$(".productcat_office").addClass("plug");
    $(".productcat").addClass("plug");
	$(".productcat img").addClass("plug");
	$(".productcat .pimg").addClass("plug");
	$(".image_location img").addClass("plug");
	$(".glossymenu").addClass("plug");
	$("img").addClass("plug");
};

function styleSearchPane() {
    resultPaneCounter = 0;
    $(".searchResults .resultPane").each(function() {
        paneClass = "search" + resultPaneCounter;
        $(this).addClass(paneClass);
        resultPaneCounter = resultPaneCounter + 1;
    });
};

function addExpando() {
    $(".expandMe").toggle(function() {
        $(this).parent().next().slideDown("slow");
    },
	function() {
	    $(this).parent().next().slideUp("slow");
	});
};



function stylePanelItems() {
    var firstPanel = $(".panelItem:eq(0) > span");
    firstPanel.prepend("<div class='gradeChoice'>&nbsp;</div><div class='gradeChoice'>&nbsp;</div><div class='gradeChoice'>&nbsp;</div>");
    for (i = 0; i < $(".gradeChoice").length; i++) {
        firstPanel.children("input:eq(0)").appendTo(".gradeChoice:eq(" + i + ")");
        firstPanel.children("label:eq(0)").appendTo(".gradeChoice:eq(" + i + ")");
        firstPanel.children("p:eq(0)").appendTo(".gradeChoice:eq(" + i + ")");
    };
};

// fix to put transparency for IE6 on search panel
function fixSearchPanel() {
    $(".resultPane a").wrap("<div></div>");
};

function styleSearchResults() {
    var resultsLength = $(".categoriesFound .product").length;
    var productItems = $(".categoriesFound .product");
    var boxType = 0;
    for (var i = 0; i < resultsLength; i++) {
        boxType++;
        if (boxType > 3) {
            boxType = 0;
        }
        $(productItems).eq(i).addClass("product" + boxType).wrapInner("<div></div>");
    };
    if (browserIE6 == true) {
        $(productItems).children("div").children("a").children("img").addClass("plug");
    };
};



$(document).ready(function() {
    if ($.browser.msie) {
        if ($.browser.version == "6.0") {
            browserIE6 = true;
            addTransparency();

        }
        $(".wishBox .addButton").attr("value", "");
    }
    // this is for IE6 only, but DOM timing events will make it necessary to add for all browsers
    fixSearchPanel();
    styleSearchResults();
    addExpando();
    addGallery();
    addImagePopUp();
    styleSearchPane();
       

});

