var selectedMenuItem;
var submenuTimeout=0;
var mainNavOn=false;
var selectedMenuItem;
var prevItem;
var subNavSelected = false

function MainNavOut( element )
{
	clearInterval(submenuTimeout);
	startResetNavHighlight();
}

function MainNavFocus( element )
{
	clearResetNavHighlight();

	if ( element == null) return;
	selectedMenuItem = element;

	if (submenuTimeout >0)
		clearTimeout(submenuTimeout);

	//Call with timer if the main nav has something on, otherwise, call straigt away
	if (mainNavOn && subNavSelected==true) 
	{
		submenuTimeout = setTimeout( 'MainNavFocusSubMenu()', 400 );
	} 
	else 
	{
		submenuTimeout = setTimeout( 'MainNavFocusSubMenu()', 100 );
	}

	subNavSelected = false;
}


function MainNavDeFocus( element )
{
	MainNavHideButton(element);
}

// Turn on menu
function MainNavFocusSubMenu()
{

    element = selectedMenuItem;
	turnOffDefaultNav();
	turnOffAllNavs();

	element.style.opacity="0.9";
	element.parentNode.style.backgroundPosition="0 -36px";
	element.style.backgroundPosition="0 -36px";
	if(element.id.length == 8) 
	{
			subNavNum=element.id.substring(3,4);
	} 
	else 
	{
		subNavNum=element.id.substring(3,5);
	}
	document.getElementById("subNav"+subNavNum).style.display="block";

	clearTimeout(submenuTimeout);
	mainNavOn=true;

}

function highLightRow(targetEl) 
{
 targetEl.style.background="#fcdddd";
 strongElements=targetEl.getElementsByTagName("strong");
 strongElements[0].style.textDecoration="underline";
 strongElements[1].style.textDecoration="underline";
}

function dehighLightRow(targetEl) 
{
 strongElements=targetEl.getElementsByTagName("strong");
 strongElements[0].style.textDecoration="none";
 strongElements[1].style.textDecoration="none";
 if(targetEl.className=="odd" || targetEl.className=="firstCol odd")
 {
    targetEl.style.background="#ffffff";
 }
 else
 {
    targetEl.style.background="";
 }
}

function highLightRowGrey(targetEl) 
{
 targetEl.style.background="#f1f1f1";
}
function dehighLightRowGrey(targetEl) 
{
 targetEl.style.background="#ffffff";
}

//  Get integer value
function getIntVal(daVar)
{
	daVar = daVar.substr(0, daVar.length -2);
	if(daVar.length <1)
	{
		return 0;
	}
	else
	{
		return parseInt(daVar);
	}
}

/*========*/
//  Specials Reveal
/*========*/
function changeSpecial(specialNum)
{
    var spec = [];
    var specialContent = [];

    spec[1] = document.getElementById('spNav1');
    spec[2] = document.getElementById('spNav2');
    spec[3] = document.getElementById('spNav3');
    spec[4] = document.getElementById('spNav4');
    spec[5] = document.getElementById('spNav5');
    spec[6] = document.getElementById('spNav6');

    specialContent[1] = document.getElementById('specialAirfares');
    specialContent[2] = document.getElementById('specialHotels');
    specialContent[3] = document.getElementById('specialCars');
    specialContent[4] = document.getElementById('specialHolidays');
    specialContent[5] = document.getElementById('specialCruises');
    specialContent[6] = document.getElementById('specialTours');

    for(i = 1; i<= spec.length; i++)
    {
        if(i == specialNum)
        {
            spec[i].className = "selected";
            document.getElementById('spNav'+i+'Over').className = "selected";
            if(specialContent[i])
            {
                specialContent[i].style.display = "block";
            }
        }
        else
        {
            if(spec[i])
            {
                spec[i].className = "spNavElement hoverPointer";
            }

            tempElement = document.getElementById('spNav'+i+'Over');
            if(tempElement)
            {
                tempElement.className = "spNavElementOver hoverPointer";
            }

            if(specialContent[i])
            {
                specialContent[i].style.display = "none";
            }
        }
    }
 }



/*=========*/
//  Destination Tools
/*=========*/
var optBtn = [];
var optList = [];
var listHeight = [];
var destTween;

