//load xml on load in browse and tour parrallel to regular load

//contains xml sheet of selected and related tours
var XMLBrowse;

//map vars
var map;
var marker = new Array(); //map marker

addLoadListener(initAll);
window.unload = unloadAll;

function initAll(){	
	refillField("search");
	AttachEvent(document.getElementById('selectCollection'),'change', selectCollection, false);
	//Drop Down Buttons
	
	//show newsletter popup note
	//initNewsletter();
}

function initLegal(){	
	
	// tab functions
	initTabs("tcTab", "tcTab", false);
	document.getElementById('tcTab').onclick = showTab;
	document.getElementById('disclaimerTab').onclick = showTab;
	
}

function initSalesHome(){	
	//show Whistler Wish List popup
	document.getElementById('whistlerWishListButton').onclick = function()
	{
		popupOn( document.getElementById('whistlerWishListForm').parentNode );
		return false;
	}
	
}

function initSalesRates(){	
	//show Whistler Wish List popup
	document.getElementById('getYourQuoteButton').onclick = function()
	{
		popupOn( document.getElementById('getYourQuoteForm').parentNode );
		return false;
	}
	
}

function initServiceDetails(){
	initTabs("photographyTab", "photographyTab", false);
	document.getElementById('photographyTab').onclick = showTab;
	document.getElementById('toursTab').onclick = showTab;
	document.getElementById('platformTab').onclick = showTab;
}

function initSalesFaq(){	
	//show Whistler Wish List popup
	document.getElementById('addYourQuestionButton').onclick = function()
	{
		popupOn( document.getElementById('addYourQuestionForm').parentNode );
		return false;
	}
	
}

function initSalesLuxuryRealty(){	
	//show Whistler Wish List popup
	document.getElementById('orderButton').onclick = function()
	{
		popupOn( document.getElementById('luxuryRealtyOrderForm').parentNode );
		return false;
	}
	
}

function initBrowse(){	
	//init XMLBrowse
	initXMLBrowse();
	
	// tab functions
	initTabs("listTab", "listTab", false);
	document.getElementById('mapTab').onclick = function(){
		showTab('mapTab');
		GLoad();
		return false;
	};
	document.getElementById('listTab').onclick = function(){
		showTab('listTab');
		GUnload();
		return false;
	};
	
}

function initTour(){	
	//NOTE: Map init in 'initAjaxRelatedTours' after xml Browse is loaded
	
	// tab functions
	//initTabs("vtTab", "vtTab", false);
	initPanoThumbs();
	
	initTabs("shareTab", "shareTab", false);
	document.getElementById('shareTab').onclick = showTab;
	document.getElementById('embedTab').onclick = openEmbed;
	
	//show/hide function for map tab
	/*document.getElementById('embedTab').onclick = function(){
		showTab('embedTab');
		GUnload();
		mapLoaded = false;
		return false;
	};
	document.getElementById('mapTab').onclick = function(){
		showTab('mapTab');
		GLoad('mapTabData');
		return false;
	};
	*/

	//auto select all input fields
	document.getElementById('linkCopy').onclick = selectAll;
	//---------------------	
	
	initTabs("linkTab", false, true);
	document.getElementById('linkTab').onclick = showTab;
	
	//auto select all input fields
	document.getElementById('embedWidth').onclick = selectAll;
	document.getElementById('embedHeight').onclick = selectAll;
	document.getElementById('linkEmbed').onclick = selectAll;
	//---------------------	
	//change size values of embed string
	document.getElementById('embedWidth').onkeyup = changeEmbedSize;
	document.getElementById('embedHeight').onkeyup = changeEmbedSize;
	//---------------------	
	
	//show more map
	document.getElementById('biggerMapLink').onclick = function(){
		showFullContent('mapTabData', 'biggerMapLink', 550);
	}
	
	//show related tours popup note
	//ajaxPopupChecker('browse', null,document.getElementById('relatedToursContainer').getElementsByTagName("DIV")[0] );

}
// Drop Down Menu
//--------------------------------------------------
//function embeded in div and li tags
function dropDownOn(element) {
	//if(document.all && document.getElementById){
	element.getElementsByTagName("ul")[0].style.display = "block";
	//}
};
function dropDownOff(element) {
	//if(document.all && document.getElementById){
	element.getElementsByTagName("ul")[0].style.display = "none";
	//}
};

