/**
 * SWFForceSize v1.0: Flash container size limiter for SWFObject - http://blog.pixelbreaker.com/
 *
 * SWFForceSize is (c) 2006 Gabriel Bucknall and is released under the MIT License:
 * http://www.opensource.org/licenses/mit-license.php
 *
 * Dependencies:
 * SWFObject v2.0 - (c) 2006 Geoff Stearns.
 * http://blog.deconcept.com/swfobject/
*/

(function($){   
 $.fn.matchWindowSize = function(opts) {  
    var defaults = {  
		minHeight:580,
		minWidth:980
    };   
    var o = $.extend(defaults, opts);
	function resizeMe(toSize){
		var $window = $(window);
		var w = $window.width() < o.minWidth? o.minWidth+"px" : "100%";
 		var h = $window.height() < o.minHeight? o.minHeight+"px" : "100%";
		/*
		 for IE on PC, turn off the disabled scrollbar
		 on the right when there's no content to scroll
		 */
		 if( document.all )
		 {
		 if ( (document.body.scroll = ( w!="100%" || h!="100%" )? "auto" : "no") == "auto")
		 {
		 document.body.style.overflow = "auto";
		 }
		 }
		toSize.css({'width':w,'height':h})
	}
    return this.each(function() { 
		var selfref=$(this).css({'position':'absolute','top':0,'left':0,'width':'100%','height':'100%'});
		resizeMe(selfref);
		$(window).bind("resize",function(){resizeMe(selfref);})
     
    });   
 };   
})(jQuery);

/*
 * jQuery ifixpng plugin
 * (previously known as pngfix)
 * Version 2.0  (04/11/2007)
 * @requires jQuery v1.1.3 or above
 *
 * Examples at: http://jquery.khurshid.com
 * Copyright (c) 2007 Kush M.
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php 
 * http://www.gnu.org/licenses/gpl.html
 */
 
