var general = {

    currentRotator: 1,

    init: function() {
        this.bubbleEvt();
		setInterval( "general.slideSwitch()", 10000 );
    },

    bubbleEvt: function() {
		
		//init fancybox on large product image (#product_image_large a)
		$("#product_image_large a").fancybox({
			'overlayOpacity': "0.5",
			'overlayColor': "#fff"
		}); 			
		
        $("*.toggle").click(function() {
            general.toggle(this);
            return false;
        });

        $("a.rotator").click(function() {
            general.topRotator(this);
            return false;
        });

        $(".product_images:not(.choose_colors) > li > a").click(function() {
            if (!$(this).hasClass("popup")) {
				this.blur();
                general.imgUrlSwap(this, true);
            } else {
                //alert(this.href);
                var picUrl = $(this)
                var newWindow = window.open(this.href, '_blank');
                newWindow.focus();

            }
            return false;
        });
		
        $(".product_images.choose_colors > li > a").fancybox({
			'overlayOpacity': "0.5",
			'overlayColor': "#fff"
		});		

        $(".tender_info_icon, .tender_info_close").click(function() {
            general.tenderInfo(this);
            return false;
        });

        $(".top_find_us a").click(function() {
            general.toggleContactSlider(this);
            return false;
        });

        //implemneted via jquery plugin jquery.equalizecols.js
        $("input[name='search']").toggleVal();
		
		
		$("#product_image_large").hover(
	      function () {
	        $(this).find("span.hover_text_holder").toggle("fast");
	      }, 
	      function () {
	        $(this).find("span.hover_text_holder").toggle("fast");

	      }
	    );

    },

    toggleContactSlider: function(e) {
        var elm = $(".contact_slider");

        elm.slideToggle("normal");
        $(".top_find_us").toggleClass("top_find_us_down");
    },

    tenderInfo: function(e) {
        var elm = $(".tender_info_placer");

        if (!elm.hasClass("visible")) {
            elm.addClass("visible");
        } else {
            elm.removeClass("visible");
        }
    },

    topRotator: function(e) {
        var container = $("#toprotator");
        var elm;
        var current = e.className.replace(/.*show-([\w-]+).*/, "$1");
        var children = $("#toprotator > .rotator-item");
        var next;
        var prev;
        var navigation = $("#toprotator-navigation > ul > li > .rotator");
        var selectedNavigation;

        if (!$(e).hasClass("prev") && !$(e).hasClass("next")) {
            elm = $("#rotatorItem-" + e.className.replace(/.*show-([\w-]+).*/, "$1"));
            children.addClass("display-none");
            elm.removeClass("display-none");
            general.currentRotator = current;
            navigation.removeClass("selected");
            $(e).addClass("selected");
        } else if ($(e).hasClass("next")) {
            next = parseInt(general.currentRotator) + 1;
            if (next <= children.length) {
                elm = $("#rotatorItem-" + next);
                children.addClass("display-none");
                elm.removeClass("display-none");
                general.currentRotator = next;
                navigation.removeClass("selected");
                selectedNavigation = $("#toprotator-navigation > ul > li > .show-" + next);
                selectedNavigation.addClass("selected");
            }
        } else if ($(e).hasClass("prev")) {
            next = parseInt(general.currentRotator) - 1;
            if (next != 0) {
                elm = $("#rotatorItem-" + next);
                children.addClass("display-none");
                elm.removeClass("display-none");
                general.currentRotator = next;
                navigation.removeClass("selected");
                selectedNavigation = $("#toprotator-navigation > ul > li > .show-" + next);
                selectedNavigation.addClass("selected");
            }
        }

        sIFR.replace(helvetica_neue, {
            selector: '.rotator-item h2',
            css: [
			  '.sIFR-root { font-size:28px; color:#ba3700; font-weight: bold; leading: -10.0; letter-spacing: -1 }'
			  , 'a { color: #ba3700; text-decoration: none; }'
			  , 'a:link { color: #ba3700; }'
			  , 'a:hover { color: #631d00; }'
			]
			, wmode: 'transparent'
        });
    },

    imgUrlSwap: function(e, toggle) {
        var newURL = e.href;
        //var newTargetURL = newURL.substring(newURL.indexOf("image=") + 6, newURL.indexOf("&"));
        var elm = $("#product_image_large a img");
        var link = $("#product_image_large a");
        var listItems = $(".product_images > li");
        var currentList = $(e).parent();
        var currentImage = $(e).children();
        //var colorHeader = document.getElementById("choose-color-header");
        var colorHeaderValue;
        var basketDropdown = $(".basket_select");
        var optionValue;

        if (toggle && newURL && elm) {
            elm.attr("src", newURL);
            link.attr("href", newURL);
            listItems.removeClass("selected");
            currentList.addClass("selected");
           
		    //if ($(currentImage).hasClass("colorchooser")) {
            //    colorHeaderValue = e.getAttribute("title")
            //    colorHeader.innerHTML = colorHeaderValue;
            //    optionValue = e.className.replace(/.*option-([\w-]+).*/, "$1");
            //    basketDropdown[0].selectedIndex = optionValue;
            //}

        }
		
		/*
		 else {
            if (!$(currentImage).hasClass("colorchooser")) {
                $(currentImage).animate({
                    top: "0px",
                    left: "0px",
                    height: "33px",
                    width: "50px",
					border: "1px solid #c6c6c6"
                }, 300);
            }
        }
        */
        
    },
	
	equalHeight: function(group) {
		tallest = 0;
		group.each(function() {
			thisHeight = $(this).height();
			if(thisHeight > tallest) {
				tallest = thisHeight;
			}
		});
		group.height(tallest);
	},		
	
    toggle: function(e) {
        var target = e.className.replace(/.*target-([\w-]+).*/, "$1");
        var elm;
        var targetClass;
        var childClass;

        if (target == "parent") {
            elm = $(e).parent();
        }

        if (target == "children") {
            elm = $(e).children();
        }

        if (target == "siblings") {
            elm = $(e).siblings();
        }

        if (target == "class") {
            targetClass = e.className.replace(/.*select-([\w-]+).*/, "$1");
            elm = $("." + targetClass + "");
        }

        if (target == "id") {
            targetClass = e.className.replace(/.*select-([\w-]+).*/, "$1");

            if ($(e).hasClass("children")) {
                elm = $("#" + targetClass + "").children();
            } else {
                elm = $("#" + targetClass + "");
            }
        }

        if (!elm.hasClass("display-none")) {
            elm.addClass("display-none");
        } else {
            elm.removeClass("display-none");
        }
    },
	
	/*** 
	    Simple jQuery Slideshow Script
	    Released by Jon Raasch (jonraasch.com) under FreeBSD license: free to use or modify, not responsible for anything, etc.  Please link out to me if you like it :)
	***/
	
	slideSwitch: function() {
	    var $active = $('#slideshow A.active');
	
	    if ( $active.length == 0 ) $active = $('#slideshow A:last');
	
	    // use this to pull the images in the order they appear in the markup
	    var $next =  $active.next().length ? $active.next()
	        : $('#slideshow A:first');
	
	    // uncomment the 3 lines below to pull the images in random order
	    
	    // var $sibs  = $active.siblings();
	    // var rndNum = Math.floor(Math.random() * $sibs.length );
	    // var $next  = $( $sibs[ rndNum ] );
	
	
	    $active.addClass('last-active');
	
	    $next.css({opacity: 0.0})
	        .addClass('active')
	        .animate({opacity: 1.0}, 1000, function() {
	            $active.removeClass('active last-active');
	        });
	}	
	
}

$(document).ready(function(){			   
	general.init();

});