/**
 * --------------------------------------------------------------------
 * jQuery-Plugin "pngFix"
 * Version: 1.1, 11.09.2007
 * by Andreas Eberhard, andreas.eberhard@gmail.com
 *                      http://jquery.andreaseberhard.de/
 *
 * Copyright (c) 2007 Andreas Eberhard
 * Licensed under GPL (http://www.opensource.org/licenses/gpl-license.php) 
 */

(function($) {
jQuery.fn.pngFix = function(settings) {
	// Settings
	settings = jQuery.extend({
		blankgif: 'blank.gif'
	}, settings);
	var ie55 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 5.5") != -1);
	var ie6 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 6.0") != -1);
	if (jQuery.browser.msie && (ie55 || ie6)) {
		//fix images with png-source
		jQuery(this).find("img[@src$=.png]").each(function() {
			jQuery(this).attr('width',jQuery(this).width());
			jQuery(this).attr('height',jQuery(this).height());
			var prevStyle = '';
			var strNewHTML = '';
			var imgId = (jQuery(this).attr('id')) ? 'id="' + jQuery(this).attr('id') + '" ' : '';
			var imgClass = (jQuery(this).attr('class')) ? 'class="' + jQuery(this).attr('class') + '" ' : '';
			var imgTitle = (jQuery(this).attr('title')) ? 'title="' + jQuery(this).attr('title') + '" ' : '';
			var imgAlt = (jQuery(this).attr('alt')) ? 'alt="' + jQuery(this).attr('alt') + '" ' : '';
			var imgAlign = (jQuery(this).attr('align')) ? 'float:' + jQuery(this).attr('align') + ';' : '';
			var imgHand = (jQuery(this).parent().attr('href')) ? 'cursor:hand;' : '';
			if (this.style.border) {
				prevStyle += 'border:'+this.style.border+';';
				this.style.border = '';
			}
			if (this.style.padding) {
				prevStyle += 'padding:'+this.style.padding+';';
				this.style.padding = '';
			}
			if (this.style.margin) {
				prevStyle += 'margin:'+this.style.margin+';';
				this.style.margin = '';
			}
			var imgStyle = (this.style.cssText);
			strNewHTML += '<span '+imgId+imgClass+imgTitle+imgAlt;
			strNewHTML += 'style="position:relative;white-space:pre-line;display:inline-block;background:transparent;'+imgAlign+imgHand;
			strNewHTML += 'width:' + jQuery(this).width() + 'px;' + 'height:' + jQuery(this).height() + 'px;';
			strNewHTML += 'filter:progid:DXImageTransform.Microsoft.AlphaImageLoader' + '(src=\'' + jQuery(this).attr('src') + '\', sizingMethod=\'scale\');';
			strNewHTML += imgStyle+'"></span>';
			if (prevStyle != ''){
				strNewHTML = '<span style="position:relative;display:inline-block;'+prevStyle+imgHand+'width:' + jQuery(this).width() + 'px;' + 'height:' + jQuery(this).height() + 'px;'+'">' + strNewHTML + '</span>';
			}
			jQuery(this).hide();
			jQuery(this).after(strNewHTML);
		});
		// fix css background pngs
		jQuery(this).find("*").each(function(){
			var bgIMG = jQuery(this).css('background-image');
			if(bgIMG.indexOf(".png")!=-1){
				var iebg = bgIMG.split('url("')[1].split('")')[0];
				jQuery(this).css('background-image', 'none');
				jQuery(this).get(0).runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + iebg + "',sizingMethod='scale')";
			}
		});		
		//fix input with png-source
		jQuery(this).find("input[@src$=.png]").each(function() {
			var bgIMG = jQuery(this).attr('src');
			jQuery(this).get(0).runtimeStyle.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader' + '(src=\'' + bgIMG + '\', sizingMethod=\'scale\');';
   		jQuery(this).attr('src', settings.blankgif)
		});	
	}	
	return jQuery;
};
})(jQuery);

/**
* hoverIntent r5 // 2007.03.27 // jQuery 1.1.2+
* <http://cherne.net/brian/resources/jquery.hoverIntent.html>
* 
* @param  f  onMouseOver function || An object with configuration options
* @param  g  onMouseOut function  || Nothing (use configuration options object)
* @author    Brian Cherne <brian@cherne.net>
*/
(function($){$.fn.hoverIntent=function(f,g){var cfg={sensitivity:7,interval:100,timeout:0};cfg=$.extend(cfg,g?{over:f,out:g}:f);var cX,cY,pX,pY;var track=function(ev){cX=ev.pageX;cY=ev.pageY;};var compare=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);if((Math.abs(pX-cX)+Math.abs(pY-cY))<cfg.sensitivity){$(ob).unbind("mousemove",track);ob.hoverIntent_s=1;return cfg.over.apply(ob,[ev]);}else{pX=cX;pY=cY;ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}};var delay=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);ob.hoverIntent_s=0;return cfg.out.apply(ob,[ev]);};var handleHover=function(e){var p=(e.type=="mouseover"?e.fromElement:e.toElement)||e.relatedTarget;while(p&&p!=this){try{p=p.parentNode;}catch(e){p=this;}}if(p==this){return false;}var ev=jQuery.extend({},e);var ob=this;if(ob.hoverIntent_t){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);}if(e.type=="mouseover"){pX=ev.pageX;pY=ev.pageY;$(ob).bind("mousemove",track);if(ob.hoverIntent_s!=1){ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}}else{$(ob).unbind("mousemove",track);if(ob.hoverIntent_s==1){ob.hoverIntent_t=setTimeout(function(){delay(ev,ob);},cfg.timeout);}}};return this.mouseover(handleHover).mouseout(handleHover);};})(jQuery);
/* Copyright (c) 2006 Brandon Aaron (http://brandonaaron.net)
 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) 
 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
 *
 * $LastChangedDate: 2007-06-19 20:25:28 -0500 (Tue, 19 Jun 2007) $
 * $Rev: 2111 $
 *
 * Version 2.1
 */