function revealOptions(optionNum)
{

    optBtn[1] = document.getElementById('headLeft');
    optBtn[2] = document.getElementById('toolsOpt');
    optBtn[3] = document.getElementById('infoOpt');
    optBtn[4] = document.getElementById('placeOpt');
    optBtn[5] = document.getElementById('siteOpt');
    optBtn[6] = document.getElementById('reviewOpt');

    optList[1] = document.getElementById('guideList');
    optList[2] = document.getElementById('travelToolList');
    optList[3] = document.getElementById('essentialInfoList');
    optList[4] = document.getElementById('topPlacesList');
    optList[5] = document.getElementById('topSiteList');
    optList[6] = document.getElementById('topReviewList');

    listHeight[1] = optList[1].offsetHeight;
    listHeight[2] = optList[2].offsetHeight;
    listHeight[3] = optList[3].offsetHeight;
    listHeight[4] = optList[4].offsetHeight;
    listHeight[5] = optList[5].offsetHeight;
    listHeight[6] = optList[6].offsetHeight;

    if(document.getElementById('destContain'+optionNum).offsetHeight <5)
    {
        document.getElementById('optImg'+optionNum).src = "/wcsstore/ConsumerDirectStorefrontAssetStore/images/arrow_head_down.gif";
        openListOption(optionNum);
    }
}


/*==========*/
//  Animate the option list opening and closing
/*==========*/


function openListOption(optNum)
{

    containerHeight = document.getElementById('destContain'+optNum).style.height;
    continerHeight = getIntVal(containerHeight);

    if(listHeight[optNum] > containerHeight+2)
    {
        speed = 5;

        if(listHeight[optNum] < containerHeight+5){speed = 2;}

        temp = containerHeight+((listHeight[optNum] - containerHeight)/speed);
        document.getElementById('destContain'+optNum).style.height = temp+"px";

        clearTimeout(destTween);
	    destTween = setTimeout('openListOption('+optNum+')', 25);

        for(g=1; g<=optBtn.length; g++)
        {
            if(g!=optNum)
            {
                if(optBtn[g])
                { //catch for ie6

                    if(g>optNum)
                    { 
                        document.getElementById('optImg'+g).src = "/wcsstore/ConsumerDirectStorefrontAssetStore/images/arrow_head_up.gif";
                    }
                    else if(g<optNum)
                    {
                        document.getElementById('optImg'+g).src = "/wcsstore/ConsumerDirectStorefrontAssetStore/images/arrow_head_down.gif";
                    }

                    containerHeight = document.getElementById('destContain'+g).style.height;

                    if(containerHeight)
                    {
                        containerHeight = getIntVal(containerHeight);
                    }
                    else
                    {
                        containerHeight = document.getElementById('destContain'+g).offsetHeight;
                    }
                    if(containerHeight>1)
                    {
                        temp = containerHeight+((0 - containerHeight)/speed);
                        document.getElementById('destContain'+g).style.height = temp+"px";
                    }
                    else
                    {
                        document.getElementById('destContain'+g).style.height = 0+"px";
                    }
                }
            }
        }
    }
    else
    {
        clearTimeout(destTween);
        document.getElementById('destContain'+optNum).style.height = contentHeight+"px";
        for(g=1; g<=optBtn.length; g++)
        {
            if(g!= optNum)
            {
                document.getElementById('destContain'+g).style.height = 0+"px";
            }
        }
    }
}


/*==============*/
//  Feedback page forms/ also used to animate search and product filters
/*=============*/
var feedTween;
var closeFeedTween;
var inMotion = false;
var destinationImgAr = [];
var originalExperienceString;

destinationImgAr[2] = "/wcsstore/ConsumerDirectStorefrontAssetStore/images/dest_bar_asia";
destinationImgAr[3] = "/wcsstore/ConsumerDirectStorefrontAssetStore/images/dest_bar_sthpac";
destinationImgAr[4] = "/wcsstore/ConsumerDirectStorefrontAssetStore/images/dest_bar_uk";
destinationImgAr[5] = "/wcsstore/ConsumerDirectStorefrontAssetStore/images/dest_bar_americas";
destinationImgAr[6] = "/wcsstore/ConsumerDirectStorefrontAssetStore/images/dest_bar_africa";


