/*-------------------------------------------------------------------------
Fixes Up the Template (used in <body onload="fixTemplate();">)
-------------------------------------------------------------------------*/
var _isLyteboxInitialized = true;
function fixTemplate(){
	
	if(isExplorer()) debugExplorer(); // debugs template for Explorer 6
	if((window.location.search).indexOf('testing=1') > -1) tripIdeasLoad(); // loads the Trip Ideas tab for testing
	
}
var _cttcSubNavigationImage = new Image;
_cttcSubNavigationImage.src = '/_images/subNavigation.png';

/*-------------------------------------------------------------------------
Survey
-------------------------------------------------------------------------*/

var _cttcSurveyPrompt = {
	ask : false,
	flush : false,
	init : function(){
		if(_cttcSurveyPrompt.flush) _cttcSurveyPrompt.removeCookie();
		if(_cttcSurveyPrompt.ask) _cttcSurveyPrompt.popUp();
	},
	popUp : function(){
		_cttcSurveyPrompt.setCookie(); 
		window.open('')
	},
	initObsolete : function(){
		if(_cttcSurveyPrompt.flush) _cttcSurveyPrompt.removeCookie();
		if(_cttcSurveyPrompt.ask) _cttcSurveyPrompt.display()
	},
	display : function(){
		_cttcSurveyPrompt.lyteboxOverlay.show();
		window.scrollTo(0,0);
		var e = document.createElement('div');
		e.id = 'cttcSurveyPromptWindow';
		e.className = 'surveyPrompt';
		e.innerHTML = '';
		document.body.appendChild(e);
	},
	click : function(b){
		_cttcSurveyPrompt.setCookie();
		if( (b.alt).toLowerCase() == 'yes') _cttcSurveyPrompt.agree();
		else _cttcSurveyPrompt.disagree();
	},
	mouseover : function(b){ b.src = (b.src).replace('.png','_over.png'); },
	mouseout : function(b){ b.src = (b.src).replace('_over.png','.png'); },
	hide : function(){
		var o = grabElem('cttcSurveyPromptWindow');
		o.parentNode.removeChild(o);
		_cttcSurveyPrompt.lyteboxOverlay.hide();
	},
	agree : function(){
		window.open('');
		_cttcSurveyPrompt.hide();
	},
	disagree : function(){
		grabElem('cttcSurveyPromptWindow').innerHTML = '';
		setTimeout(_cttcSurveyPrompt.hide, 4000);
	},
	removeCookie : function(){
		if ( (document.cookie.toUpperCase()).indexOf(_cttcSurveyPrompt.cookieName) > -1 ) document.cookie = _cttcSurveyPrompt.cookieName + "=;expires=Thu, 01-Jan-1970 00:00:01 GMT";
	},
	setCookie : function(){
		document.cookie = _cttcSurveyPrompt.cookieName + "=true"; 
	},
	cookieName : 'CTTCSURVEYPROMPTED',
	lyteboxOverlay : {
		show : function(){
			var pageSize	= myLytebox.getPageSize();
			var objOverlay	= myLytebox.doc.getElementById('lbOverlay');
			var objBody		= myLytebox.doc.getElementsByTagName("body").item(0);
			objOverlay.style.height = pageSize[1] + "px";
			objOverlay.style.display = '';
			myLytebox.appear('lbOverlay', myLytebox.maxOpacity);
			myLytebox.toggleSelects('');
			myLytebox.toggleFlash('');
		},
		hide : function(){
			myLytebox.fade('lbOverlay', 0);
			myLytebox.toggleSelects('visible');
			myLytebox.toggleFlash('visible');
		}
	}
}

/*-------------------------------------------------------------------------
Explorer Fixes for Template and Widgets (used only for IE 6)
-------------------------------------------------------------------------*/