(function($){$.fn.bgIframe=$.fn.bgiframe=function(s){if($.browser.msie&&parseInt($.browser.version)<=6){s=$.extend({top:'auto',left:'auto',width:'auto',height:'auto',opacity:true,src:'javascript:false;'},s||{});var prop=function(n){return n&&n.constructor==Number?n+'px':n;},html='<iframe class="bgiframe"frameborder="0"tabindex="-1"src="'+s.src+'"'+'style="display:block;position:absolute;z-index:-1;'+(s.opacity!==false?'filter:Alpha(Opacity=\'0\');':'')+'top:'+(s.top=='auto'?'expression(((parseInt(this.parentNode.currentStyle.borderTopWidth)||0)*-1)+\'px\')':prop(s.top))+';'+'left:'+(s.left=='auto'?'expression(((parseInt(this.parentNode.currentStyle.borderLeftWidth)||0)*-1)+\'px\')':prop(s.left))+';'+'width:'+(s.width=='auto'?'expression(this.parentNode.offsetWidth+\'px\')':prop(s.width))+';'+'height:'+(s.height=='auto'?'expression(this.parentNode.offsetHeight+\'px\')':prop(s.height))+';'+'"/>';return this.each(function(){if($('> iframe.bgiframe',this).length==0)this.insertBefore(document.createElement(html),this.firstChild);});}return this;};if(!$.browser.version)$.browser.version=navigator.userAgent.toLowerCase().match(/.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/)[1];})(jQuery);



