﻿var stateBag = [];
var priceTagContent;

function dayOver(cell)
{
    priceItem = findPriceItem(cell);
    if (priceItem != null) {
        setPriceTag(priceItem, false)
    }
}

function dayOut(cell) 
{
    priceItem = findPriceItem(cell);
    if (priceItem != null) {
        setPriceTag(priceItem, true)
    }
}

function makeOrder(cell) {
    priceItem = findPriceItem(cell);
    if (priceItem != null) {
        location.href = priceItem[5];
    }
}

function findPriceItem(cell) 
{
    var id = cell.id.split("_")[0];
    var selectedIndex = findArrival(id);
    if (selectedIndex > -1) {
        var priceItem = CalendarPrices[selectedIndex];
        return priceItem;
    } else {
        removeActivity(cell.id);
    }
}

function findArrival(id) 
{
    for (i = 0; i < CalendarPrices.length; i++) {
        if (CalendarPrices[i][0] == id) {
            return i;
            break;
        }
    }
    return findNearestArrival(id);
}

function findNearestArrival(id) {
    for (i = CalendarPrices.length - 1; i >= 0; i--) {
        for (x = 0; x < CalendarPrices[i][4].length; x++) {
            if (id == CalendarPrices[i][4][x]) {
                return i;
            }
        }
    }
    return -1;
}

function removeActivity(id)
{
	var el = document.getElementById(id);
	if (el)
	{
		el.onclick = new Function("return false;");
		el.onmouseover = new Function("return false;");
		el.onmouseout = new Function("return false;");
		el.style.cursor = "default";
	}
}

function setState(priceItem, state)
{
    for (i = 0; i < priceItem[4].length; i++) 
    {
        var el = document.getElementById(priceItem[4][i]);
        if (el) 
        {
        	if (state)
		    {
			    el.className = stateBag[el.id];
		    }
		    else
		    {
			    stateBag[el.id] = el.className;
			    el.className = "MouseOver";
		    }
        }
    }	
}

function setPriceTag(priceItem, state)
{
	var priceTag = document.getElementById("CalendarPrice");
	if (priceTag != null)
	{
	    setState(priceItem, state);
		if (!state)
		{
			priceTagContent = priceTag.childNodes[0].nodeValue;
			if (priceItem[3]) {
			    InsertHTML(priceTag, "<strike>"+ priceItem[2] + "</strike> " + priceItem[1]);
            } else {
                priceTag.childNodes[0].nodeValue = priceItem[1];
            }
		}
		else
		{
			if (!priceTagContent) {
				priceTagContent = "";
            }
			InsertHTML(priceTag, priceTagContent);
		}
	}
}