function debugExplorer(){
	
	var navs = new Array('explorecalifornia','lifestyle','thingstodo','traveltools','deals');
	for(i = 0; i < navs.length; i++){
		navigationAttachRollovers(navs[i]);
	}
	
	var sW = grabElem('largesearchwidget');
	if(sW){
		var p = sW.parentNode;
		p.style.paddingTop = '8px'
		p.style.height = '100px';
		p.style.position = 'relative';
		sW.style.position = 'absolute';
	}
	
	
	var a = document.all;
	for(i = 0; i < a.length; i++){
		theStyle = 'columns';
		if(
			 ( (window.location.search).toLowerCase() ).indexOf('section=deals') > -1 &&
			 a[i].tagName == 'TABLE' && parseFloat(a[i].cellPadding) == 10
			){
			
			a[i].cellPadding = 0;
			
		}else if(a[i].className && a[i].className == 'deals_tan_box'){
		
			a[i].style.margin = '16px 0px 16px 0px';
		
		}else if(a[i].tagName == 'UL' && a[i].className == 'areas'){
			
			var lis = a[i].getElementsByTagName('li');
			lis[0].className = 'areasInclude';
			
		}else if(a[i].tagName == 'IMG' && ((a[i].src).toLowerCase()).indexOf('.png') > -1 ){
		
			var tempSrc = a[i].src;
			a[i].style.width = a[i].clientWidth;
			a[i].style.height = a[i].clientHeight;
			a[i].style.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=\'true\',sizingMethod=crop src="' + tempSrc + '")';
			a[i].src = '/_images/holder.gif';
		
		}else if(a[i].className && (a[i].className).indexOf(theStyle) > -1){
			
			for(s = 0; s < a[i].childNodes.length; s++){
				sE = a[i].childNodes[s];
				// loop through divs only
				if(
					 sE.tagName && (sE.tagName).indexOf('DIV') > -1 && 
					 sE.parentNode.className && 
					 (
						(sE.parentNode.className).indexOf(theStyle) > -1 &&
					 	(sE.parentNode.className).indexOf(theStyle + 'Child') == -1
					 )
					){
					// change styles to childfirst to target directly with css
					switch(sE.className){
						case "first": 
							sE.className = theStyle + 'ChildFirst';
						break;
						case "middle":
							sE.className = theStyle + 'ChildMiddle';
						break;
						case "last": 
							sE.className = theStyle + 'ChildLast';
						break;
					}
					// remove margin from initial grid
					if((sE.parentNode.parentNode.parentNode.parentNode.className).indexOf('columns') > -1) sE.parentNode.parentNode.parentNode.parentNode.style.marginTop = '0px';
					// 
					if((sE.parentNode.className).indexOf('height') > -1){
						parentClasses = (sE.parentNode.className).split(' ');
						grabHeight = 0;
						for(cI = 0; cI < parentClasses.length; cI++){
							cc = parentClasses[cI];
							if(cc.indexOf('height') > -1) grabHeight = parseInt(cc.replace('height',''));
						}
						if(grabHeight > 0) sE.style.height = grabHeight + 'px';
					}
					
				}
			}
		
		}
	}
	
	fixTravelTools.init();
	
	var tB = grabElem('cttcBody');
	tB.style.visibility = 'visible';
	
}

function debuggedIEBg(img){
	if(!isExplorer()) return 'background-image:url(' + img + ');';
	else return "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled='true',sizingMethod=crop src='" + img + "');";
}


function navigationAttachRollovers(theId){
	e = grabElem(theId);
	if(e && e.parentNode.childNodes.length >= 3){
		e.onmouseover = function(){this.parentNode.childNodes[2].style.display = 'block';}
		e.onmouseout = function(){this.parentNode.childNodes[2].style.display = 'none';}
		e.parentNode.childNodes[2].onmouseover = function(){this.style.display = 'block';}
		e.parentNode.childNodes[2].onmouseout = function(){this.style.display = 'none';}
	}
}