var prefix='';
function fbOption(feedNum, totalNum, imgSet)
{
	if (imgSet==2)
	    {prefix='lb'} 
	else 
	    {prefix=''};
    contentHeight = document.getElementById(prefix+'fbContent'+feedNum).offsetHeight;
    containerHeight = document.getElementById(prefix+'fbContain'+feedNum).style.height;
    containerHeight = getIntVal(containerHeight);

    if(contentHeight>containerHeight+5)
    {
        if(imgSet == 1)
        {
            for(g=1; g<=totalNum; g++)
            {
                if(g>feedNum)
                {
                    document.getElementById(prefix+'optImg'+g).src = "/wcsstore/ConsumerDirectStorefrontAssetStore/images/arrow_head_up.gif";
                }
                else
                {
                    document.getElementById(prefix+'optImg'+g).src = "/wcsstore/ConsumerDirectStorefrontAssetStore/images/arrow_head_down.gif";
                }
            }
        }
        else if(imgSet == 2)
        {
            for(g=2; g<=totalNum; g++)
            {
                if(g!=feedNum)
                {
                    document.getElementById(prefix+'optImg'+g).src = destinationImgAr[g]+"_up.gif";
                }
                else
                {
                    document.getElementById(prefix+'optImg'+g).src = destinationImgAr[g]+"_down.gif";
                }
            }
        }

        openFeed(feedNum, totalNum); //ANIMATE PANEL OPENING
    
    
    }
    else if(feedNum == 99)
    {
        document.getElementById(prefix+'fbContent99').innerHTML = originalExperienceString;
        document.getElementById('bar99').style.display = "none";
        openFeed(feedNum, totalNum);
        
    }
    else if(inMotion == false)
    {
        if(imgSet == 1)
        {
            for(g=1; g<=totalNum; g++)
            {
                document.getElementById(prefix+'optImg'+g).src = "/wcsstore/ConsumerDirectStorefrontAssetStore/images/arrow_head_down.gif";
            }
            closeFeed(feedNum); //CLOSE ALL PANELS
            
        }
        else if(imgSet == 2)
        {
            for(g=2; g<=totalNum; g++)
            {
                document.getElementById(prefix+'optImg'+g).src = destinationImgAr[g]+"_up.gif";
            }
            openFeed(1, totalNum); //ANIMATE PIC PANEL

        }
        else
        {
            closeFeed(feedNum); //CLOSE ALL PANELS
        }
   }
}


function openFeed(feedNum, totalNum)
{
    contentHeight = document.getElementById(prefix+'fbContent'+feedNum).offsetHeight;
    containerHeight = document.getElementById(prefix+'fbContain'+feedNum).style.height;
    containerHeight = getIntVal(containerHeight);

    if(contentHeight >= containerHeight+2)
    {
        inMotion = true;
        speed = 5;

        if(contentHeight < containerHeight+5)
        {
            speed = 2;
        }

        temp = containerHeight+((contentHeight - containerHeight)/speed);
        document.getElementById(prefix+'fbContain'+feedNum).style.height = temp+"px";

        clearTimeout(feedTween);
	    feedTween = setTimeout('openFeed('+feedNum+', '+totalNum+')', 25);


        for(g=1; g<=totalNum; g++)
        {
            if(g!=feedNum)
            {
                contentHeight = document.getElementById(prefix+'fbContent'+g).offsetHeight;
                containerHeight = document.getElementById(prefix+'fbContain'+g).style.height;
                containerHeight = getIntVal(containerHeight);

                if(containerHeight>1)
                {
                    temp = containerHeight+((0 - containerHeight)/speed);
                    document.getElementById(prefix+'fbContain'+g).style.height = temp+"px";
                }
                else
                {
                    document.getElementById(prefix+'fbContain'+g).style.height = 0+"px";
                }
            }
        }
    }
    else
    {
        inMotion = false;
        clearTimeout(feedTween);
        document.getElementById(prefix+'fbContain'+feedNum).style.height = contentHeight+"px";
        for(g=1; g<=4; g++)
        {
            if(g!=feedNum)
            {
                document.getElementById(prefix+'fbContain'+g).style.height = 0+"px";
            }
        }
    }
}

