$(document).ready(function() { 
    var navName = new String(navigator.appName);
    var app = new String(navigator.appVersion);
    var version = app.substring(app.indexOf("MSIE")+5, 23);  
    var ie6 = (navName == 'Microsoft Internet Explorer' && version == '6') ? true : false;
    

    if (navName == 'Microsoft Internet Explorer' && version == '6') { 

        var arVersion = navigator.appVersion.split("MSIE")
        var version = parseFloat(arVersion[1])

        function correctPNG() // correctly handle PNG transparency in Win IE 5.5 and 6.
        {
           if ((version >= 5.5) && (document.body.filters)) 
           {
               for(var i=0; i<document.images.length; i++)
               {
	              var img = document.images[i]
	              var imgName = img.src.toUpperCase()
	              if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
	              {
		             var imgID = (img.id) ? "id='" + img.id + "' " : ""
		             var imgClass = (img.className) ? "class='" + img.className + "' " : ""
		             var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
		             var imgStyle = "display:inline-block;" + img.style.cssText 
		             var imgAttribs = img.attributes;
		             for (var j=0; j<imgAttribs.length; j++)
			         {
			            var imgAttrib = imgAttribs[j];
			            if (imgAttrib.nodeName == "align")
			            {		  
			               if (imgAttrib.nodeValue == "left") imgStyle = "float:left;" + imgStyle
			               if (imgAttrib.nodeValue == "right") imgStyle = "float:right;" + imgStyle
			               break
			            }
                     }
		             var strNewHTML = "<span " + imgID + imgClass + imgTitle
		             strNewHTML += " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
	                 strNewHTML += "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
		             strNewHTML += "(src='" + img.src + "', sizingMethod='scale');\""
		             strNewHTML += "></span>" 
		             img.outerHTML = strNewHTML
		             i = i-1
	              }
               }
           }
        }
        correctPNG();
        $("#rangeimage a").css("visibility", "visible");
    }



	
        // Onclick -
        //      1. Permanantly highlight the clicked item
        //          a. Remove the hover binding of the current event
        //      2. Loop through all of the other items and change their source to _0.png
        //          a. Re-apply hover bindings to all other items
         
        //$("#rangeimage a").each(function(){alert($(this).html());});
        if(ie6){
       $("#rangeimage span").each(function(){
                
                bindHover(this);
                $(this).click(function(){
                
                var current = this;
                    $("#rangeimage span").each(function(){
                        $(this).unbind("mouseout");
                        $(this).unbind("mouseover");                      
                        if(current != this){
                            pngOff(this);
                            bindHover(this);
                        }else{
                            pngOn(this);
                        }
                        
                    });
                  
                    
                });

        });
        }else{


           $("#rangeimage img").each(function(){
                    
                    bindHover(this);
                    $(this).click(function(){
                    
                    var current = this;
                        $("#rangeimage img").each(function(){
                            $(this).unbind("mouseout");
                            $(this).unbind("mouseover");
                            var source = $(this).attr("src");
                            
                            if(current != this){
                                    if(!$(this).attr("src").match("_off.png")){
                                        $(this).attr("src", source.replace(/_on.png/i, "_off.png"));
                                    }
                                bindHover(this);
                            }
                            
                        });
                      
                        
                    });

            });
        }
        function pngOff(el){
            var off = $(el).css("filter").replace(/_on.png/i, "_off.png");
            $(el).css("filter", off);
        }    
        
        function pngOn(el){
            var off = $(el).css("filter").replace(/_off.png/i, "_on.png");
            $(el).css("filter", off);
        }      

	

        /*
        var url = document.URL;    
        var flav = url.substring(url.indexOf('=')+1, url.length);
        $("#rangeimage img").each(function(i){
            var source = $(this).attr("src");
            var flavParent = $(this).parent().attr('href').replace(/range_(.*)aspx\?flav=/, "") 
            if (flav!=flavParent){       
                //$(this).attr("src", source.replace(/.png/i, "_0.png")); 
            }else{
                $(this).unbind("mouseout");
                $(this).unbind("mouseover");
            }
        });
        
        */
        
        var url = document.URL;    
        var flav = url.substring(url.indexOf('=')+1, url.length);
        if(!ie6){
            $("#rangeimage img").each(function(i){
                var source = $(this).attr("src");
                var flavParent = $(this).parent().attr('href').replace(/range_(.*)aspx\?flav=/, "") 
                if (flav!=flavParent){   
                    $(this).attr("src", source.replace(/_on.png/i, "_off.png")); 
                }else{
                    $(this).unbind("mouseout");
                    $(this).unbind("mouseover");
                    $(this).attr("src", source.replace(/_off.png/i, "_on.png")); 
                }
            });
        }else{
            $("#rangeimage span").each(function(i){
                var flavParent = $(this).parent().attr('href').replace(/range_(.*)aspx\?flav=/, "") 
                if (flav!=flavParent){   
                    pngOff(this);
                }else{
                    $(this).unbind("mouseout");
                    $(this).unbind("mouseover");
                    pngOn(this);
                }
            });
        }
        
        function PNGswap(myID)
        {
           var strOver  = "_on"
           var strOff = "_off"
           var oSpan = document.getElementById(myID)
           var currentAlphaImg = oSpan.filters(0).src
           if (currentAlphaImg.indexOf(strOver) != -1)
              oSpan.filters(0).src = currentAlphaImg.replace(strOver,strOff)
           else
              oSpan.filters(0).src = currentAlphaImg.replace(strOff,strOver)
        }        


        function bindHover(el){

            var id = $(el).attr("id");
            $(el).hover(function(){
                var source = $(el).attr("src");
                
                if(ie6){
                    PNGswap(id);
                }else{
                    $(el).attr("src", source.replace(/_off.png/i, "_on.png"));
                }
            }, function(){
                
                var source = $(el).attr("src");
                if(ie6){
                    PNGswap(id);    
                }else{
                    $(el).attr("src", source.replace(/_on.png/i, "_off.png"));               
                }
                
                    
            });
        }
 
	$("div#nav ul li.primary a.primary").mouseover(function() {
		// hide all subnav lists
		$("div#nav ul li.primary").removeClass("show");
		// show selected list
		$(this).parent().addClass("show");		
	});
	
	// add pipes between footer links, then remove last pipe
	$("div#footer ul li").each(function() { $(this).html($(this).html() + "|"); });
	$("div#footer ul li:last").each(function() { $(this).html($(this).html().replace(/\|/,""));	});
	
	var gallerySelect = $("body#videogallery").find("select#ctl00_c_order option");
	var galleryName = $("body#videogallery").find("input[@name=gallery]").val();
	var galleryOrder = "<ul id=\"order\">";	
	$(gallerySelect).each(function() {	    
	    var galleryOptionClass = "";
	    if ($(this).attr("selected")) {
	        galleryOptionClass = "selected";	        
	    }
	    galleryOrder += "<li class=\"order" + $(this).attr("value") + "\"><a href=\"videogallery.aspx?gallery=" + galleryName + "&ctl00$c$order=" + $(this).attr("value") + "\" class=\""+galleryOptionClass+"\">" + $(this).text() + "</a></li>";	    
	});
	galleryOrder += "</ul>";
	
	$("body#videogallery a.seeall").each(function(){
	    $(this).click(function() {
	        var galleryToShow = $("#ctl00_c_" + $(this).attr("class").replace(/seeall cta /, "").replace(/ /, ""));
	        //alert($(galleryToShow).css("display"));
	        $(galleryToShow).slideToggle("slow");
	        return false;
	    });
	});
	
	$("body#videogallery form").before(galleryOrder);
	$("body#videogallery form").remove();
	
	$("a[@rel=external]").each(function(){
	    $(this).click(function() {
	        window.open($(this).attr("href"));
	        return false;
	    });
	});	
	
	$("#rangeimage a").each(function(){
	    $(this).click(function() {
	        $(".flavourdetails").hide();
	        var flavour = $(this).attr("href");
	        $("#ctl00_c_" + flavour.split("=")[1]).show();
	        return false;
	    });
	});	
		
});