var fixTravelTools = {
	init : function(){
		ajaxPage('/widget/EventsSmall.aspx?TOP=10', function(r){
			var fArr = document.getElementsByTagName('iframe');
			var tT = new Array();
			for(var xa = 0; xa < fArr.length; xa++) if(fArr[xa].name == 'wid-events') tT[tT.length] = fArr[xa];
			if(tT.length > 0){
				var newLi, newLink;
				var tempDiv = document.createElement('div');
				tempDiv.innerHTML = r;
				var links = tempDiv.getElementsByTagName('a');
				var newDiv = document.createElement('div');
				newDiv.className = 'debuggedTravelTools';
				var newUl = document.createElement('ul');
				newUl.innerHTML = '<li class="header">Events</li>';
				newDiv.appendChild(newUl);
				for(var lx = 0; lx < links.length; lx++){
					if(links[lx].getAttribute('rel')){
						newLi = document.createElement('li');
						newUl.appendChild(newLi);
						newLink = document.createElement('a');
						newLi.appendChild(newLink);
						newLink.href = "javascript:fixTravelTools.linkTo('" + links[lx].href + "')";
						newLink.innerHTML = links[lx].innerHTML;
					}
				}
				var imgLinks = isExplorer() ? '<img src="/_images/eventsCornerTop.gif" class="c1" /><img src="/_images/eventsCornerBottom.gif" style="margin-bottom:5px;" class="c2" />' : '';
				newDiv.innerHTML += '<a class="more" href="/modules/findyourself.aspx?activity=Events&tab=Events" target="_parent">More events...</a><div class="s1"><img src="/_images/holder.gif" /></div><div class="s2"><img src="/_images/holder.gif" /></div>' + imgLinks;
				tT[0].parentNode.parentNode.appendChild(newDiv);
				tT[0].parentNode.parentNode.removeChild(tT[0].parentNode);
			}
		});
	},
	linkTo : function(l){
		var o = {
			href: l,
			rev: "width:800px;height:500px;scrolling:yes;",
			title: "",
			rel: "lyteframe",
			getAttribute: function(a){
				return o[a];
			}
		}
		myLytebox.start(o, false, true);
	}
}

/*-------------------------------------------------------------------------
Applies Custom Scrollbars - usage: <div class="scrollbars"><table></table></div>
-------------------------------------------------------------------------*/

var scrollingElement, scrollingAction, keepScrolling, scrollMouseOn;
function scrollElem(){
	eId = scrollingElement;
	action = scrollingAction;
	e = grabElem(eId);
	e.style.position = 'absolute';
	e.style.zIndex = 1;
	for(i = 0; i < e.parentNode.childNodes.length; i++){
		if(e.parentNode.childNodes[i].tagName == 'A'){
			e.parentNode.childNodes[i].style.zIndex = 2;
		}
	}
	if(keepScrolling == true){
			diff = action == 'up' ? -5 : 5;
	}else{
		diff = e.parentNode.clientHeight - 50;
		diff = action == 'up' ? -1 * diff : diff;
	}
	newY = e.offsetTop + diff;
	minScroll = 25;
	maxScroll = e.offsetHeight * -1 + e.parentNode.offsetHeight - 25;
	if(newY < maxScroll && action == 'up'){
		newY = maxScroll;
	}else if(newY > minScroll && action == 'down'){
		newY = minScroll;
	}
	if( (action == 'up' && e.offsetTop > maxScroll ) || (action == 'down' && e.offsetTop < minScroll)){
		e.style.top = newY + 'px';
		if(keepScrolling == true && scrollMouseOn == true) setTimeout(scrollElem, 50);
	}
}
function scrollUp(eId,kS){scrollMouseOn=true;scrollingElement=eId;scrollingAction='up';keepScrolling=kS;scrollElem();}
function scrollDown(eId,kS){scrollMouseOn=true;scrollingElement=eId;scrollingAction='down';keepScrolling=kS;scrollElem();}
function flickerImage(){}

/*-------------------------------------------------------------------------
Obtains HTML Contents Via AJAX - usage: <div class="ajax">http://www.google.com</div>
-------------------------------------------------------------------------*/

var currentAjax = 0;
var rootDomain="http://"+window.location['hostname'];
function loadAjax(){
	var bod = grabElem('cttcBody');
	if(bod){
		var aT = bod.getElementsByTagName('DIV');
		for(i = 0; i < aT.length; i++){
			if(aT[i].className == 'ajax' || (aT[i].className).indexOf('ajax') > -1){
				theURL = aT[i].innerHTML;
				if(theURL.indexOf('http') == -1)theURL = rootDomain + theURL;
				currentAjax = currentAjax + 1;
				if(aT[i].id == ''){
					newID = 'ajaxObject' + currentAjax;
					aT[i].id = newID;
				}else{
					newID = aT[i].id;
				}
				aT[i].innerHTML = '';
				aT[i].style.height = aT[i].parentNode.style.height;
				aT[i].style.width = aT[i].parentNode.style.width;
				aT[i].style.display='block';
				try{
					ajaxPage(theURL,newID);
				}catch(err){
					// do nothing with this widget
				}
			}
		}
	}
}
/*USAGE: ajaxPage('ajaxfiles/external3.htm', 'rightcolumn');*/
function ajaxPage(url, nxt){
	var page_request = false;
	if(window.XMLHttpRequest){
		/*if Mozilla, Safari etc*/
		page_request = new XMLHttpRequest();
	}else if(window.ActiveXObject){
		/*if IE*/
		try{
			page_request = new ActiveXObject("Msxml2.XMLHTTP");
		}catch(e){
			try{
				page_request = new ActiveXObject("Microsoft.XMLHTTP");
			}catch(e){
			}
		}
	}else{
		return false;
	}
	page_request.onreadystatechange = function(){
		if(page_request.readyState == 4 && (page_request.status == 200 || window.location.href.indexOf("http") == -1)){
			if(typeof(nxt) == 'function') nxt(page_request.responseText);
			else writeAjaxContent(page_request, nxt);
		}
	}
	page_request.open('GET', url, true);
	page_request.send(null);
}
function writeAjaxContent(page_request, containerid, toWrite){
	ajaxedElem = grabElem(containerid);
	ajaxedElem.innerHTML = page_request.responseText;
	parseTripIdeaItems(ajaxedElem);
}