function closeFeed(feedNum)
{
    containerHeight = document.getElementById(prefix+'fbContain'+feedNum).style.height;
    containerHeight = getIntVal(containerHeight);

    clearTimeout(closeFeedTween);

    if(containerHeight>2)
    {
        temp = containerHeight+((0 - containerHeight)/5);
        document.getElementById(prefix+'fbContain'+feedNum).style.height = temp+"px";
        closeFeedTween = setTimeout('closeFeed('+feedNum+')', 25);
    }
    else
    {
        document.getElementById(prefix+'fbContain'+feedNum).style.height = 0+"px";
    }
}


function truncateString(max) 
{
  str = document.getElementById(prefix+'fbContent99').innerHTML;
  originalExperienceString = str;

  if(str.length > max) 
  {
    do{
        max--;
    }while(str.charAt(max)!=" ")

    str = str.substring(0, max);
    document.getElementById(prefix+'fbContent99').innerHTML = str+"...";
    str = document.getElementById(prefix+'fbContain99').style.height = document.getElementById(prefix+'fbContent99').offsetHeight+"px";
  }
}


function turnOffAllNavs()
{
// Turn off all navigattion elements

	var numberOfNavigators=10;
	mainNavOn=false;

	for(i=1;i<=numberOfNavigators;i++)
	{
	    if(document.getElementById("nav"+i+"Over") !=null)
	    {
		    document.getElementById("nav"+i+"Over").style.opacity="-0.1";
		    document.getElementById("nav"+i+"Over").setAttribute("class","topNavElement");
		    document.getElementById("nav"+i+"Over").style.backgroundPosition="0 0";
		    document.getElementById("nav"+i).style.backgroundPosition="0 0";
		    document.getElementById("subNav"+i).style.display="none";
		}
	}
}

function turnOffDefaultNav()
{
	var mainNavEle=document.getElementById(saveMainNav);
	if (selectedMenuItem != mainNavEle) 
	{
	    if (mainNavEle!=null) 
	    {
		    mainNavEle.style.opacity="-0.1";
		    mainNavEle.style.backgroundPosition="0 0px";
		    mainNavEle.parentNode.style.backgroundPosition="0 0px";
		    if(saveMainNav.length == 8) 
		    {
			    subNavNum=saveMainNav.substring(3,4);
		    } 
		    else 
		    {
			    subNavNum=saveMainNav.substring(3,5);
		    }
		    document.getElementById("subNav"+subNavNum).style.display="none";
	    }

	    subNavEle=document.getElementById(saveSubNav);
	    if (subNavEle!=null) 
	    {
		    subNavEle.style.backgroundColor="";
	    }
	}
}

var saveMainNav ="";
var saveSubNav = "";
function highlightDefaultNav(mainNav,subNav) 
{
	saveMainNav=mainNav;
	saveSubNav=subNav;
	prevItem=selectedMenuItem;

	turnOffAllNavs();

	var mainNavEle=document.getElementById(mainNav);
	if (mainNavEle!=null) 
	{
		mainNavOn=true;
		mainNavEle.style.opacity="0.9";
		mainNavEle.parentNode.style.backgroundPosition="0 -36px";
		mainNavEle.style.backgroundPosition="0 -36px";
		if(mainNav.length == 8) 
		{
			subNavNum=mainNav.substring(3,4);
		} 
		else 
		{
			subNavNum=mainNav.substring(3,5);
		}
		document.getElementById("subNav"+subNavNum).style.display="block";
	}

	subNavEle=document.getElementById(subNav);
	if (subNavEle!=null) 
	{
		subNavEle.style.backgroundColor="#dddddd";
	}
}

