var CURRENCY_SYMBOL = "$";
var hotel = 'H';
var vacationPackage = 'P';

$.extend({
    getUrlVars: function(){
        var vars = [], hash;
        var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
        for(var i = 0; i < hashes.length; i++)
        {
            hash = hashes[i].split('=');
            vars.push(hash[0]);
            vars[hash[0]] = decodeURIComponent(hash[1]);
        }
        return vars;
    }
});
$.extend({
    getUrlVar: function(name){
        return $.getUrlVars()[name];
    }
});

// This will return ALL matching parameters not just the first one like the above functions
$.urlParamArray = function(name){
    var hash;
    var results = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    var array = [];
    for(var i = 0; i < results.length; i++)
    {
        hash = results[i].split('=');
        if (hash[0] == name){
            array.push(decodeURIComponent(hash[1]));
        }
    }
    return array;
}

function gallery(imgNumber, showCaption) {
	$('#gallery div.show').hide().removeClass('show');
	var next = $('#gallery_' + imgNumber);
	next.hide().addClass('show').show();
	
	if (showCaption) {
		var caption = next.find('span').html();	
		$('#gallery .pkgText').hide(0, function() {$('#gallery .pkgText').html(caption);});	
		$('#gallery .pkgText').show();
        var lbl = imgNumber + 1;
        $("#imgSelector").find("li").each(function() {
            var li = $(this);
            if (li.html() == lbl) {
                li.removeClass("primaryLink");
            } else if (!li.hasClass("primaryLink")) {
                li.addClass("primaryLink");
            }
        });
	}	
}


function removeParmaeterFromURL(url, parameter) {
    newUrl = String(url);
    var regex = new RegExp( "\\?" + parameter + "=[^&]*&?", "gi");
    newUrl = newUrl.replace(regex,'?');
    regex = new RegExp( "\\&" + parameter + "=[^&]*&?", "gi");
    newUrl = newUrl.replace(regex,'&');
    newUrl = newUrl.replace(/(\?|&)$/,'');
    regex = null;
    return newUrl;
}

function isValidDate(id, format){
    var isValid = true;
    try{
        $.datepicker.parseDate(format, $('#' + id).val(), null);
    }
    catch(error){
        isValid = false;
    }
    return isValid;
}

function isDateGTToday(id, format){
    var date = $.datepicker.parseDate(format, $('#' + id).val());
    return date > new Date()
}

function isDateTooFarOut(id, format) {
	 var date = $.datepicker.parseDate(format, $('#' + id).val());
	 var allowedDate = new Date();
	 allowedDate.setDate(allowedDate.getDate() + 330);
	 return date > allowedDate;
}

function resetDropDownSelectors()
{
    $(".dropDownPlus").show();
    $(".dropDownMinus").hide();
}

function toggleLinks(id, id2)
{
    resetDropDownSelectors();
    resetLinks();
    if($(id).find('.dropDownPlus').is(":visible"))
    {
        $(id).find("span").toggle();
        $(id2).show();
    }
}

function toggleDepartureLinks()
{
    var ctl = $("#departureDropDown");
    ctl.find("span").toggle();
    var mnu = $("#departureLinkContainers")
    mnu.css('left', ctl.position().left);
    mnu.toggle();
}

function reload(airport) {
    var search = window.location.search;
    var newDeparture = "departureAirport=" + airport;
    if (search) {
        if (search.indexOf("departureAirport")!=-1) {
            window.location.search = search.replace(/departureAirport=[A-Za-z]+/, newDeparture);
        } else {
            window.location.search = search + "&" + newDeparture;
        }
    } else {
        window.location.search = "?" + newDeparture;
    }
}

function validateEmail(email){

    if (email != null && email != ""){
        var pattern = /^[\w.]+@[a-zA-Z_.]+?\.[a-zA-Z]{2,3}$/;
        if (email.match(pattern)){
            return true;
        }
        else{
            alert("Email is not valid");
            return false;
        }
    }
    else{
        alert("Email is required");
        return false;
    }
}