/*-------------------------------------------------------------------------
Detects for Explorer 6 (simply returns true/false on IE6) - usage: if(isExplorer()){}
-------------------------------------------------------------------------*/

function isExplorer(){return ( (navigator.appName).indexOf('Explorer') > -1 && (navigator.appVersion).indexOf('MSIE 6') > -1 ) ? true : false;}

/*-------------------------------------------------------------------------
Detects for Explorer 7 (simply returns true/false on IE6) - usage: if(isExplorer()){}
-------------------------------------------------------------------------*/

function isExplorer7(){return ( (navigator.appName).indexOf('Explorer') > -1 && (navigator.appVersion).indexOf('MSIE 7') > -1 ) ? true : false;}

/*-------------------------------------------------------------------------
Grabs an Element (supports different browsers) - usage: e=grabElem('theId');
-------------------------------------------------------------------------*/

function grabElem(theObj,theDoc){var p,i,foundObj;if(!theDoc)theDoc=document;if((p=theObj.indexOf("?"))>0&&parent.frames.length){theDoc=parent.frames[theObj.substring(p+1)].document;theObj=theObj.substring(0,p);}if(!(foundObj=theDoc[theObj])&&theDoc.all)foundObj=theDoc.all[theObj];for(i=0;!foundObj&&i<theDoc.forms.length; i++)foundObj=theDoc.forms[i][theObj];for(i=0;!foundObj&&theDoc.layers&&i<theDoc.layers.length;i++)foundObj=findObj(theObj,theDoc.layers[i].document);if(!foundObj&&document.getElementById) foundObj = document.getElementById(theObj);return foundObj;}

/*-------------------------------------------------------------------------
Obtains Coordinates and Sizes of Objects - usage: e.style.top=getElementTop('theId')+'px';
-------------------------------------------------------------------------*/

function getElementTop(Elem){if(document.getElementById){var elem=document.getElementById(Elem);}else if(document.all){var elem=document.all[Elem];}yPos=elem.offsetTop;tempEl=elem.offsetParent;while(tempEl!=null){yPos+=tempEl.offsetTop;tempEl=tempEl.offsetParent;}return yPos;}
function getElementHeight(Elem){var elem;if(document.getElementById){var elem=document.getElementById(Elem);}else if (document.all){var elem=document.all[Elem];}if((navigator.userAgent.indexOf("Opera 5")!=-1)||(navigator.userAgent.indexOf("Opera/5")!=-1)){xPos = elem.style.pixelHeight;}else{xPos=elem.offsetHeight;}return xPos;}
function getElementLeft(Elem){var elem;if(document.getElementById){var elem=document.getElementById(Elem);}else if(document.all){var elem=document.all[Elem];}xPos=elem.offsetLeft;tempEl=elem.offsetParent;while(tempEl!=null){xPos+=tempEl.offsetLeft;tempEl=tempEl.offsetParent;}return xPos;}
function getElementWidth(Elem){var elem;if(document.getElementById){var elem=document.getElementById(Elem);}else if(document.all){var elem=document.all[Elem];}if((navigator.userAgent.indexOf("Opera 5")!=-1)||(navigator.userAgent.indexOf("Opera/5")!=-1)){xPos=elem.style.pixelWidth;}else{xPos=elem.offsetWidth;}return xPos;}


