function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

/*
		CSS Browser Selector v0.4.0 (Nov 02, 2010)
		Rafael Lima (http://rafael.adm.br)
		http://rafael.adm.br/css_browser_selector
		License: http://creativecommons.org/licenses/by/2.5/
		Contributors: http://rafael.adm.br/css_browser_selector#contributors
*/


		function css_browser_selector(u){var ua=u.toLowerCase(),is=function(t){return ua.indexOf(t)>-1},g='gecko',w='webkit',s='safari',o='opera',m='mobile',h=document.documentElement,b=[(!(/opera|webtv/i.test(ua))&&/msie\s(\d)/.test(ua))?('ie ie'+RegExp.$1):is('firefox/2')?g+' ff2':is('firefox/3.5')?g+' ff3 ff3_5':is('firefox/3.6')?g+' ff3 ff3_6':is('firefox/3')?g+' ff3':is('gecko/')?g:is('opera')?o+(/version\/(\d+)/.test(ua)?' '+o+RegExp.$1:(/opera(\s|\/)(\d+)/.test(ua)?' '+o+RegExp.$2:'')):is('konqueror')?'konqueror':is('blackberry')?m+' blackberry':is('android')?m+' android':is('chrome')?w+' chrome':is('iron')?w+' iron':is('applewebkit/')?w+' '+s+(/version\/(\d+)/.test(ua)?' '+s+RegExp.$1:''):is('mozilla/')?g:'',is('j2me')?m+' j2me':is('iphone')?m+' iphone':is('ipod')?m+' ipod':is('ipad')?m+' ipad':is('mac')?'mac':is('darwin')?'mac':is('webtv')?'webtv':is('win')?'win'+(is('windows nt 6.0')?' vista':''):is('freebsd')?'freebsd':(is('x11')||is('linux'))?'linux':'','js']; c = b.join(' '); h.className += ' '+c; return c;}; css_browser_selector(navigator.userAgent);
		$(document).ready(function () {
		    $('.col100').mouseover(function () {
		        $(this).find('.overlay').css('left', '-100px');
		        $(this).find('.locData').css('color', '#FFF');
		    }).mouseout(function () {
		        $(this).find('.overlay').css('left', '0px');
		        $(this).find('.locData').css('color', '#FFF');
		    }).mousedown(function () {
		        $(this).find('.overlay').css('left', '-200px');
		        $(this).find('.locData').css('color', '#FFF');
		    });



//		    $('.ExListPagerButton').click(function () {
//		        var page = $(this).attr('name');
//		        $('#' + page).css('display', 'block !important')
//		        $('#' + page).show();
//		        $('a[name="' + page + '"]').hide();

//		        //   alert(page);
//		        return false;
//		    })



		    // temp count
		    var count = 1;

		    // listing object 
		    $('.listingObj:odd').addClass('alternateBGColor');
		    $('.listingObjEx:even').addClass('alternateBGColor');
		    $('.cartButton').click(function () {
		        $('.cartObj').slideToggle("normal");
		    });

		    //lightbox	
		    $('#exhLogin').click(function () {
		        _buildLightbox('lbLogin');
		        return false;
		    });

		    $('#exhEditCats').click(function () {
		        _buildLightbox('lbEditCategories');
		        return true;
		    });


		});
$(window).load(function(){
	indexImg();
});	

$(window).resize(function(){
	indexImg();
});


(function ($) {
    $.fn.appear = function (fn, options) {
        var settings = $.extend({
            //arbitrary data to pass to fn
            data: undefined,
            //call fn only on the first appear?
            one: true
        }, options);
        return this.each(function () {
            var t = $(this);
            //whether the element is currently visible
            t.appeared = false;
            if (!fn) {
                //trigger the custom event
                t.trigger('appear', settings.data);
                return;
            }

            var w = $(window);
            //fires the appear event when appropriate
            var check = function () {
                //is the element hidden?
                if (!t.is(':visible')) {
                    //it became hidden
                    t.appeared = false;
                    return;
                }

                //is the element inside the visible window?
                var a = w.scrollLeft();
                var b = w.scrollTop();
                var o = t.offset();
                var x = o.left;
                var y = o.top;

                if (y + t.height() >= b &&
            y <= b + w.height() &&
            x + t.width() >= a &&
            x <= a + w.width()) {

                    //trigger the custom event
                    if (!t.appeared) t.trigger('appear', settings.data);
                } else {
                    //it scrolled out of view
                    t.appeared = false;
                }
            };

            //create a modified fn with some additional logic
            var modifiedFn = function () {
                //mark the element as visible
                t.appeared = true;
                //is this supposed to happen only once?
                if (settings.one) {
                    //remove the check
                    w.unbind('scroll', check);
                    var i = $.inArray(check, $.fn.appear.checks);
                    if (i >= 0) $.fn.appear.checks.splice(i, 1);
                }
                //trigger the original fn
                fn.apply(this, arguments);
            };

            //bind the modified fn to the element
            if (settings.one) t.one('appear', settings.data, modifiedFn);
            else t.bind('appear', settings.data, modifiedFn);
            //check whenever the window scrolls
            w.scroll(check);
            //check whenever the dom changes
            $.fn.appear.checks.push(check);
            //check now
            (check)();
        });
    };

    //keep a queue of appearance checks
    $.extend($.fn.appear, {
        checks: [],
        timeout: null,
        //process the queue
        checkAll: function () {
            var length = $.fn.appear.checks.length;
            if (length > 0) while (length--) ($.fn.appear.checks[length])();
        },

        //check the queue asynchronously
        run: function () {
            if ($.fn.appear.timeout) clearTimeout($.fn.appear.timeout);
            $.fn.appear.timeout = setTimeout($.fn.appear.checkAll, 20);
        }
    });

    //run checks when these methods are called
    $.each(['append', 'prepend', 'after', 'before', 'attr',
          'removeAttr', 'addClass', 'removeClass', 'toggleClass',
          'remove', 'css', 'show', 'hide'], function (i, n) {
              var old = $.fn[n];
              if (old) {
                  $.fn[n] = function () {
                      var r = old.apply(this, arguments);
                      $.fn.appear.run();
                      return r;
                  }
              }
          });

})(jQuery);

	
function indexImg(){
	// position index fade
	var winH = $(window).height();
	var imgH = Math.round($('.indexImage img').height());
	var imgW = Math.round($('.indexImage img').width());
	var winRatio = Math.round(winH / 10);
	var imgScale = Math.round(winRatio * 7); 
	var imgPos =  Math.round((imgH / 2) - (imgScale /2));
	$('.indexImage').css('height', imgScale+'px'); // show the visible image
	if(imgH > imgScale){
		$('.indexImage img').css('top', -imgPos+'px') // position the image
	} else if (imgH <= imgScale) {
		$('.indexImage').css('height', imgH + 'px');		
	}
	
	$('.indexFade').css('top' , $('.indexImage').height() - 150 + 'px'); //position the fade
}