// Popup
//--------------------------------------------------
//function embeded in tags
function popupOn(element, popup) {
	element.getElementsByTagName("div")[0].style.display = "block";
	//close button function
	if(typeof popup != 'undefined'){
		ajaxPopupChecker(popup, 1);
	}
	try{
		var closeButton = element.getElementsByTagName("div")[0].getElementsByTagName("A")[0];
		var pattern = new RegExp("(^| )popupClose( |$)");
		if(pattern.test(closeButton.className)){
			closeButton.onclick = function(){ popupOff(element, popup); return false; }
		}
	}catch(e){
		
	}
	return false;
};
function  popupOff(element, popup) {
	element.getElementsByTagName("div")[0].style.display = "none";
	
	if(typeof popup != 'undefined'){
		ajaxPopupChecker(popup, 0);
	}
	
	return false;
};

// Load and unlad functions START
//--------------------------------------------------
function unloadAll(){
 	GUnload();
	mapLoaded = false;	
}

function addLoadListener(fn) {
	if (typeof window.addEventListener != "undefined")
	{
		window.addEventListener("load", fn, false);
	}
	else if (typeof document.addEventListener != "undefined")
	{
		document.addEventListener("load", fn, false);
	}
	else if (typeof window.attachEvent != "undefined")
	{
		window.attachEvent("onload", fn);
	}
	else
	{
		var oldfn=window.onload;
		if (typeof window.onload != "function") 
		{
			window.onload = fn; 
		}
		else 
		{
			window.onload = function() 
			{ 
				oldfn(); 
				fn();
			};
		}
	}
}

// tab functions START
//--------------------------------------------------
function initTabs(initId, openTab, allowClose)
{

	//write Tab config into parent container of tabs group
	thisE = document.getElementById(initId).parentNode;
	thisE.tabConfig = new Object();
	thisE.tabConfig.openTab = 'none';
	thisE.tabConfig.allowClose = allowClose;
	thisE.tabConfig.tabClass = document.getElementById(initId).className;
	
	if(openTab != false){
		showTab(openTab);
	}

}

function show(layerId) { 
	document.getElementById(layerId).style.display = '';
} 

function hide(layerId) { 
	document.getElementById(layerId).style.display = 'none';
}	

function showTab(id) {
	
	if(this.tagName == "A" || this.tagName == "a"){
		//runs if function is attached to link element
		id		= this.id;
	}
	
	thisE			= document.getElementById(id)
	thisTab 		= thisE.id;
	openTab 		= thisE.parentNode.tabConfig.openTab;
	allowClose 		= thisE.parentNode.tabConfig.allowClose;
	tabClass 		= thisE.parentNode.tabConfig.tabClass;

	if(thisTab == openTab && allowClose == true){
		//hides tab if active
		thisE.className = tabClass;
		hide(thisTab +'Data');
		thisE.parentNode.tabConfig.openTab = 'none';
	
	}else{

		//shows next tab
		if(openTab!='none'){
			document.getElementById(openTab).className = tabClass
			hide(openTab + 'Data');
		}
		thisE.className = tabClass + 'Active';
		
		show(thisTab + 'Data');
		thisE.parentNode.tabConfig.openTab = thisTab;
	}
	thisE.blur();
	return false;
}
// more info function START
//--------------------------------------------------
function showFullContent(target, thisE, fullSize)
{	
	//write Tab config into parent container of tabs group
	//using name value of target div, to toggle and safe current status, as safari can't read style.overflow value

	thisE = document.getElementById(thisE);
	var target = document.getElementById(target);
	if( target.className != target.name){
		target.style.overflow = "visible";
		
		target.originalHeight = target.style.height;
		target.style.height = "auto";
		if(typeof fullSize == 'number'){
			target.style.height = fullSize + "px";
		}
		
		thisE.textContent = "less...";
		
		target.name = target.className;
	}else{
		target.style.overflow = "hidden";
		
		target.style.height = target.originalHeight;
		
		thisE.textContent = "more...";
		
		target.name = target.className + " closed";
	}
}

// pano thumb functions START
//--------------------------------------------------
function initPanoThumbs()
{
	//only init, if thumb holder exist
	if(document.getElementById('panoThumbHolder')){
		var thisE = document.getElementById('panoThumbHolder')
		thisE.tabConfig = new Object();
		thisE.tabConfig.openTab = thisE.title;
		thisE.title = ""; //prevents browsers from showing cryptic title
		
		thumbMarkShow(thisE.tabConfig.openTab);
		
	}
	//write Tab config into parent container of tabs group

}

function thumbMarkShow(layerId) { 
	document.getElementById(layerId).className = 'active';
} 

function thumbMarkHide(layerId) { 
	document.getElementById(layerId).className = '';
}	