function forward(id) {
    var value = $("#" + id).val();
    if (value != "") {
        window.location = value;
    }
}

var dialogs = {};
function showInfo(link, width, height) {
    var a = $(link);
    var w = width != undefined ? width : 700;
    var h = height != undefined ? height : 200;
    var href = a.attr('href');
    if(href.indexOf('?') >= 0) {
    	href = href.substring(0, href.indexOf('?'));
    }
    var dialog = dialogs[href];
    if (!dialog) {
    		dialog = $('<div></div>')
            .load(a.attr('href'))
            .dialog({
                autoOpen: false,
                title: a.attr('title'),
                modal: true,
                show: 'slide',
                maxHeight: h,
                maxWidth: w,
                width: w,
                position: 'center'
            });
        if (height) {
            dialog.dialog("option", "height", height);
        }
        dialogs[href] = dialog;
    }

    dialog.dialog('open');
    return false;
}

function parseUsDate(d) {
   var parts = d.match(/(\d+)/g);
   var t = new Date(parts[2], parts[0] -1, parts[1]);
   return t.getTime();
}

/* SEO Scripts */
/* Shared */
function scrollToElement(element, offset) {
	if (typeof offset == 'undefined') {
		var offset = 0;
	}
	
	if($(element).length < 1) {
		return false;
	}
	
	$('html,body').animate({scrollTop:$(element).first().offset().top + offset}, 500);
}

function isScrolledIntoView(element) {
    var docViewTop = $(window).scrollTop();
    var docViewBottom = docViewTop + $(window).height();

    var elementTop = $(element).offset().top;
    var elementBottom = elementTop + $(element).height();

    return ((elementBottom >= docViewTop) && (elementTop <= docViewBottom));
}
/* Right Nav */
$(function() {
	$(".content .otherResorts ul").each(function(){makeExpandableList(this)});
});

function makeExpandableList(parentElement, maxCount) {
	if (typeof maxCount == 'undefined') {
		maxCount = 10;
	}
	
	function changeHeightSmooth(element, newHeight) {
		$(element).animate({height: newHeight}, 500);
	}
	
	function expand(parentElement) {
		changeHeightSmooth(parentElement, $(parentElement).data("originalHeight"));
		$(parentElement).parent().find("a.moreOrLess").text("Less");
	}
	
	function contract(parentElement, initial) {
		var firstHiddenElement = children.get(maxCount);
		var newHeight = $(firstHiddenElement).position().top;
		
		$(parentElement).parent().find("a.moreOrLess").text("More");
		
		if (typeof initial == 'undefined' || initial == false) {
			changeHeightSmooth(parentElement, newHeight);
			var firstElement = children.first();
			if (!isScrolledIntoView(firstElement)) {
				scrollToElement(firstElement, -75);
			}
		} else {
			$(parentElement).height(newHeight);
		}
	}
	
	$(parentElement).css("overflow", "hidden");
	$(parentElement).css("position", "relative");
	
	var children = $(parentElement).children();
	
	if (children.size() > maxCount) {
		$(parentElement).after("<div class='moreOrLess'><a href='#' class='moreOrLess'>More</a></div>");
		
		$(parentElement).parent().find("a.moreOrLess").click(function(event) {
			switch($(this).text()) {
			case "More":
				expand(parentElement);
				break;
			case "Less":
				contract(parentElement);
				break;
			}
			event.stopPropagation();
			return false;
		});
		
		$(parentElement).data("originalHeight", $(parentElement).height());
		
		contract(parentElement, true);
	}
}
/* Footer */
$(function() {
	$(".footerSection").hide();
});

function toggleFooter(control) {
	var id = $(control).attr('id');
	var opening = $(control).text() == "+";

	// hide all and reset
	$(".footer .footerSection").hide();
	$(".footer .plus").text("+");

	if (opening) {
		$("#"+id+"FooterLinks").show();
		$(control).text("-");
	}
}
/* SEO Ends */