(function($) {
	$.ifixpng = function(customPixel) {$.ifixpng.pixel = customPixel;};
	$.ifixpng.getPixel = function() {
		return $.ifixpng.pixel || '/images/spacer.gif';
	};
	var hack = {
		ltie7  : $.browser.msie && $.browser.version < 7,
		filter : function(src) {
			return "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true,sizingMethod=crop,src='"+src+"')";
		}
	}; 
	$.fn.ifixpng = hack.ltie7 ? function() {
    	return this.each(function() {
			var $$ = $(this);
			var base = $('base').attr('href'); // need to use this in case you are using rewriting urls
			if ($$.is('img') || $$.is('input')) { // hack image tags present in dom
				if ($$.attr('src')) {
					if ($$.attr('src').match(/.*\.png([?].*)?$/i)) { // make sure it is png image
						// use source tag value if set 
						var source = (base && $$.attr('src').substring(0,1)!='/') ? base + $$.attr('src') : $$.attr('src');
						// apply filter
						$$.css({filter:hack.filter(source), width:$$.width(), height:$$.height()})
						  .attr({src:$.ifixpng.getPixel()})
						  .positionFix();
					}
				}
			} else { 
			// hack png css properties present inside css
				//var image = $$.css('backgroundImage');
				//if (image.match(/^url\(["']?(.*\.png([?].*)?)["']?\)$/i)) {
				//	image = RegExp.$1;
				//	$$.css({backgroundImage:'none', filter:hack.filter(image)})
				//	  .children().children().positionFix();
				//}
			}
		});
	} : function() { return this; };
	
	 
	$.fn.iunfixpng = hack.ltie7 ? function() {
    	return this.each(function() {
			var $$ = $(this);
			var src = $$.css('filter');
			if (src.match(/src=["']?(.*\.png([?].*)?)["']?/i)) { // get img source from filter
				src = RegExp.$1;
				if ($$.is('img') || $$.is('input')) {
					$$.attr({src:src}).css({filter:''});
				} else {
					$$.css({filter:'', background:'url('+src+')'});
				}
			}
		});
	} : function() { return this; };	
	
	$.fn.positionFix = function() {
		return this.each(function() {
			var $$ = $(this);
			var position = $$.css('position');
			if (position != 'absolute' && position != 'relative') {
				$$.css({position:'relative'});
			}
		});
	};

})(jQuery);

(function($){   
 $.fn.pngFix = function(){
	if($.browser.msie && $.browser.version < 7){
	 	return this.each(function() {
			$('img[src$=.png], input[type="image"][src$=.png]',this).ifixpng();
		})
	}else{
		return this;
	}
 }
})(jQuery);


function trim(str){
	while(str.length >0 && str.substr(0,1)==" "){
		str=str.substr(1);
	}
	while(str.length >0 && str.substr(str.length-1,1)==" "){
		str=str.substr(0,str.length-1);
	}
	return str;
}

/* tabulate */
(function($){   
 $.fn.tabulate = function(opts) {  
    var defaults = {  
		cell_selector:'.Item',
		float:'left',
		divider_element:'<div class="divider" ></div>'
    };   
    var o = $.extend(defaults, opts);  
    return this.each(function() { 
		var selfref=$(this);
        var cells = $(o.cell_selector,selfref).css({'float':o.float,'display':'block'});
		var cols = Math.floor(selfref.innerWidth()/cells.outerWidth(true));
		if(cells.length > 0){
			cells.each(function(i){
				if(i%cols == cols-1 || i == cells.length-1){
					var me=$(this);
					if(me.next('.divider').length ==0){
						var d=$(o.divider_element);
						d.css('clear',o.float);
						$(this).after(d);
					}
				}
			});				
			var heights=[];
			function setHeights(e){
				cells.each(function(i){
					var myRow=Math.floor(i/cols);
					$(this).css("height","auto");
					var myH = $(this).height();
					if(heights[myRow]==undefined || heights[myRow]==null || heights[myRow] < myH){
						heights[myRow]=myH;
					}
				});	
				cells.each(function(i){
					var myRow=Math.floor(i/cols);
					 $(this).css("height",heights[myRow]+"px");				
				});				
			}
			$('img',cells).bind('load',function(e){setHeights(e);});
			setHeights();			
		}
    });   
 };   
})(jQuery);

/* addOverlayBorder */
(function($){   
 $.fn.addOverlayBorder = function(opts) {  
    var defaults = {  
		borderThickness:13,
		borderColour:'#FFFFFF',
		borderOpacity:0.5
    };   
    var o = $.extend(defaults, opts);  
    return this.each(function() { 
		var selfref=$(this).css('position','relative');
		var overlay=$('<div style="border: '+o.borderThickness+'px solid '+o.borderColour+'; position:absolute; top:0; left:0;"></div>');
		overlay.css('opacity',o.borderOpacity)
		overlay.css('height',(selfref.contents().height() - 2* o.borderThickness)+'px');
		overlay.css('width',(selfref.contents().width() - 2* o.borderThickness)+'px')
       selfref.append(overlay);
    });   
 };   
})(jQuery);

(function($){   
 $.fn.expandingMenu = function(opts) {   
	function openMe(toOpen){
		$(toOpen).addClass('On').siblings().removeClass('On');
	}	
    return this.each(function() { 
		var me=this;		
		if($('ul',me).length >0){
			$('>a',me).attr('href','#').click(function (){openMe(me)});	
		}
    });   
 };   
})(jQuery);

function getNav(){
	var n='<nav>'
	function makeLink(el){
		var me=$(el);		
		var l='<link href="'+me.attr('href')+'" target="'+me.attr('target')+'" text="'
		var myImg=$('img',me);
		var myTxt='';
		if(myImg.length>0){
			myTxt+=myImg.attr('alt');
		}else{
			myTxt+=me.text();
		}
		if(myTxt =='home'){
			l='';
		}else{
			l+=myTxt+'" />'
		}
		return l
	}
	$('.header_nav li a').each(function(){		
		n+= makeLink(this);
	});
	$('.secondary_header_nav li a').each(function(){
		n+= makeLink(this);
	})
	n+='</nav>';
	return n;
}

function addFlashNav(flObj){
	document.getElementById('splashpage').addNav(getNav());
}

(function($){
 $.fn.makeViewer = function(opts) {
 var defaults = {
 borderThickness:13,
 borderColour:'#FFFFFF',
 borderOpacity:0.5
 };
 var o = $.extend(defaults, opts);
 if($('.CatalogueDetails').contents().length==0){
	$('.CatalogueDetails_Block').remove(); 
 }
 return this.each(function() {
 var selfref=$(this).css('position','relative');
 var items=$('.Item',selfref);

 if(items.length>0){
 selfref.addClass('ViewerAdded');
 var viewer=$('<div class="Viewer"></div>');
 selfref.before(viewer);
 if($.browser.msie && $.browser.version>=8){
  viewer.append($('<div class="ViewerIE8"></div>'))
   viewer = $('.ViewerIE8', viewer)
 }
 var backButton=$('<div class="ViewerBack"></div>');
 var nextButton=$('<div class="ViewerNext"></div>');
 viewer.append(backButton);
 viewer.append(nextButton);
 var imageHolder = $('<div class="ViewerImage"></div>');
 viewer.append(imageHolder);
 var overlay = $('<div class="ViewerOverlay"><div class="OverlayContent"></div></div>');
 viewer.append(overlay);
 
 var textHolder = $('<div class="ViewerText"></div>');
 viewer.append(textHolder);
 var title = $('<h3 class="ViewerTitle"></h3>');
 textHolder.append(title);
 var description=$('<div class="ViewerDescription"></div>');
 textHolder.append(description);
 var showing =0;
 function showItem(idx){
 var item=$(items[idx]); 
 showing = idx;
 viewer.css('opacity','0');
 imageHolder.html($('.Image a img', item).clone());
 title.html($('.ItemName', item).text());
 description.html($('.Offer > a', item).contents().clone());
 if(showing>0){
 backButton.css('display','');
 }else{
 backButton.css('display','none');
 }
 if(showing+1<items.length){
 nextButton.css('display','');
 }else{
 nextButton.css('display','none');
 }
 viewer.animate({opacity:1},750,function(){viewer.css({opacity:''})});
 return false;
 }
 function showNext(){
 var n=showing+1;
 if(!(n<items.length)){
 n=items.length-1;
 }
 return showItem(n);
 }
 function showBack(){
 var n=showing-1;
 if(n<0){
 n=0;
 }
 return showItem(n);
 }
 nextButton.click(function(){showNext();return false;});
 backButton.click(function(){showBack();return false;});
 items.each(function(idx){
 var me=$(this);	 
 $('a',this).attr('href','#').click(function(){showItem(idx);return false;});
 me.attr('title',$('.ItemName',me).text())
 });
 showItem(0);

 }
 });
 };
})(jQuery); 
function fixEstarLink(){
	var e=$('.estar');
	var contentsHeight=0
	$('body>div').each(function(){contentsHeight+=$(this).outerHeight(true)})
	//alert('t:'+e.offset().top+' h:'+e.outerHeight(true)+' wh:'+$(window).height())
	if(contentsHeight < $(window).height()){
		if($.browser.msie && $.browser.version<7){
			e.css({position:'absolute',top:($(window).height()-e.outerHeight(true))+'px'})
		}else{
			e.css({position:'fixed',bottom:'0px',width:'100%'})
		}
	}else{
	e.css({position:'relative',bottom:'',right:'',top:'',left:'',width:'auto'})	
	}
}
$(document).ready(function(){
	// Make the catalogue and search listings behave like a table by
	// matching the heights of the cells in each row and adding a clear:left
	// div at the end of each row
	//$('.Listing').tabulate({float:'right'});
	if($('.ProductDetails').length > 0){
		window.location='/default.aspx';
	}

	// Do the same for the catalogues in the sitemap	
	$('.SiteMap').tabulate({cell_selector:'.sitemap_catalogue'});

	//$('.nav_hierarchy h2:contains("Catalogues")').html('redesign').sifr({path:'/assets/',font:'HelveticaNeue',textAlign: 'left'});
	
	// Sifr font
	//$('.nav_hierarchy h2' );
	
	$('.hero_shot').addOverlayBorder();
	$('.nav_hierarchy > ul > li > a > img').parent().each(function(){
		var me=$(this);
		var myTextParts=me.attr('href').split('/');
		var myText=myTextParts[myTextParts.length-1].replace(/_/g,' ').replace('.htm','')
		me.html(myText)
	})
	$('.nav_hierarchy > ul > li').expandingMenu();
	
	$('.Listing').makeViewer();
	
	if($.browser.msie && $.browser.version<7){
		var maxW=$('.header').innerWidth()-$('.header_logo').outerWidth(true);
		var ulW=0;
		$('.header_nav li').each(function(){ulW+=$(this).outerWidth(true)});
		if(ulW>maxW){
			ulW=maxW;
		}
		$('.header_nav').css('width',ulW+'px');
		ulW=0;
		$('.header_content_bottom li').each(function(){ulW+=$(this).outerWidth(true)});
		if(ulW>maxW){
			ulW=maxW;
		}
		$('.header_content_bottom').css('width',ulW+'px');	
		$('.contact_heading').parent().css('margin-bottom','0')
		$('div, p').each(function(){var me=$(this);if(me.css('position')=='relative'){me.css('position','absolute');me.css('position','relative');}})
	}
	if($('.contact_heading').length>0){
		$('.contact_heading').remove();
		$('.nav_hierarchy h2:first').html('<img src="/assets/h_contacts.gif" />');
	}
	if($('body.hidden').length ==0){
	fixEstarLink();
	$(window).bind('resize',function(){fixEstarLink()})
	}
;})