function thumbMarkActive(elementId) {
		
	var thisE			= document.getElementById(elementId);
	var thisTab 		= thisE.id;
	var openTab 		= thisE.parentNode.tabConfig.openTab;
	
	thumbMarkHide(openTab);
	thumbMarkShow(thisTab);
	thisE.parentNode.tabConfig.openTab = thisTab;
	
	thisE.blur();
	return false;
}
// Flash Popup functions - not inplace because of mozilla resize reload bug
//--------------------------------------------------
function panoPopup(flag)
{
	thisE = document.getElementById("vtTabData");
	
	if(flag == "true")
	{
		thisE.className = "panoPopup";
	}else
	{
		thisE.className = "";
	}
}
// Input field functions
//--------------------------------------------------
function selectAll()
{
	var element = this;
	element.focus();
	element.select();	
}
function changeEmbedSize()
{
	var target = document.getElementById('linkEmbed');
	var inputString = target.value;
	var newValue = 	this.value.replace(/[^0-9]*/g, "");//eliminate everything but numbers
	
	
	if(this.id == "embedWidth"){
		parseInt(newValue) > 600 ? newValue = 600 : newValue;
		//parseInt(newValue) <= 0 ? newValue = 600 : newValue;
		target.value = inputString.replace(/width="[0-9]*/g, "width=\"" + newValue);
	}else{
		parseInt(newValue) > 400 ? newValue = 400 : newValue;
		//parseInt(newValue) <= 0 ? newValue = 400 : newValue;
		target.value = inputString.replace(/height="[0-9]*/g, "height=\"" + newValue);
	}
	this.value = newValue;//replace fixed input string

}
// Select Collection functions
//--------------------------------------------------
function selectCollection()
{
	var element = document.getElementById("selectCollection");
	var collection = element.options[element.selectedIndex].value;
	//alert(keyword);
	
	if(collection != '')
	{
		if(document.collectionsForm.action == ''){
			document.collectionsForm.action = collection;
		}
		document.collectionsForm.submit();
	}else{
		element.selectedIndex = 0;
	}
	
}
// Refill field functions
//--------------------------------------------------
function refillField(id)
{
	var element = document.getElementById(id);
	
	AttachEvent(element,'focus',myFocus,false);
	AttachEvent(element,'blur',myBlur,false);
	
	function myFocus() {
		if (element.value == element.defaultValue) {
			element.value = '';
		}
	}
	function myBlur() {
		if (element.value == '') {
			element.value = element.defaultValue;
		}
	}
}
// GMap functions
//--------------------------------------------------
function showMapMarker(id)
{
	if(mapLoaded == true){
		GEvent.trigger(marker[id],'click');		
	}else{
		showTab('mapTab');
		GLoad("mapTabData", id);		
	}
	return false;
}
// Popup functions
//--------------------------------------------------
function openBrWindow(pFile, pWindow, pBreite, pHoehe) { //v2.0
	if(window.open(pFile, pWindow,'toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=no,width='+pBreite+',height='+pHoehe+', left='+(Math.round((screen.width-pBreite)/2))+',top='+(Math.round((screen.height-pHoehe)/2)))){
		return true;
	}else{
		return false;
	};
}

function openBrWindowResize(pFile, pWindow, pBreite, pHoehe) { //v2.0
	window.open(pFile, pWindow,'toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width='+pBreite+',height='+pHoehe+', left='+(Math.round((screen.width-pBreite)/2))+',top='+(Math.round((screen.height-pHoehe)/2)));
}
// Share functions
//--------------------------------------------------
function fbs_click() 
{
	u=location.href;
	t=document.title;
	window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');
	return false;
}

// Attach Event functions (external)
//--------------------------------------------------
//*** This code is copyright 2003 by Gavin Kistner, gavin@refinery.com
//*** It is covered under the license viewable at http://phrogz.net/JS/_ReuseLicense.txt
//*** Reuse or modification is free provided you abide by the terms of that license.
//*** (Including the first two lines above in your source code satisfies the conditions.)


//***Cross browser attach event function. For 'evt' pass a string value with the leading "on" omitted
//***e.g. AttachEvent(window,'load',MyFunctionNameWithoutParenthesis,false);

function AttachEvent(obj,evt,fnc,useCapture){
	if (!useCapture) useCapture=false;
	if (obj.addEventListener){
		obj.addEventListener(evt,fnc,useCapture);
		return true;
	} else if (obj.attachEvent) return obj.attachEvent("on"+evt,fnc);
	else{
		MyAttachEvent(obj,evt,fnc);
		obj['on'+evt]=function(){ MyFireEvent(obj,evt) };
	}
}