﻿/* Source: http://www.w3schools.com/js/js_cookies.asp 
Returns a cookie value based on the provided c_name
*/
function getCookie(c_name) {
    if (document.cookie.length > 0) {
        c_start = document.cookie.indexOf(c_name + "=");
        if (c_start != -1) {
            c_start = c_start + c_name.length + 1;
            c_end = document.cookie.indexOf(";", c_start);
            if (c_end == -1) c_end = document.cookie.length;
            return unescape(document.cookie.substring(c_start, c_end));
        }
    }
    return null;
}

// Sets a cookie
// Updated the script by adding ";path=/", else id didn't work properly
function setCookie(c_name, value, expiredays) {
    var exdate = new Date(); exdate.setDate(exdate.getDate() + expiredays);
    document.cookie = c_name + "=" + escape(value) +
((expiredays == null) ? "" : ";expires=" + exdate.toGMTString()) + ";path=/;";
}

/*
	SearchTextBox control (server code: see NetMatch.MTravel.Web.UI.SearchTextBox).
*/
function SearchTextBox(clientId, searchUrl, initialValue, resourceText)
{
	this._SearchUrl = searchUrl;
	this._InitialValue = initialValue;
	this._ResourceText = resourceText;
	this._Element = document.getElementById(clientId);
}

SearchTextBox.prototype.focus = function() {
	if (this._Element != null) {
		if (this._Element.value == this._ResourceText) {
			this._Element.value = '';
			this._Element.style.color = '#4f4f4f';
		}
		else {
			this._Element.style.color = '#4f4f4f';
		}
	}
}

SearchTextBox.prototype.blur = function() {
	if (this._Element != null) {
		if (this._Element.value == '') {
			this._Element.value = this._ResourceText;
			this._Element.style.color = '#b5b5b5';
		}
	} 
}

SearchTextBox.prototype.keyPress = function(event)
{			
	// FireFox: event.which, IE: event.keyCode
	var KeyCode = (event.which != null) ? event.which : event.keyCode;
			
	if (KeyCode == 13)
	{
		event.cancelBubble = true;
		event.returnValue = false;
		this.doSearch();
		return false;
	}
	return true;
}

SearchTextBox.prototype.doSearch = function()
{
	if ( (this._Element.value != "") && (this._Element.value != this._InitialValue) )
	{
		var SearchUrl = this._SearchUrl + this._Element.value;
		window.location = SearchUrl;		
	}
}
/*
	End of SearchTextBox control.
*/

/*
	ExpandableFactList control
*/

function openFactByLocationHash()
{
	/// <summary>Opens the </summary>
	var hash = location.hash;
	var factId;
	
	if(hash != '')
	{
		factId = hash.replace('#', '');
		openFact(factId);
	}
}

function openFact(factId)
{
	// Find the containing div (e.g. <div class="closed">)
	var element = document.getElementById(factId);
	if (element != null)
	{			
		element.className ='open';		
	}
}
/*
 End of	ExpandableFactList control
*/

function CreateBookmark(url, title) {
	if (window.sidebar) { // Mozilla Firefox Bookmark
	    window.sidebar.addPanel(title, url,"");
	}
	else if (window.external ) { // IE Favorite
	    window.external.AddFavorite(url, title);
	}
	else if (window.opera && window.print) { // Opera Hotlist
	    return true;
	}
}

/*
Opens a popup window in the specified width en height.
*/
function showPopUpWindow(theURL, width, height) {
    options = 'toolbar=no,menubar=no,location=no,directories=no,status=no,scrollbars=no,resizable=no,copyhistory=no';

    options += ',width=' + width + ',height=' + height;
    if (window.screen) {
        windowLeft = (screen.availWidth - width) / 2;
        windowTop = (screen.availHeight - height) / 2;
        options += ',left=' + windowLeft + ',top=' + windowTop;
    }
    newwindow = window.open(theURL, 'popup', options);
    newwindow.opener = this;
    newwindow.focus();
}

function InitDurationDimensions(durationWeeksVisible) {
    var lContainerDurationWeeks;
    var lContainerDurationNights;

    lContainerDurationDays = document.getElementById("dimensionDurantionNights");
    lContainerDurationWeeks = document.getElementById("dimensionDurantionWeeks");

    if(lContainerDurationDays != null) {
        lContainerDurationDays = lContainerDurationDays.firstChild;
    }

    if (lContainerDurationWeeks != null) {
        lContainerDurationWeeks = lContainerDurationWeeks.firstChild;
    }
    
    var lMoreElement = document.createElement("DD");
    var lLessElement = document.createElement("DD");

    lMoreElement.className = "morelesslink";
    lLessElement.className = "morelesslink";
    
    lMoreElement.innerHTML = '<a href="#" onclick="toggleDateMore();return false">'+DateDimesionMoreText+'</a>'
    lLessElement.innerHTML = '<a href="#" onclick="toggleDateLess();return false">'+DateDimesionLessText+'</a>'

    if (lContainerDurationWeeks != null) {
        lContainerDurationWeeks.appendChild(lMoreElement);
        lContainerDurationWeeks.style.display = "block";
        lContainerDurationWeeks.parentNode.style.display = "block";
    }

    if (lContainerDurationWeeks != null && lContainerDurationDays != null) {
        lContainerDurationDays.appendChild(lLessElement);
        lContainerDurationDays.style.display = "none";
        lContainerDurationDays.parentNode.style.display = "none";
    }
    else {
        if (lContainerDurationDays != null) {
            lContainerDurationDays.style.display = "block";
            lContainerDurationDays.parentNode.style.display = "block";
        }
    }
}

