var acts_total = 0;
var act_cols = 5;
var addallacts = 1;
var pagetot=30;
var firstup = true;

//setInterval("killwindow()",200);
function killwindow() {
//	if (frameStore) {
//		if ($("#fancy_close").css('display') !='none') {
			$("#content").css("z-index", 0);
			$(".trip_planner").css({'top' : '40%', 'position' : 'absolute', 'bottom' : 'auto', 'z-index' : 10000})
			$("#view").click();
			$(".trip_planner").expose({'maskId' : 'customExposeMask', 'onClose' : function(){
				if ($(".trip_planner").css('bottom').indexOf('0') != 0) $(".trip_planner").css({'top' : 'auto', 'position' : 'fixed', 'bottom' : '0', 'z-index' : 10000});
				$("#view").click();
			}});
			$('ul.tp_tabs li a.1').click();
			top.startPlan();
			$("#fancy_close").click();
//		}
//	}
}

jQuery.fn.swap = function(b) {
    b = jQuery(b)[0];
    var a = this[0],
        a2 = a.cloneNode(true),
        b2 = b.cloneNode(true),
        stack = this;
    a.parentNode.replaceChild(b2, a);
    b.parentNode.replaceChild(a2, b);
    stack[0] = a2;
    return this.pushStack( stack );
};

function showForgotSignOn() {
	$('div#divFrameLogin').swap('div#divFramePasswordLookup');
//	$('div#divFrameLogin').fadeOut(10);
	$('div#divFrameLogin').slideUp(500);
	$('div#divFramePasswordLookup').fadeIn(500);
	$('a#wucSignOn_hlForgotSignOn').click(showForgotSignOn);
	$('a#wucSignOn_hlBackToLogin').click(showLogin);
	return false;
}
function showLogin() {	
	$('div#divFrameLogin').swap('div#divFramePasswordLookup');
	$('div#divFramePasswordLookup').slideUp(500);
//	$('div#divFramePasswordLookup').fadeOut(10);
	$('div#divFrameLogin').fadeIn(500);	
	$('a#wucSignOn_hlForgotSignOn').click(showForgotSignOn);
	$('a#wucSignOn_hlBackToLogin').click(showLogin);
	return false;
}

function getQuerystring(key)
{
  key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regex = new RegExp("[\\?&]"+key+"=([^&#]*)");
  var qs = regex.exec(window.location.href);
  if(qs == null)
    return '';
  else
    return qs[1];
}

jQuery.fn.extend({
  highlight: function(newColor, originalColor) {
    newColor = '#cfc7c0';
    if (! originalColor) {
      var coloredElem = this;
      while (!originalColor && originalColor != 'transparent' && coloredElem) {
        originalColor = coloredElem[0].originalColor || coloredElem.css('background-color');
        coloredElem[0].originalColor = originalColor;
        coloredElem = coloredElem.parent();
      }
      originalColor = (originalColor && originalColor != 'transparent') ? originalColor : '#f9f2ea';
    }
    return this.css({ backgroundColor: newColor }).animate({ backgroundColor: originalColor }, 700);
  }
});

function firesibbox(x) {
	box = $(x).parent().find('input')
	box.attr('checked', box.attr('checked')?'':'checked');
	box.click(); 
	return false;
}

 function is_array(input){
   return typeof(input)=='object'&&(input instanceof Array);
 }

function toggleboxes(t) {

}

function toTimestamp(strDate){
 var datum = Date.parse(strDate);
 return datum/1000;
}


