var originalGlobalNav=null;
var originalSubNav=null;
var globalNavTimer=null;
var PreviousNav=null;
var MENUBLOCKS = new Array();


function addOnloadEvent(fnc){
  if ( typeof window.addEventListener != "undefined" )
    window.addEventListener( "load", fnc, false );
  else if ( typeof window.attachEvent != "undefined" ) {
    window.attachEvent( "onload", fnc );
  }
  else {
    if ( window.onload != null ) {
      var oldOnload = window.onload;
      window.onload = function ( e ) {
        oldOnload( e );
        window[fnc]();
      };
    }
    else
      window.onload = fnc;
  }
}


function toggleWhatsHotPackages(flag){
	if(!flag){
		toggleGlobalSubnav('whatshotcollection','none')
		changeClass('packagesbutton', 'on')

		toggleGlobalSubnav('packagescollections','block')
		changeClass('whatshotbutton', '')
		toggleGlobalSubnav('packagefooter','block')
		toggleGlobalSubnav('whatshotfooter','none')

		if(allArcSliders['packagescollections']==undefined||allArcSliders['packagescollections']==null)allArcSliders['packagescollections'] = new arc_sliderObectj('packagescollections',true);
	}else{
		toggleGlobalSubnav('whatshotcollection','block')
		changeClass('packagesbutton', '')

		toggleGlobalSubnav('packagescollections','none')
		changeClass('whatshotbutton', 'on')
		toggleGlobalSubnav('packagefooter','none')
		toggleGlobalSubnav('whatshotfooter','block')
		if(allArcSliders['whatshotcollection']==undefined||allArcSliders['whatshotcollection']==null)allArcSliders['whatshotcollection'] = new arc_sliderObectj('whatshotcollection',true);
	}

}




function toggleGlobalSubnav(theDiv,onOff) {

	var elem = document.getElementById(theDiv);
	if(elem!=''&&elem!=undefined&&elem!=null){
		elem.style.display = onOff;
	}

}

function showMore(id) {
        var p = document.getElementById(id);
        if (p) {
            
            (p.style.display == "none"||p.style.display =="undefined"||p.style.display =="") ? p.style.display = "block" : p.style.display = "none";
        }
    }


function changeClass(id, newclass) {
	
	identity=document.getElementById(id);
	if(document.All)identity.setAttribute("className", newclass);
	else identity.className=newclass;
}

function setGlobalNavOn(newclass){

	var rssval = 'rss'; 
	if(newclass.indexOf('_') > -1){
		changeClass(newclass, "on");
		var tmpArr = newclass.split("_");
		newclass =tmpArr[0]; 
    }else if(newclass.indexOf('simplenav')>-1){
		changeClass(newclass, "on");
		newclass = "";
	}else{
	if((originalGlobalNav!=''&&originalGlobalNav.indexOf(".aspx")==-1)||PreviousNav!=null)rssval ='rss on';
	}

		clearTimeout(globalNavTimer);
		changeClass('globalnavRSS',rssval);
		changeClass('topnavcontainer', newclass);
		toggleGlobalSubnav(newclass,"block");
		if(PreviousNav!=null&&newclass!=PreviousNav)clearPreviousNav(PreviousNav)
		PreviousNav=newclass;

}

function clearPreviousNav(newclass){
	toggleGlobalSubnav(newclass,"none");
}

function setGlobalNavOff(newclass){
	globalNavTimer = setTimeout('removeGlobalNav("'+newclass+'");',500);
}

function removeGlobalNav(newclass){
	var elem = document.getElementById(originalGlobalNav);
	if(elem!=''&&elem!=undefined&&elem!=null){
			changeClass('globalnavRSS','rss on');
			setGlobalNavOn(originalGlobalNav);
	}else {
		changeClass('globalnavRSS','rss');
		toggleGlobalSubnav(newclass,"none");
		changeClass('topnavcontainer', '');
	}	
	
}



function clearGlobalTimer(){
	clearTimeout(globalNavTimer);
}

function setGlobalTimer(newclass){
	setGlobalNavOff(newclass)
}

function setupPage(){
	if(document.getElementById('EXPMENU1')!=undefined){
	
		MENUBLOCKS['EXPMENU1'] = new slideMenuDown('EXPMENU1','group1')
		MENUBLOCKS['EXPMENU2'] = new slideMenuDown('EXPMENU2','group1')
		MENUBLOCKS['EXPMENU3'] = new slideMenuDown('EXPMENU3','group1')
		MENUBLOCKS['EXPMENU4'] = new slideMenuDown('EXPMENU4','group1')
		MENUBLOCKS['EXPMENU5'] = new slideMenuDown('EXPMENU5','group1')
	
		MENUBLOCKS['EXPMENU1'].inc=MENUBLOCKS['EXPMENU1'].oinc;
		MENUBLOCKS['EXPMENU1'].moveMenu();
	
	}


	
}


  function goSearchAgain()
    {
        var srchKeyTxt = document.forms[0].srchAgainTxt.value;
        var theValidator = document.getElementById("srchKeyValidator");
        if (typeof(theValidator) != 'undefined')
        {    
            if (srchKeyTxt=="")
            {
                theValidator.innerHTML = "*required";
                return false;      
            }   
            else
                theValidator.innerHTML = "";
        }

        window.location.href = document.forms[0].srchAgainUrl.value + "?Query=" + srchKeyTxt;
		

    }