function toggleDateMore() {
    var lContainerDurationWeeks;
    var lContainerDurationNights;

    lContainerDurationDays = document.getElementById("dimensionDurantionNights");
    lContainerDurationWeeks = document.getElementById("dimensionDurantionWeeks");

    if (lContainerDurationDays != null) {
        lContainerDurationDays = lContainerDurationDays.firstChild;
    }

    if (lContainerDurationWeeks != null) {
        lContainerDurationWeeks = lContainerDurationWeeks.firstChild;
    }

    if (lContainerDurationWeeks != null && lContainerDurationDays != null) {
        lContainerDurationDays.style.display = "block";
        lContainerDurationDays.parentNode.style.display = "block";
        
        lContainerDurationWeeks.style.display = "none";
        lContainerDurationWeeks.parentNode.style.display = "none";
        
        var $lItem = $(lContainerDurationDays);
        if (!$lItem.children("dt").hasClass("open")) {
            $lItem.children("dt").click();
        }
    }

}

function toggleDateLess() {
    var lContainerDurationWeeks;
    var lContainerDurationNights;

    lContainerDurationDays = document.getElementById("dimensionDurantionNights");
    lContainerDurationWeeks = document.getElementById("dimensionDurantionWeeks");

    if (lContainerDurationDays != null) {
        lContainerDurationDays = lContainerDurationDays.firstChild;
    }

    if (lContainerDurationWeeks != null) {
        lContainerDurationWeeks = lContainerDurationWeeks.firstChild;
    }

    if (lContainerDurationWeeks != null && lContainerDurationDays != null) {
        lContainerDurationDays.style.display = "none";
        lContainerDurationDays.parentNode.style.display = "none";
        
        lContainerDurationWeeks.style.display = "block";
        lContainerDurationWeeks.parentNode.style.display = "block";
        
        var $lItem = $(lContainerDurationWeeks);
        if (!$lItem.children("dt").hasClass("open")) {
            $lItem.children("dt").click();
        }
    }
}

function initMenu(itemArray) {
    var lCurrentState = getCookie(RefinementStateCookieName);
    var lIdArray;
    var lSplitvalues =  new Array();

    if(lCurrentState != null)
    {
        lSplitvalues = lCurrentState.split("|");
    }

    if (lSplitvalues != null && lSplitvalues.length > 0) {
        lIdArray = lSplitvalues;
    }
    else {
        lIdArray = itemArray;
        StoreFirstState(itemArray);
    }
    // init all the menu items
    $('.sidePane .refinements dl dt').click(function() { toggleOpenClosedMenuItem(this) })

    if ($.isArray(lIdArray)) {
        for (var i = 0; i < lIdArray.length; i++) {
            if (lIdArray != "") {
                var $lItem = $('#' + lIdArray[i]);
                $lItem.children("dt").click();
            }
        }
    }
 }

 function toggleOpenClosedMenuItem(item) {
    // set the state
    $(item).toggleClass("closed");
    $(item).toggleClass("open");
    var $parent = $(item).parent("dl");
    $parent.toggleClass("closed");
    $parent.toggleClass("open");

    // Add the it in the cookie or remove it from the stored state
    SetElementsInCookie($parent[0].id, $(item).hasClass("open"));
}

function StoreFirstState(itemIdArray) {
    CreateCookie(itemIdArray.join("|"));
 }


function SetElementsInCookie(id, addItem) {
    // get the old value of the cookie
    var prevOpenRefGroupsIds = getCookie(RefinementStateCookieName);
    // initialize the cookie
    newOpenRefGroupsIds = "";
    // calculate the new value of the cookie
    if (addItem) {
        // add id
        if (prevOpenRefGroupsIds != null && prevOpenRefGroupsIds.indexOf(id) < 0) {
            newOpenRefGroupsIds = (prevOpenRefGroupsIds != "") ? prevOpenRefGroupsIds + "|" + id : id;
        }
    }
    else 
    {
        // remove id
        if (prevOpenRefGroupsIds != null && prevOpenRefGroupsIds.indexOf(id) >= 0) {
            var groupsArray = prevOpenRefGroupsIds.split("|");
            for (var i = 0; i < groupsArray.length; i++) {
                if (groupsArray[i] != id) {
                    newOpenRefGroupsIds += groupsArray[i] + "|";
                }
            }
            //delete last separator
            newOpenRefGroupsIds = newOpenRefGroupsIds.substring(0, newOpenRefGroupsIds.length - 1);
        }
    }

    // set the new value of the cookie
    if (newOpenRefGroupsIds != "") {
        CreateCookie(newOpenRefGroupsIds);
    }
}

function CreateCookie(value) {
    setCookie(RefinementStateCookieName, value, null);
}