function setDefaultNav() 
{
	urlStr=document.URL.toLowerCase();

	if (urlStr.indexOf("aboutus")>0) highlightDefaultNav('nav1Over','subNav11');
	if (urlStr.indexOf("global-travel-blog")>0) highlightDefaultNav('nav1Over','subNav14');

	if (urlStr.indexOf("find-a-holiday")>0) highlightDefaultNav('nav4Over','');
	if (urlStr.indexOf("plan/map")>0) highlightDefaultNav('nav4Over','subNav42');
	//if (urlStr.indexOf("/destinations")>0) highlightDefaultNav('nav4Over','subNav43');
	if (urlStr.indexOf("/experience")>0) highlightDefaultNav('nav4Over','subNav44');
	if (urlStr.indexOf("global-travel-blog")>0) highlightDefaultNav('nav4Over','subNav46');
	if (urlStr.indexOf("travel-planning")>0) highlightDefaultNav('nav4Over','subNav48');

	if (urlStr.indexOf("travel-specials")>0) highlightDefaultNav('nav5Over','');
	if (urlStr.indexOf("cheap-flights")>0) highlightDefaultNav('nav5Over','subNav51');
	if (urlStr.indexOf("cheap-hotels")>0) highlightDefaultNav('nav5Over','subNav52');
	if (urlStr.indexOf("travel-specials/cheap-cars")>0) highlightDefaultNav('nav5Over','subNav53');
	if (urlStr.indexOf("cheap-holidays")>0) highlightDefaultNav('nav5Over','subNav54');
	if (urlStr.indexOf("cheap-cruises")>0) highlightDefaultNav('nav5Over','subNav55');
	if (urlStr.indexOf("cheap-tours")>0) highlightDefaultNav('nav5Over','subNav56');
	if (urlStr.indexOf("news-room")>0) highlightDefaultNav('nav5Over','subNav57');

	if (urlStr.indexOf("/booking")>0) highlightDefaultNav('nav6Over','');
	if (urlStr.indexOf("booking/flights")>0) highlightDefaultNav('nav6Over','subNav61');
	if (urlStr.indexOf("booking/hotels")>0) highlightDefaultNav('nav6Over','subNav62');
	if (urlStr.indexOf("booking/cars")>0) highlightDefaultNav('nav6Over','subNav63');
	if (urlStr.indexOf("booking/insurance")>0) highlightDefaultNav('nav6Over','subNav64');
	if (urlStr.indexOf("make-a-booking/airport-parking")>0) highlightDefaultNav('nav6Over','subNav65');
	if (urlStr.indexOf("make-a-booking/rail")>0) highlightDefaultNav('nav6Over','subNav66');
	if (urlStr.indexOf("make-a-booking/activities")>0) highlightDefaultNav('nav6Over','subNav67');

	if (urlStr.indexOf("/australia-travel/")>0) highlightDefaultNav('nav8Over','subNav85');
	if (urlStr.indexOf("/adventure-travel")>0) highlightDefaultNav('nav8Over','subNav86');
	if (urlStr.indexOf("/ski/")>0) highlightDefaultNav('nav8Over','subNav87');

	if (urlStr.indexOf("contactus")>0) highlightDefaultNav('nav9Over','');
	if (urlStr.indexOf("contact-flightcentre")>0) highlightDefaultNav('nav9Over','subNav91');
	if (urlStr.indexOf("#customerassistance")>0) highlightDefaultNav('nav9Over','subNav94');
	if (urlStr.indexOf("#feedback")>0) highlightDefaultNav('nav9Over','subNav95');
	if (urlStr.indexOf("#companyenquiry")>0) highlightDefaultNav('nav9Over','subNav96');
	if (urlStr.indexOf("findastore")>0) highlightDefaultNav('nav9Over','subNav92');
}

var resetNavTimer;
function clearResetNavHighlight()
{
	clearTimeout(resetNavTimer);
}

function setSubNav(subNavEle) 
{
// When a subnav is mouseOver then clear all previous and highlight current.
	hoverColor="#dddddd";
	clearTimeout(resetNavTimer);
	subNavSelected = true;

	allSubNavEles=subNavEle.parentNode.getElementsByTagName("li");
	for (i=0;i<=allSubNavEles.length-1;i++) 
	{
		allSubNavEles[i].style.backgroundColor="";
	}

	subNavEle.style.backgroundColor=hoverColor;
}

function startResetNavHighlight()
{
	resetNavTimer = setTimeout( 'highlightDefaultNav(saveMainNav,saveSubNav)', 2000 ); 
}