// ------------------------------------------------------------------------------------
function _buildLightbox(lightboxId) {
	$('body').append('<div id="lightboxOverlay"></div>');
	var sH = $('body').height();
	var sW = $('body').width();
	$("#lightboxOverlay").css('height',sH+'px').css('display','block');
	if ( $.browser.msie) {
		if($.browser.version  <= 6){
			//$('#lightboxOverlay').bgiframe(); // js to fix select boxes in IE 6		
		}
	}
	$('#' + lightboxId).fadeIn();
	$('#' + lightboxId).css('left', (sW / 2) - ($('#' + lightboxId).width() / 2));	
	$('#lightboxOverlay').click(function() {
		_destroyLightbox()
	});
$('.closeLb, .cancelLb').click(function () {
		_destroyLightbox()
	});		

	//$('html, body').animate({scrollTop: $(".header").offset().top}, 500);
	
};


// ------------------------------------------------------------------------------------
function _destroyLightbox() {
	$('#lightboxOverlay').fadeOut(function(){
		$('#lightboxOverlay').remove();
	});
	$('.lightboxContent').fadeOut();
		lightboxStatus = false;	
};

$(window).resize(function() {
	var sH = $('body').height();
	var sW = $('body').width();
	$("#lightboxOverlay").css('height',sH+'px').css('display','block');
	if ( $.browser.msie) {
		if($.browser.version  <= 6){
			//$('#lightboxOverlay').bgiframe(); // js to fix select boxes in IE 6		
		}
	}
	$('.lightboxContent').css('left', (sW / 2) - ($('.lightboxContent').width() / 2));	
});

function addItemToCart(showKey, itemId, itemCategory, linkObj) {
    $.get('/handlers/AddToItinerary.ashx', { showKey: showKey, itemId: itemId, itemCategory: itemCategory }, function (data) {
        if (data == "Success") {
            alert('This item has sucessfully been added to your Fine Food New Zealand itinerary. \r\n\r\nUse the \'My itinerary\' button at the top of the page to view your full itinerary.');
            
        } else {
            alert("We could not add this to your itinerary at this time. Please try again later.");
        }
    });
}

function removeItemFromCart(showKey, itemId, callback) {
    $.get('/handlers/RemoveFromItinerary.ashx', { showKey: showKey, itemId: itemId }, function (data) {
        if (data == "Success") {
            callback();
        } else {
            alert("We could not remove this item from your itinerary at this time. Please try again later.");
        }
    });
}

function logLinkClick(clickType, ExId) {
    $.get('/handlers/ClickHandler.ashx?clickType=' + clickType + '&ExId=' + ExId + '', function (data) {
    });
}

function getQueryString(name) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.href);
    if (results == null)
        return "";
    else
        return results[1];
}


$(document).ready(function () {
    $("#txt_SearchString").keyup(function (event) {
        if (event.keyCode == 13) {
            $(".headerSearchBox").val(decodeURI($("#txt_SearchString").val()));
            doSiteSearch(decodeURI($("#txt_SearchString").val()));
        }
    });
  

    $(".headerSearchBox").keyup(function (event) {
        if (event.keyCode == 13) {
            $("#txt_SearchString").val(decodeURI($(".headerSearchBox").val()));
            doSiteSearch(decodeURI($(".headerSearchBox").val()));
        }
    });
	matchHeight(); 
});

function fireSearchPage() {
    $(".headerSearchBox").val(decodeURI($("#txt_SearchString").val()));
    doSiteSearch(decodeURI($("#txt_SearchString").val()));
}

function doSiteSearch(string) {
    window.location = "/home/search-results.aspx?search=" + string;
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//					
// match heights 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//		
		function matchHeight() {
		$('.jsAutoH').css('height', 'auto');
		$('.jsAutoH').each(function() {
			//find max height
			var maxHeight = 0;
			$(this).parent().find('.jsAutoH').each(function() {
				if ($(this).height() > maxHeight)
					maxHeight = $(this).height();
			});
			if ($(this).hasClass('js2Level')) {
				$(this).parent().parent().find('.jsAutoH').each(function() {
					if ($(this).height() > maxHeight)
						maxHeight = $(this).height();
				});
			}
			$(this).css('height', maxHeight + 'px');
		});
	}