function goSearchPage()
    {

		var searchBox = document.getElementById('ttSrchTxtBox');

        var srchPageUrl = "http://www.seetorontonow.com/Visitor/Search.aspx";
        var srchKeyTxt = searchBox.value;
		//document.forms[0].searchKey.value;
		if(srchKeyTxt!="SEARCH")window.location.href = srchPageUrl + "?Query=" + srchKeyTxt;
    }
    
    //Remove the search box background image when the box gets focus.
    function removeSearchBoxBackgroundImg()
    {
        var srchBox = document.getElementById("ttSrchTxtBox")
        if (typeof srchBox != 'undefined')
        {
            srchBox.style.backgroundImage = 'url(http://www.seetorontonow.com/App_Themes/tourismtoronto/Images/searchBgOff.PNG)';                        
        }
    }


	function slideMenuDown(ID,group){
		this.ID=ID;
		this.Group = group;
		this.object = document.getElementById(ID);
		this.Style = document.getElementById(ID).style;
		this.moveMenu =moveMenu;
		this.openstate = parseInt(this.object.scrollHeight)
		this.orig = parseInt(this.Style.height)
		this.target = this.openstate;
		this.movieIt = movieIt;
		this.toggle = toggle;
		this.oinc = parseInt(this.openstate/10);
		this.inc = -1*(this.oinc);
		this.timeID = null;
		
		function toggle(){
			this.inc=this.inc*-1
			if(this.inc < 0)this.target = this.orig;
			else this.target = this.openstate; 
		
			for ( keyVar in MENUBLOCKS ){
				if(keyVar!=this.ID&&MENUBLOCKS[keyVar].Group==this.Group){
					MENUBLOCKS[keyVar].inc = -1*(MENUBLOCKS[keyVar].oinc);
					MENUBLOCKS[keyVar].target = MENUBLOCKS[keyVar].orig;
					MENUBLOCKS[keyVar].moveMenu();
				}
			}
			this.moveMenu();
		}
		
		
		function moveMenu(){
			if((parseInt(this.Style.height) < this.target&&this.target >this.orig)||(parseInt(this.Style.height) > this.target&&this.target ==this.orig)) {
				this.movieIt()
				 MENUBLOCKS[this.ID].timeID = setTimeout("MENUBLOCKS['"+this.ID+"'].moveMenu();","10");
			}//else{
				//	this.Style.height = this.target+"px";
				//}
		}
		
		function movieIt(){
		  //  document.forms[0].output.value=(this.target)+"|"+parseInt(this.Style.height)+"|"+this.inc
			if(parseInt(this.Style.height) > (this.target*.9)&&this.inc >0){
				//this.inc = (this.target - parseInt(this.Style.height))/10
				if(this.inc < 1)this.inc = 1;
			}else if(this.inc < 0&&parseInt(this.Style.height) > (this.openstate*.1) ){
			//	this.inc = -1*(this.oinc)-((parseInt(this.Style.height)-this.target)/10)
				if(this.inc < (-1*(this.oinc)))this.inc = -1*(this.oinc);
			}
			if((parseInt(this.Style.height) + this.inc)>this.target&&this.inc >0||(parseInt(this.Style.height) + this.inc) < this.target&&this.inc < 0)this.Style.height = this.target+"px";
			else this.Style.height = (parseInt(this.Style.height) + this.inc)+"px"
		}
	}
	


    var urls = new Array();
    urls["room"] = "https://www.reservations.torontotourism.com/English/availability_check.asp";
    urls["package"] = "https://www.reservations.torontotourism.com/English/Pkg_Availability_Check.asp";
    

    function GetRef(id) {
    	return (document.all) ? document.all[id] : (document.getElementById) ? document.getElementById(id) : null;
	}
	
    function showHideById(id, show) {
	    var e = GetRef(id);
		if (e == null) return;
		var display = (show) ? "block" : "none";
		e.style.display = display;
	}
	
    function SetSelectedByValue(id, val) {
        var o = GetRef(id);
        if (o == null) return;
        
        for (var i=0; i <o.options.length; i++) {
            if (o.options[i].value == val) {
                o.selectedIndex = i;
                break;
            }
        }
    }
    
    function GetSelectedValue(id) {
        var o = GetRef(id);
        if (o == null) return null;
        return o.options[o.selectedIndex].value;
    }
    
    /**
     * Populate days of month dropdown
     */
    function GetDaysInMonth(iMonth, iYear) {
        return 32 - new Date(iYear, iMonth, 32).getDate();
    } 
    
    function ClearDaysDropdown() {
        var oDays = GetRef("selDay");
        if (oDays == null) return;
        
        oDays.options.length = 0;
        
        for (var o in oDays.options) {
            oDays.remove(o);
        }
        
        
    }
    
    function PopulateDaysDropdown() {
        ClearDaysDropdown();
        
        var iYear = GetSelectedValue("selYear");
        var iMonth = GetSelectedValue("selMonth");
        var numDays = GetDaysInMonth(iMonth - 1, iYear);
        
        var oDays = GetRef("selDay");
        for (var d=1; d<=numDays; d++) {
            var o = document.createElement("option");
            o.text = d;
            o.value = d;
            try { // everyone but ie
                oDays.add(o, null);
            }
            catch (ex) { // ie
                oDays.add(o);
            }
        }
    }
     
    
    /**
     * Show and hide the dropdowns for choosing age of children
     */
    function DisplayChildrenAges(numChildren) {
        var idPrefix = "selChildAge_";
        var maxChildren = GetRef("selChildren").options.length - 1;
        
        // reset all age dropdowns
        showHideById("childrenAgeContainer", false);
        for (var i=1; i<=maxChildren; i++) {
            showHideById(idPrefix + i, false);
            
            if (i > numChildren) {
                var s = GetRef(idPrefix + i);
                s.selectedIndex = 0;
            }
        }
        
        // show the needed dropdowns
        if (numChildren > 0) {
            showHideById("childrenAgeContainer", true);
            for (var i=1; i<=numChildren; i++) {
                showHideById(idPrefix + i, true);
            }
        }
    }
    
    /**
     * callback for calendar, populates day, month, year dropdowns
     */
    function calendar_callback(cal) {
	    var dt = cal.date;
	    var y = (dt.getYear() < 1000) ? dt.getYear() + 1900 : dt.getYear();
	    
	    SetSelectedByValue("selYear", y);
	    SetSelectedByValue("selMonth", dt.getMonth() + 1);
	    SetSelectedByValue("selDay", dt.getDate());
    }
    
    /**
     * submit form for availability check
     */
    function search_submit(search_type) {
        var url = urls[search_type];
        if (url == null) return;
        
        var qstring = "?Pkg_Only_Type=&room_types=All";
        qstring += "&monthList="+ GetSelectedValue("selMonth");
        qstring += "&daysList="+ GetSelectedValue("selDay");
        qstring += "&yearList="+ GetSelectedValue("selYear");
        qstring += "&no_nights="+ GetSelectedValue("selNights");
        qstring += "&no_rooms="+ GetSelectedValue("selRooms");
        qstring += "&no_adult="+ GetSelectedValue("selAdults");
        
        var oChildren = GetRef("selChildren");
        var numChildren = GetSelectedValue("selChildren");
        var maxChildren = oChildren.options[oChildren.options.length - 1].value;
        
        var validAges = 0;
        var qstringAge = "";
        for (var i=1; i<=maxChildren; i++) {
            var val = GetSelectedValue("selChildAge_" + i);
            var age = -1;
            if (val > 0) {
                age = val;
                validAges++;
            }
            qstringAge += "&opt_child_"+ (i - 1) +"="+ age;
        }
        
        qstring += "&no_child="+ validAges + qstringAge;
        
        //alert(url + qstring);
        window.open(url + qstring);
    }