function filter_acts_new(link) {
	id = $(link).attr('value');
	showme = $(link).attr('checked')?0:1;
	filter = "filter_" + $(link).parents('form').attr('name');
	if (filter == 'filter_categories1') filter = 'filter_categories';
	if ( $(link).parents('form').attr('name') == 'categories1' ) {
		els = $("#activities_list_store > li:not([" + filter + "*=-"+id+"-])");	
		if (showme ==1) {
			els.removeClass('selffiltered'+id);
		} else {
			els.addClass('selffiltered'+id);	
		}
	} else {
	    els = $("#activities_list_store > li[" + filter + "*=-"+id+"-]")
		if (showme ==1) {
			els.removeClass('selfaded'+id);
		} else {
			els.addClass('selfaded'+id);	
		}
	}
	sfs = $("#activities_list_store > li[class*=selffiltered]");
	sas = $("#activities_list_store > li[class*=selfaded]");
	fcount = (sfs.length > 0) ? 1 : 0;
	acount = (sas.length > 0) ? 1 : 0;
	switch(acount + "" + fcount){
	  case '11':
	    acts = $("#activities_list_store > li[class*=selfaded]:not([class*=selffiltered])");
		break;
	  case '10':
	    acts = sas;
		break;
	  case '01':
	    acts = $("#activities_list_store > li:not([class*=selffiltered])");
		break;
	  case '00':
	    acts = sas;
	}
	
	$(".items_wrap").html('');
	repaginate(acts);
	if (firstup == false) addpagination();
	if (showme ==1) {
		out_r = location.hash.split($(link).attr('alt'));
		out_s = out_r.join('');
		out_s = out_s.replace(/\/\//, "/");
		out_s = out_s.replace(/\/$/, "");
		out_s = out_s.replace(/^\//, "");
		location.hash = out_s;
	} else {
		if (location.hash.indexOf($(link).attr('alt')) == -1) {
			out_s = location.hash + "/" + $(link).attr('alt');
			out_s = out_s.replace(/^\//, "");
			location.hash = out_s;
		}
	}
	return false;
}

function filter_acts(link) {
	id = $(link).attr('value');
	showme = $(link).attr('checked')?1:0;
	filter = "filter_" + $(link).parents('form').attr('name');
    els = $("#activities_list_store > li[" + filter + "*=-"+id+"-]")
	if (showme ==1) {
		els.removeClass('selfaded');
	} else {
		els.addClass('selfaded');	
	}
	acts = $("#activities_list_store > li:not([class^='selfaded'])");
	$(".items_wrap").html('');
	repaginate(acts);
	addpagination();
	return false;
}

function repaginate(acts) {
	act_pages=1;
	$(".activities, .act_nav").remove();
	$("#gridwrap").append($("<div class='activities'><div class='items_wrap'></div></div>"));
	$("#gridwrap").append($("<div class='act_nav'><span class='forward'></span><p class='paginpara'>Page <span class='navi'></span>&nbsp;of&nbsp;<span class='items_num'></span></p><span class='back'></span></div>"));
	out = $("<ul class='activities_list'></ul>");
	pagetot = 30;
	if (acts.first().hasClass('shopify')) pagetot = 8;
	acts.each(function(index){
		if (index%pagetot==0 && index!=0) {
			out = $("<div class='item'></div>").append(out);
			$(".items_wrap").append(out);
			out = $("<ul class='activities_list'></ul>");
		}
		addme = $(this).clone();
		if (index%act_cols==0) addme.addClass('first');
		out.append(addme);
	})
	out = $("<div class='item'></div>").append(out);
	$(".items_wrap").append(out);
	if ($(".explore .total").length) $(".explore .total").html(acts.length + "/" + acts_total).highlight();
	
	$(".child_class_activity .activities_list li a, .child_class_spa_service .activities_list li a").fancybox({
		frameWidth: 705,
		frameHeight: 505,
		hideOnContentClick: false
	});

	$('.activities_list li').hover(function(){
		$(this).find('.description').animate({left:'0'},{queue:false,duration:400});
	}, function(){
		$(this).find('.description').animate({left:'-182px'},{queue:false,duration:400});
	});
	
}

function addpagination() {
	if ($(".center_module div.activities li").length) {
		$(".paginpara").html("Page <span class='navi'></span>&nbsp;of&nbsp;<span class='items_num'></span>");
	
		$(".center_module div.activities").scrollable({ 
				vertical: false,
				clickable: false,
				size: 1,
				next: ".forward",
				prev: ".back",
				speed:	550,
				loop: true
		}).navigator();
	
		var items_count = 0;
		$(".navi a").each(function(){
			items_count++;
			$(this).html(items_count);	
		});
		$(".items_num").html(items_count);
	}
}

Array.prototype.unique =
  function() {
    var a = [];
    var l = this.length;
    for(var i=0; i<l; i++) {
      for(var j=i+1; j<l; j++) {
        // If this[i] is found later in the array
        if (this[i] === this[j])
          j = ++i;
      }
      a.push(this[i]);
    }
    return a;
  };

Array.prototype.intersect =
  function() {
    if (!arguments.length)
      return [];
    var a1 = this;
    var a = a2 = null;
    var n = 0;
    while(n < arguments.length) {
      a = [];
      a2 = arguments[n];
      var l = a1.length;
      var l2 = a2.length;
      for(var i=0; i<l; i++) {
        for(var j=0; j<l2; j++) {
          if (a1[i] === a2[j])
            a.push(a1[i]);
        }
      }
      a1 = a;
      n++;
    }
    return a.unique();
  };




$(document).ready(function() {
	
	$('a#wucSignOn_hlForgotSignOn').click(showForgotSignOn);

	$('a#wucSignOn_hlBackToLogin').click(showLogin);

	if (location.hash == '#password') {$('a#wucSignOn_hlForgotSignOn').click();}

	if ($('#r_col div').length) act_cols = 4;

	var acts = $('#activities_list_store > li');
	acts_total = acts.length;

	if (acts.length) {
		processed = false;
		if (firstup) {
			hash_r=document.location.href.split('#');
			if (hash_r.length > 1) {
				hash_r = hash_r[1].split('/');
				for (i in hash_r) {
					preset = $("[alt='"+hash_r[i]+"']");
					if (preset.length) {
						preset.attr('checked', 1);
						processed = true;
						filter_acts_new(preset);
					}
				}
			}
			firstup = false;
		}
		if (addallacts && processed == false) repaginate(acts)
	} 

	// nav hover in ie6
	if($('ul.btns').length > 0 && jQuery.browser.msie) {
		$('ul.btns li')
			.mouseenter(function(){$(this).addClass('sfhover')})
			.mouseleave(function(){$(this).removeClass('sfhover')});
	}
	
	$(".thumbs li a").click(function () {
//display image from gallery
		var img_class = $(this).children("img").get(0).className;		
		$(".showroom img").removeClass("visible");
		$(".showroom img."+img_class+"").addClass("visible");
		return false;
	});

// thumb opacity
	$(".thumbs li a img").css("opacity", "0.75");
	$(".thumbs li a").hover(
		function(){
			$(this).children("img").css("opacity", "0.9");
		},
		function(){
			$(this).children("img").css("opacity", "0.75");
		}	
	);

	// thumb opacity - activities

	//	$(".activities_list li a").hover(
	//		function(){
	//			$(this).children("img").css("opacity", "1");
	//		},
	//		function(){
	//			$(this).children("img").css("opacity", "0.85");
	//		}	
	//	);

//scrollable
	if ($("div.scrollable").length > 0) {
		$("div.scrollable").scrollable({ 
				vertical: false,
				size: 1,
				next: ".r_btn",
				prev: ".l_btn",
				speed:	550,
				loop: true				
		}).circular().autoscroll({autoplay: true, interval: 10000});
	}

//scrollable activities
	addpagination();

//navigator - pagination


//Switch the "more" and "less" state per click in explore_mod
	$(".explore_mod h3").toggle(function(){
			$(this).next().hide("slow");
			$(this).children('span').html('[ + ]');
		}, function () {
			$(this).next().show("slow");
			$(this).children('span').html('[ &ndash; ]');
	});

//Switch the "Uncheck all" and "Check" state per click in alllink
	$(".toggle_container .alllink").click(function() {
			if ( $(this).children('i:first').html() == 'Uncheck All') {
				$(this).children('i').html('Check All');
				$(this).parent().find('input').each(function(){
					$(this).attr('checked', '');
					filter_acts($(this)[0]);
					});
			} else {
				$(this).children('i').html('Uncheck All');
				$(this).parent().find('input').each(function(){
					$(this).attr('checked', 'checked');
					filter_acts($(this)[0]);
					});
			}
	});
	
	$(".toggle_container .alllink_new").click(function() {
			if ( $(this).children('i:first').html() == 'Uncheck All') {
				$(this).children('i').html('Check All');
				$(this).parent().find('input').each(function(){
					$(this).attr('checked', '');
					filter_acts_new($(this)[0]);
					});
			} else {
				$(this).children('i').html('Uncheck All');
				$(this).parent().find('input').each(function(){
					$(this).attr('checked', 'checked');
					filter_acts_new($(this)[0]);
					});
			}
	});
	
	$('.child_class_program .activities_noscroll li').hover(function(){
		$(this).find('.description').animate({left:'0'},{queue:false,duration:400});
	}, function(){
		$(this).find('.description').animate({left:'-182px'},{queue:false,duration:400});
	});

//description opacity
	$(".description").css("opacity", "0.9");

// tabs switching
	if($('.tabs').length > 0) {
		tabMaxHeight = 0;
		$('.tc_module').each(function(){
			if(tabMaxHeight < $(this).height()) {
				tabMaxHeight = $(this).height();
			}
				$(this).css({position: 'static', display: 'none'}).filter('.on').css({display: 'block'})
		});
		tabMaxHeight += 40;
		$('.tabs_container').css({height: tabMaxHeight+'px'})
		$('.tabs a').click(function(){
			$('.tabs li').removeClass();
			$(this).parent().addClass('on')
			ind = $('.tabs a').index($(this));
			$('.tabs_content .tc_module').slideUp();
			$('.tabs_content .tc_module:eq('+ind+')').slideDown()
			return false;
		});
		$("#trip_plan").css("bottom", "0");
	}

//fancybox
	if($('.fancy_details, .play_vid_link').length > 0) {
		$(".fancy_details, .play_vid_link").fancybox({
			frameWidth: 560,
			frameHeight: 340,
			hideOnContentClick: false});
	}
//fancybox activities popup
	if($('.activities_list li a.activitylink').length > 0) {
		$(".activities_list li a").click(function(){
			myhref = $(this).attr('href');
			findme = "[href='" + myhref + "']:first";
			top.autoclick['0win'] = findme;
		}).fancybox({
			frameWidth: 705,
			frameHeight: 505,
			hideOnContentClick: false
		});
	}
	
	
//pngFix
	$(document).pngFix();
	
//categories expand/collapse
	$(".shop_cats > li > a:first-child").click(function(){
		var parent_li = $(this).parent("li");
		if (parent_li.hasClass("cat_collapsed")) {
			parent_li.removeClass("cat_collapsed").addClass("cat_expanded");
			parent_li.children(".shop_subcats").show();
		} else if (parent_li.hasClass("cat_expanded")) {
			parent_li.removeClass("cat_expanded").addClass("cat_collapsed");
			parent_li.children(".shop_subcats").hide();
		}
		return false;
	});
	
	
	// product area carousel
		paDelay = 6000;

		paCurrent = 0;
		paNext = 1;
		paAnimating = false;
		paWidth = $('.product_area').width();
		paTotal = $('.product_area .item').length;
		// make the pager
		$('.prod_pager').append('<a href="#" class="prev">&lt;</a><ul class="prod_pagin"></ul><a href="#" class="next">&gt;</a>')
		for(var i=0; i<paTotal; i++) {
			$('.prod_pagin').append('<li><a>'+(i+1)+'</a></li>')
		}
		$('.prod_pager li').click(function(){
			if($(this).hasClass('active')) return false;
			paNext = $('.prod_pager li').index($(this))
			paShowNext()
		})
		$('.prod_pager .prev').click(function(){
			paNext = paCurrent-1;
			if(paNext < 0) paNext = paTotal-1;
			paShowNext()
		})
		$('.prod_pager .next').click(function(){
			paNext = paCurrent+1;
			paNext %= paTotal;
			paShowNext()
		})

		$('.prod_pagin li:first').addClass('first active')
		$('.product_area').append('<div class="desc_holder"></div>')
		$('.desc_holder').css({position:'absolute', left:600})
		$('.product_area .item:first').find('.product_desc_wrap').prependTo('.product_area').find('.product_desc').clone().appendTo('.desc_holder')
		$('.product_area .item:not(:first)').each(function(){
			$(this).find('.product_desc').appendTo('.desc_holder')
			$(this).find('.product_desc_wrap').remove()
		})

		paRotate = setTimeout(paShowNext, paDelay)
		function paShowNext() {
			if(paAnimating) return false;
			paAnimating = true;
			clearTimeout(paRotate)
			$('.prod_pager li').removeClass('active');
			$('.prod_pager li:eq('+paNext+')').addClass('active');

			$('.product_area .item:eq('+paCurrent+')').css({zIndex:1})
			$('.product_area .item:eq('+paNext+')')
				.css({
					zIndex: 2,
					left: paWidth
				})
				.animate({
					left: 0
				},{
					duration: 600,
					complete: function(){
						paAnimating = false;
						paCurrent = paNext;
						paNext++;
						paNext %= paTotal;
						paRotate = setTimeout(paShowNext, paDelay)
					}
				})
			$('.product_desc_wrap:eq(0) .product_desc').fadeOut(450, function(){
				$(this).remove();
				$('.desc_holder .product_desc:eq('+paNext+')').clone().appendTo('.product_desc_wrap')
			})
		}

});

if (typeof urlencode != 'function') {
	
	function urlencode (str) {
	    // http://kevin.vanzonneveld.net
	    // +   original by: Philip Peterson
	    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
	    // +      input by: AJ
	    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
	    // +   improved by: Brett Zamir (http://brett-zamir.me)
	    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
	    // +      input by: travc
	    // +      input by: Brett Zamir (http://brett-zamir.me)
	    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
	    // +   improved by: Lars Fischer
	    // +      input by: Ratheous
	    // +      reimplemented by: Brett Zamir (http://brett-zamir.me)
	    // +   bugfixed by: Joris
	    // +      reimplemented by: Brett Zamir (http://brett-zamir.me)
	    // %          note 1: This reflects PHP 5.3/6.0+ behavior
	    // %        note 2: Please be aware that this function expects to encode into UTF-8 encoded strings, as found on
	    // %        note 2: pages served as UTF-8
	    // *     example 1: urlencode('Kevin van Zonneveld!');
	    // *     returns 1: 'Kevin+van+Zonneveld%21'
	    // *     example 2: urlencode('http://kevin.vanzonneveld.net/');
	    // *     returns 2: 'http%3A%2F%2Fkevin.vanzonneveld.net%2F'
	    // *     example 3: urlencode('http://www.google.nl/search?q=php.js&ie=utf-8&oe=utf-8&aq=t&rls=com.ubuntu:en-US:unofficial&client=firefox-a');
	    // *     returns 3: 'http%3A%2F%2Fwww.google.nl%2Fsearch%3Fq%3Dphp.js%26ie%3Dutf-8%26oe%3Dutf-8%26aq%3Dt%26rls%3Dcom.ubuntu%3Aen-US%3Aunofficial%26client%3Dfirefox-a'

	    str = (str+'').toString();

	    // Tilde should be allowed unescaped in future versions of PHP (as reflected below), but if you want to reflect current
	    // PHP behavior, you would need to add ".replace(/~/g, '%7E');" to the following.
	    return encodeURIComponent(str).replace(/!/g, '%21').replace(/'/g, '%27').replace(/\(/g, '%28').
	                                                                    replace(/\)/g, '%29').replace(/\*/g, '%2A').replace(/%20/g, '+');
	}
}
