/* Fixing Z-index in IE 7 
$(function() {
       var zIndexNumber = 1000;
       // Put your target element(s) in the selector below!
       $("div").each(function() {
               $(this).css('zIndex', zIndexNumber);
               zIndexNumber -= 10;
       });
});
*/


/* 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(true));
		
		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");				
				});				
			}
			setHeights();	
			$('img',cells).bind('load',function(e){setHeights(e);});					
		}
    });   
 };   
})(jQuery);

// z-index fix on IE 7

//$(function() {
       //var zIndexNumber = 1000;
       // Put your target element(s) in the selector below!
       //$("div").each(function() {
       //        $(this).css('zIndex', zIndexNumber);
       //        zIndexNumber -= 10;
       //});
//});



// Simple JavaScript Rotating Banner Using jQuery
// www.mclelun.com
// Script used in the portfolio with pagination

/* homepage gallery  */

// set the defaults
var jqb_vCurrent = 0;
var jqb_vTotal = 0;
var jqb_vDuration = 6500;
var jqb_intInterval = 0;
var jqb_vGo = 1;
var jqb_vIsPause = false;
var jqb_tmp = 20;
var jqb_title;
var jqb_slide = 1;
var newlink = 1
jQuery(document).ready(function() {	
								
	jqb_vTotal = $(".jqb_slides").children().size() -1;
	
	$(".jqb_info").text($(".jqb_slide").attr("title"));	
	$(".jqb_info2").text($(".jqb_slide").attr("id"));
	$("#btn_prev").css('visibility','hidden')
	jqb_intInterval = setInterval(jqb_fnLoop, jqb_vDuration);
			
	$("#jqb_object").find(".jqb_slide").each(function(i) { 												  
		jqb_tmp = ((i - 1)*659) - ((jqb_vCurrent -1)*659);
		$(this).animate({"left": jqb_tmp+"px"}, 500);
	});
	
	
	// Added exclusively for the menu slider
	$("#menuGallery_object").find(".jqb_slide").each(function(i) { 												  
		jqb_tmp = ((i - 1)*300) - ((jqb_vCurrent -1)*300);
		$(this).animate({"left": jqb_tmp+"px"}, 500);
	});
	
	
	if($(".jqb_slide").length > 1){ // set up the pagination (1,2,3 ) buttons 
			pagination = $('<div class="pagination"><ul/></div>');
			$('.jqb_btn_next').after(pagination);
	
			$(".jqb_slide").each(function(i){ // loop through the slides andcreate a number button for each
			var count = 1+i;
			var links = '<li><a href="#">'+count+'</a></li>';
			$('ul',pagination).append(links)
		});
	}
	
	$(".pagination a").first().addClass('selected') // set the first mumber to selected by default
	
	$(".pagination a").click(function(evt) { // when a number button is clicked...
		evt.preventDefault();
		var me = $(this);	
		var current = $('.selected')	
	
		if(me.hasClass('selected')){return false} // do nothing if it is already selected
					
		$(".pagination a").removeClass('selected'); // otherwise remove the selected class from the previously select number and add it to the one that has been clicked
		 me.addClass('selected');
		
		jumpTo(me.text()); // jump to relevant slide
		})
	
	$("#btn_pauseplay").click(function() {
		if(jqb_vIsPause){
			jqb_fnChange();
			jqb_vIsPause = false;
			$("#btn_pauseplay").removeClass("jqb_btn_play");
			$("#btn_pauseplay").addClass("jqb_btn_pause");
		} else {
			clearInterval(jqb_intInterval);
			jqb_vIsPause = true;
			$("#btn_pauseplay").removeClass("jqb_btn_pause");
			$("#btn_pauseplay").addClass("jqb_btn_play");
		}
	});
	
	$("#btn_prev").click(function() {		
		jqb_vGo = -1;
		jqb_fnLoop();	
	});
		
	$("#btn_next").click(function() {		
		jqb_vGo = 1;
		jqb_fnLoop();
	});
});


function jqb_fnChange(){
	clearInterval(jqb_intInterval);
	jqb_intInterval = setInterval(jqb_fnLoop, jqb_vDuration);
	jqb_fnLoop();
}

function refreshButtons(){ // set the visibility of the next & prev buttons

	if(jqb_vCurrent == jqb_vTotal){
			$("#btn_next").css('visibility','hidden')
		}else{
			$("#btn_next").css('visibility','')
		}
		if(jqb_vCurrent == 0){
			$("#btn_prev").css('visibility','hidden')
		}else{
			$("#btn_prev").css('visibility','')
		}
		
}

function jumpTo(idx){ // jump to a different slide
	jqb_vCurrent=idx-1;
	$("#jqb_object").find(".jqb_slide").each(function(i) { 	
		jqb_tmp = ((i - 1)*659) - ((jqb_vCurrent -1)*659);
		$(this).animate({"left": jqb_tmp+"px"}, 500);
	});
	
	// Added exclusively for the menu slider
	$("#menuGallery_object").find(".jqb_slide").each(function(i) { 	
		jqb_tmp = ((i - 1)*300) - ((jqb_vCurrent -1)*300);
		$(this).animate({"left": jqb_tmp+"px"}, 500);
	});
	
	refreshButtons();
}