function toTitleCase(str)
{
    return str.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
}

function popup(namep, url, h, w, opts){
	window.open( url, namep, "status = 0, height = "+645+", width = "+570+", "+opts )
}

function popupLegal(namep, url, h, w, opts){
	window.open( url, namep, "status = 0, height = "+645+", width = "+w+", "+opts )
}

function SubmitCapturedMessage(pageDisplayName, submitUrl) 
    {
		var pageUrl = "";
		var pageTitle = "";
        if (canGetwindowLocation() && canGetDocumentTitle() && canGetElementById())
        {
        	// Retrieve page URL and title
            pageUrl = window.location.href + ""; 
            pageTitle = document.title + "";    
        }
		
		if (pageUrl != "" && pageTitle != "" && pageUrl.toLowerCase().indexOf("?region=")==-1)
		{
		    if (pageDisplayName=="")
		        if(pageUrl.toLowerCase().indexOf("listing.aspx")>-1){
					pageDisplayName = pageTitle;
				}else{  
					var tmp = pageUrl.split("/");
					var tmpname = tmp[tmp.length-1].replace(/-/g," ");
					pageDisplayName = toTitleCase(tmpname.toLowerCase().replace(".aspx",""));
		  		}
			// Ajax call to backend
			$.get(submitUrl, {
				theURL:         pageUrl,
				title:          pageTitle,
				displayName:    pageDisplayName
			});
		}
    }

function readLess(rm,rl,obj,ID){
	var d = document.getElementById(ID)

	if(d!=undefined&&d!=null){
		if(d.innerHTML == rm)d.innerHTML = rl;
		else if(d.innerHTML == rl)d.innerHTML = rm;
	}

}


var singlePopWin = null;
function closePopWin()
{
	if (singlePopWin != null)
	{
     	if (!singlePopWin.closed)
         	singlePopWin.close();
        singlePopWin = null;
    }
}