function xOffset(){
	var ie6XOffset = 0;
	if($.browser.msie && $.browser.version<7){
		ie6XOffset += ($('.header').offset().left);				
	}
	return ie6XOffset;	
}
/* tabulate */
(function($){   
 $.fn.tabulate = function(opts) {  
    var defaults = {  
		cell_selector:'.Item'
    };   
    var o = $.extend(defaults, opts);  
    return this.each(function() { 
		var selfref=$(this);
        var cells = $(o.cell_selector,selfref).css({'float':'left','display':'block'});
		var cols = Math.floor(selfref.innerWidth()/cells.outerWidth());
		if(cells.length > 0){
			cells.each(function(i){
				if(i%cols == cols-1 || i == cells.length-1){
					$(this).after($('<div class="divider" style="clear:left"></div>'));
				}
			});				
			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);

(function($){   
 $.fn.make_tabs = function(opts) {  
    var defaults = {  
		item_selector:'.info_item',
		link_selector:'.info_heading',
		content_selector:'.info_content'
    };   
    var o = $.extend(defaults, opts); 	
    return this.each(function() { 
		var selfref=$(this);
		selfref.displayHolder = $('<div class="tab_display"></div>')
		$(o.item_selector+':last',selfref).after(selfref.displayHolder);
		selfref.showItem = function(toShow){		
			$(o.item_selector,this).removeClass("tab_open").addClass("tab_closed");
			$(o.content_selector,this).css('display','none');
			$(toShow).addClass("tab_open").removeClass("tab_closed");			
			this.displayHolder.html($(o.content_selector,toShow).html());
		} 
		$(o.item_selector,selfref).each(function(i){
			var me	= this;
			$(o.link_selector, this).click(function(){selfref.showItem(me);});
		;});
		selfref.showItem($(o.item_selector,selfref)[0]);
    }); 	
 };   
})(jQuery);



/* tabulate */
(function($){   
 $.fn.makemenu = function(opts) {  
    var defaults = {  
		top_level_selector:'.level_1',
		open_image:'/assets/showall_ov.png',
		closed_image:'/assets/showall.png'
    };   
    var o = $.extend(defaults, opts);
	
	var popup_base = '<div class="popup_menu" style="position:absolute;"><p class="opener"><img src="'+o.open_image+'" alt="Show All" /></p><div class="menu_holder"><ul></ul></div></div>';   
	return this.each(function() { 
		function turnMeOn(idx){			
			top_level_links.removeClass('On').hide();			
			$('li',menu_popup).removeClass('On');	
			if(idx > -1){
				$($('li',menu_popup)[idx]).addClass('On').show();				
				var target_li = $(top_level_links[idx]);
				target_li.addClass('On').show();
				hideAll();						
				return false;				
			}
		}
	  
		function showAll(){
			if($('ul',menu_popup).css('display')=='none'){
				$('img',open_button).attr('src',o.open_image);
				$('ul',menu_popup).slideDown(350,function(){});
			}
		}
		function hideAll(){
			if($('li.On',menu_popup).length > 0){		
				$('ul',menu_popup).slideUp(250,function(){if($('.On',selfref).css('display')=='none'){$('.On',selfref).show(250);}	});							
				$('img',open_button).attr('src',o.closed_image);
			}
		}
		function setTop(){
			$('img',open_button).unbind('load',this);
			if(!topSet){			
				if($.browser.msie && $.browser.version <7){
					menu_popup.css({'top':( 0 - (open_button.outerHeight(true) ))+'px','z-index':'999'});	
					topSet = true;
				}else{					
					menu_popup.css({'top':0 - open_button.outerHeight(true)+'px'});	
				}
			}			
		}
		function setHeight(){
			if($('.On',selfref).length == 0){
				var hNow=selfref.innerHeight();
				var newH=$('ul',menu_popup).height()
				if(newH >hNow){								
					if($.browser.msie && $.browser.version <7){
						selfref.css('height', newH+'px');					
					}else{
						selfref.css('min-height', newH+'px');
					}
				}	
			}
		}
		 if($('.On',selfref).length > 0){		 
			var selfref = $(this);	
			var topSet = false;
			$('a.On',selfref).removeClass('On').parent().addClass('On');
			var top_level_links=$(o.top_level_selector+'>li',selfref);
			var active_link = -1;
			var menu_popup = $(popup_base);					
			selfref.append(menu_popup);	
			$('ul',menu_popup).append(top_level_links.clone());
			$('li ul',menu_popup).remove();
			setHeight();
			if(!isIE6){
				$('img',open_button).bind('load',setTop);
			}		
			var open_button = $('p',menu_popup);		
			$('li img',menu_popup).bind('load',setHeight);		
			if($.browser.msie && $.browser.version < 8){
				$('li',menu_popup).each(function(i){$(this).css({'position':'relative','top':i*-2+'px'})});
			}
			
			menu_popup.hoverIntent(showAll,hideAll)	
			top_level_links.each(
					function(i){
						var this_li=$(this);
						if(this_li.hasClass('On')){
							active_link=i;					
						}
						var my_link=$('a',this_li).filter(function(idx){return (idx==0)});
						var my_img = $('img',my_link);
					});			
			turnMeOn(active_link);	
			if(isIE6){
				setTop();			
			}
		}
    });   
	
	};   
})(jQuery);

(function($){   
 $.fn.makedropdowns = function(opts) { 
  var defaults = {  
		useWidth:false
    };   
    var o = $.extend(defaults, opts);
    return this.each(function() { 
		var selfref=$(this).addClass('dropdown_menu');			
		$('li ul',selfref).hide();	
		var minX=function(){return selfref.parent().offset().left;};
		var maxX = function(){return minX() + selfref.parent().outerWidth(true);};
		 //alert(minX()+':'+maxX())
		function hideChildren(me){
			me.removeClass('over');
				$('ul',me).hide();
				$('div',me).hide();
		}	
		function showChildren(me){	
			var posX = 0;
			var newW = 0;
			me.addClass('over');
			me.siblings().removeClass('over');
			newW = me.innerWidth();
			posX = me.offset().left - 10;
			var ie6XOffset = 0;
			if(isIE6){
				ie6XOffset -= ($('.header').offset().left);	
				posX += ie6XOffset ;
			}
			$('div',me).show();
			var child_list =$('ul',me).show().filter(function(idx){return (idx == 0);}).css({'position':'absolute'});			
			if(!o.useWidth && child_list.width() < newW){
				child_list.css({'width':newW+'px'});
			}
			if(posX + child_list.outerWidth() > maxX()){
				posX -= child_list.innerWidth() - me.innerWidth() -20;
			}
			child_list.css({'left': posX+'px','top':(me.position().top + me.innerHeight())+4+'px'});			
		}
		
		var ulWidth = 0;		
		$('li',selfref).filter(function(idx){return idx==0}).siblings().andSelf().addClass('top_level')
		
		$('.top_level',selfref).each(function(i){
			var this_li=$(this);			
			if(this_li.parent().html() == selfref.html()){							
				this_li.hoverIntent({sensitivity:7,interval:100,timeout:100,over:function(){showChildren(this_li)},out:function(){hideChildren(this_li)}});
			}
			if(i == 0){
				this_li.addClass('first');
			}else if(i== ($('.top_level',selfref).length-1)){
				this_li.addClass('last');
			}
			if(isIE6){
				ulWidth += this_li.outerWidth(true);
				$('ul',this_li).filter(function(idx){return(idx == 0)}).wrap('<div></div>').bgiframe() ;				
			}
			var myLinks=$('a',this_li).filter(function(idx){return (idx == 0 && (this.href==window.location.pathname || (this.href==window.location.href )))});
			
			if(myLinks.length >0){
				//alert(myLinks[0])
				this_li.addClass('On');
			}
		});	
		if(isIE6 && ulWidth>0){			
			selfref.parent().css('width',ulWidth+8+'px');			
		}
		
    });   
 };   
})(jQuery);

function positionQuickorderButton(){
	var myDiv = $('.quickorder');  
	var buttTo = $('.header_menu'); 	
	if(isIE6){		
		var newW = 0;
		$('li',buttTo).css({'display':'inline-block'},{'float':'left'}).each(function(idx){newW += $(this).outerWidth(true);});
		
		$('ul',buttTo).css('width',(newW+4)+'px');		
		
	}	
		var marginL = buttTo.offset().left -  ($('.search_options').offset().left + myDiv.outerWidth(true));
		myDiv.css('margin-left',marginL+'px');
	
}
function popup(type, pageUrl){
	if(type==1){
		info_popup(pageUrl);
	}else{
		zoomify(pageUrl)
	}
}

function info_popup(pageUrl){
	window.open(pageUrl,"info_window","width=510,height=640,toolbar=no,header=no,location=no,resizable=0,scrollbars=1");
}
function postcode_popup(){
	window.open("http://www.nzpost.co.nz/Cultures/en-NZ/OnlineTools/PostCodeFinder/","info_window","width=640,height=640,toolbar=no,header=no,location=no,resizable=0,scrollbars=1");
}
function zoomify(pageUrl){
	window.open(pageUrl,"zoomify_window","width=510,height=640,toolbar=no,header=no,location=no,resizable=0,scrollbars=0");	
}
var isIE6 = false;
$(document).ready(function(){
	//$('.Listing').tabulate();	
	isIE6 = ($.browser.msie && $.browser.version<7);
	$('.SiteMap').tabulate({cell_selector:'.sitemap_catalogue'});	
	//$('.AdditionalInfoTextHtml').make_tabs();
	$('.header_nav ul').filter(function(idx){return(idx==0);}).makedropdowns();
	if(isIE6){
		$('.catalogue_nav .nav_hierarchy').makemenu({useWidth:true, open_image:'/assets/showall_ov.gif', closed_image:'/assets/showall.gif'});      
	}else{
		$('.catalogue_nav .nav_hierarchy').makemenu({useWidth:true});
	}
	positionQuickorderButton();
	
	
;})