function jqb_fnLoop(){
	if(jqb_vGo == 1){
		jqb_vCurrent == jqb_vTotal ? jqb_vCurrent = 0 : jqb_vCurrent++;
	} else {
		jqb_vCurrent == 0 ? jqb_vCurrent = jqb_vTotal : jqb_vCurrent--;
	}

	
	$(".pagination a").removeClass('selected');
		
	$(".pagination a").each(function(idx){ 
		
		if(idx == jqb_vCurrent){$(this).addClass('selected')}
	});
		refreshButtons();
		
	$("#jqb_object").find(".jqb_slide").each(function(i) { 
		jqb_tmp = ((i - 1)*659) - ((jqb_vCurrent -1)*659);
		$(this).animate({"left": jqb_tmp+"px"}, 500);
	});
	
	// Added exclusively for the menu slider
	$("#menuGallery_object").find(".jqb_slide").each(function(i) { 
		jqb_tmp = ((i - 1)*300) - ((jqb_vCurrent -1)*300);
		$(this).animate({"left": jqb_tmp+"px"}, 500);
	});
}

/**
 * --------------------------------------------------------------------
 * 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: '/images/spacer.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 && jQuery.browser.version<7) {
		//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 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);





(function($){   
 $.fn.makedropdowns = function(opts) { 
    return this.each(function() { 
		var selfref=$(this)
		if(selfref.hasClass('dropdown_menu')){
			return;
		}
		selfref.addClass('dropdown_menu');		
		var wrapper=$('<div class="dropdown"><div class="element_wrapper"><div class="element_body"></div></div></div>');
		$('ul ul:not(ul ul ul)',selfref).wrap(wrapper);
		$('li:not(li li)',selfref).hover(function(){$(this).addClass('over')},function(){$(this).removeClass('over')});
		
    });   
 };   
})(jQuery);

(function($){
	$.fn.makeselectlist = function(opts) {
		var selfref = $(this).css({'display':'none','position':'absolute'});
		if(selfref.parent().is('.select_list')){ return; }
		var holder = $('<div class="select_list" style="position:relative;z-index:1;" />');
		var selected_text=$('<div class="selected_text" />').hover(function(){$(this).addClass('over')},function(){$(this).removeClass('over')}).bind('click',function(evt){ evt.stopPropagation(); showList()});
		selfref.wrap(holder);
		selfref.before(selected_text);
		
		var activeLink=[];
		
		var links=$('li',selfref).hover(function(){$(this).addClass('over')},function(){$(this).removeClass('over')}).each(function(){
			var myLink=$('a',this);
			if(myLink.attr('href') == window.location.href){
				activeLink=myLink;
			}
		});
		if(activeLink.length==0 && links.length>0){
			activeLink=$('a',links[0]);
		}
		if(activeLink.length>0){ 
		activeLink.parent().addClass('on');
		selected_text.html(activeLink.text())
		}
		function showList(){			
			selfref.css({'display':'block'});
			$(document).bind('click',hideList);
		}
		function hideList(){
			$(document).unbind('click',hideList);
			selfref.css({'display':'none'});
		}
	}
})(jQuery);



(function($){ 
	jQuery.fn.photo_gallery = function(opts) {var defaults={link_selector:'.gallery_thumbnails a' }; 
		var o=jQuery.extend(defaults,opts); 
		return this.each(function() { 
			var me=$(this); 
			if(me.hasClass('photo_gallery')){return;} 
			me.addClass('photo_gallery');
			function nextImage(step){
				var nextIdx=0;
				var lnks=$(o.link_selector,me).each(function(idx){if($(this).parent().hasClass('on')){nextIdx=idx+step}});	
				if(lnks.length>0){
					if(nextIdx >= lnks.length){
						nextIdx -= lnks.length;
					}
					if(nextIdx < 0){
						nextIdx = lnks.length-1;
					}
				}
				$(lnks[nextIdx]).trigger('click');
			}
			var count=$(o.link_selector,me).bind('click',function(evt){ 
				evt.preventDefault(); 
				var lnk=$(this);
				var img=$('img',this).clone(); 
				var txt=$('.caption',this.parentNode).contents().clone(); 
				img.attr('src',this.href); 
				img.bind('load',function(){$('#gallery_main_image').removeClass('loading')});
				lnk.parent().addClass('on').siblings().removeClass('on'); 
				$('#gallery_main_image img').unbind(); 
				$('#gallery_main_image').addClass('loading').empty().append(img);
				$('#gallery_image_caption').empty().append(txt); 
			}).length;
			if(count>1){
			var next_link=$('<a href="#" class="next_link">next &gt;&gt;</a>');
			next_link.bind('click',function(evt){evt.preventDefault(); nextImage(1);});
			var prev_link=$('<a href="#" class="prev_link">&lt;&lt; previous</a>');
			prev_link.bind('click',function(evt){evt.preventDefault(); nextImage(-1);});
			var lnk_div=$('<div class="gallery_links" />').append(prev_link);
			lnk_div.append(next_link);
			$('.gallery_image_display',me).append(lnk_div);
			}
		}); 
	};
})(jQuery);

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(){
	$('.SiteMap').tabulate({cell_selector:'.sitemap_catalogue'});		
	$('.header_nav').makedropdowns();
	$('.gallery_section_list ul').makeselectlist();
	$('.gallery_thumbnails').tabulate({cell_selector:'li'});
    $('.Listing').tabulate('.Item');
    $('.event_list.featured').tabulate({cell_selector:'li'});
   //$('.dropdown').wrap('<div class="ZIndex" />');
   
;})


