/**
 *  Common javascript for Royal Caribbean 
 *  
 *  @version 1.0
 *	@author Edalene Tay
 *  @author Kien Wai
 */

function init(){
	setBg();
	setNav();
//	if (section == "packages") {
//		setSelected();
//	}
//	else 
	if(section == "home") {
	    initTabs();
		//setSelectedTab();
		//initBanner();
		
	}
	
}

function setBg(){
	var metaCollection = $$(document).elmsByTag("meta");
	var lang = $$(document).elmsByTag("meta")[metaCollection.length-1].content;
	
	switch(section){
		case "home":
/*			$(document.body).setAttributes({
				background: "../" + lang + "/images/_common/bg-01.jpg"
			});
*/			
			$(document.body).attr('background', '/App_Themes/RCI_' + lang + '/images/_common/bg-01.jpg');
			
			break;
		case "experience":
		case "before-cruise":
		case "journal":
/*			$(document.body).setAttributes({
				background: "../" + lang + "/images/_common/bg-03.jpg"
			});
*/			
			$(document.body).attr('background', '/App_Themes/RCI_' + lang + '/images/_common/bg-03.jpg');
			break;
		case "packages":
		case "crown-anchor":
/*			$(document.body).setAttributes({
				background: "../" + lang + "/images/_common/bg-04.jpg"
			});
*/			
			$(document.body).attr('background', '/App_Themes/RCI_' + lang + '/images/_common/bg-04.jpg');
			
			break;
		case "fleet":
		case "about-us":
		case "customer-service":
/*			$(document.body).setAttributes({
				background: "../" + lang + "/images/_common/bg-05.jpg"
			});
			
*/			$(document.body).attr('background', '/App_Themes/RCI_' + lang + '/images/_common/bg-05.jpg');
			break;
		default:
			break;
	}
}

/*function setBg(){alert("hello! " + section);
	var metaCollection = $$(document).elmsByTag("meta");
	var lang = $$(document).elmsByTag("meta")[metaCollection.length-1].content;
    
	//var lang = $$(document).elmsByTag("meta")[2].content;
	switch(section){
		case "home":
			$(document.body).setAttributes({
				background: "/App_Themes/RCI_" + lang + "/images/_common/bg-01.jpg"
			});
			break;
		case "experience":
		case "journal":
		case "before-cruise":
			$(document.body).setAttributes({
				background: "/App_Themes/RCI_" + lang + "/images/_common/bg-03.jpg"
			});
			break;
		case "packages":
		case "crown-anchor":
			$(document.body).setAttributes({
				background: "/App_Themes/RCI_" + lang + "/images/_common/bg-04.jpg"
			});
			break;
		case "fleet":
		case "about-us":
		case "customer-service":
			$(document.body).setAttributes({
				background: "/App_Themes/RCI_" + lang + "/images/_common/bg-05.jpg"
			});
			break;
		default:
			break;
	}
}*/


/*
function showToolTip(id){
	$$(id).removeClass("cta-tool-hidden");
	$$(id).addClass("cta-tool-show");
}


function hideToolTip(id){
	$$(id).removeClass("cta-tool-show");
	$$(id).addClass("cta-tool-hidden");
}*/


/*function setSelectedTab(){
	$$(tabName+"-l").removeClass("left-corner");
	$$(tabName+"-m").removeClass("mid-area");
	$$(tabName+"-r").removeClass("right-corner");
	$$(tabName+"-l").addClass("selected-l");
	$$(tabName+"-m").addClass("selected-m");
	$$(tabName+"-r").addClass("selected-r");
	
	$$("content-"+tabName).removeClass("hide");
}*/


function setSelected() {
	$$(pageName).removeClass("pkg-listing-item");
	$$(pageName).addClass("selected");
}

function initTabs(){
	$$("tabs").elmsByTag("li").addEvent("click", setSelectedTab);
}

function setSelectedTab(){
	if(prevTab != null){
		$$(prevTab).removeClass("tab-selected");
		$$(prevTab+"-tab").addClass("hide");
		$$(prevTab+"-tab").removeClass("show");	
		
		$$(this.id).addClass("tab-selected");
		$$(this.id+"-tab").removeClass("hide");
		$$(this.id+"-tab").addClass("show");
		
		prevTab = this.id;
	}else{
		prevTab = "asia";
		
		$$(prevTab).addClass("tab-selected");
		$$(prevTab+"-tab").removeClass("hide");
		$$(prevTab+"-tab").addClass("show");
		
	}	
}


function setNav(){
	if(section != "home"){
			switch(section){
				case "experience":
				case "packages":
				case "fleet":
				case "about-us":
				case "before-cruise":
				case "journal":
					$$(section).addClass("navselect");
					break;
				case "crown-anchor":
					$$("crown").addClass("navselect");
					break;
				case "customer-service":
					$$("cust-service").addClass("navselect");
					break;
			}
	}
}


function openSurvey(){
	
	if(surveyIsDisplayed == true){
		$$("survey").addClass("hide");
		$$("survey-open").addClass("hide");
		$$("survey-closed").removeClass("hide");
	}
	else{
		$$("survey").removeClass("hide");
		$$("survey-open").removeClass("hide");
		$$("survey-closed").addClass("hide");
	}
	
	surveyIsDisplayed = !surveyIsDisplayed;
}


QTObject = function(mov, id, w, h) {
	this.mov = mov;
	this.id = id;
	this.width = w;
	this.height = h;
	this.redirect = "";
	this.sq = document.location.search.split("?")[1] || "";
	this.altTxt = "This content requires the QuickTime Plugin. <a href='http://www.apple.com/quicktime/download/'>Download QuickTime Player</a>.";
	this.bypassTxt = "<p>Already have QuickTime Player? <a href='?detectqt=false&"+ this.sq +"'>Click here.</a></p>";
	this.params = new Object();
	this.doDetect = getQueryParamValue('detectqt');
}


QTObject.prototype.addParam = function(name, value) {
	this.params[name] = value;
}


QTObject.prototype.getParams = function() {
    return this.params;
}


QTObject.prototype.getParam = function(name) {
    return this.params[name];
}


QTObject.prototype.getParamTags = function() {
    var paramTags = "";
    for (var param in this.getParams()) {
        paramTags += '<param name="' + param + '" value="' + this.getParam(param) + '" />';
    }
    if (paramTags == "") {
        paramTags = null;
    }
    return paramTags;
}


QTObject.prototype.getHTML = function() {
    var qtHTML = "";
	if (navigator.plugins && navigator.plugins.length) { // not ie
        qtHTML += '<embed type="video/quicktime" src="' + this.mov + '" width="' + this.width + '" height="' + this.height + '" id="' + this.id + '"';
        for (var param in this.getParams()) {
            qtHTML += ' ' + param + '="' + this.getParam(param) + '"';
        }
        qtHTML += '></embed>';
    }
    else { // pc ie
        qtHTML += '<object classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" width="' + this.width + '" height="' + this.height + '" id="' + this.id + '">';
        this.addParam("src", this.mov);
        if (this.getParamTags() != null) {
            qtHTML += this.getParamTags();
        }
        qtHTML += '</object>';
    }
    return qtHTML;
}


QTObject.prototype.getVariablePairs = function() {
    var variablePairs = new Array();
    for (var name in this.getVariables()) {
        variablePairs.push(name + "=" + escape(this.getVariable(name)));
    }
    if (variablePairs.length > 0) {
        return variablePairs.join("&");
    }
    else {
        return null;
    }
}


QTObject.prototype.write = function(elementId) {
	if(isQTInstalled() || this.doDetect=='false') {
		if (elementId) {
			document.getElementById(elementId).innerHTML = this.getHTML();
		} else {
			document.write(this.getHTML());
		}
	} else {
		if (this.redirect != "") {
			document.location.replace(this.redirect);
		} else {
			if (elementId) {
				document.getElementById(elementId).innerHTML = this.altTxt +""+ this.bypassTxt;
			} else {
				document.write(this.altTxt +""+ this.bypassTxt);
			}
		}
	}		
}


function isQTInstalled() {
	var qtInstalled = false;
	qtObj = false;
	if (navigator.plugins && navigator.plugins.length) {
		for (var i=0; i < navigator.plugins.length; i++ ) {
         var plugin = navigator.plugins[i];
         if (plugin.name.indexOf("QuickTime") > -1) {
			qtInstalled = true;
         }
      }
	} else {
		execScript('on error resume next: qtObj = IsObject(CreateObject("QuickTimeCheckObject.QuickTimeCheck.1"))','VBScript');
		qtInstalled = qtObj;
	}
	return qtInstalled;
}

/* get value of querystring param */
function getQueryParamValue(param) {
	var q = document.location.search;
	var detectIndex = q.indexOf(param);
	var endIndex = (q.indexOf("&", detectIndex) != -1) ? q.indexOf("&", detectIndex) : q.length;
	if(q.length > 1 && detectIndex != -1) {
		return q.substring(q.indexOf("=", detectIndex)+1, endIndex);
	} else {
		return "";
	}
}

function popwindow(url, width, height) {
	popup= window.open(url, "popup" ,"width="+width+",height="+height+",scrollbars=no,resizable=no,toolbar=no,menubar=no,status=yes,location=no");
	if (window.focus) { 
		popup.focus(); 
	}
}

function openURL(index){
	var url = bannersURL[index];
	var target = bannersTarget[index];
	if(target == "_blank"){
		window.open(url);	
	}else{
		window.location.replace(url);	
	}
	return true;
}

/*** For Home page banner ****/
function initBanner(){
	loadBanner(curr);
}


function loadBanner(){
	var bnrLinks = new Array();
	
	bnrLinks = banners[curr].links;
	bnrSrc = "/App_Themes/RCI_" + lang + path + banners[curr].src;
	
	var bnrLink = $(document.body).create("a", {id: "bnrCta", href: banners[curr].url, target: banners[curr].target});
	var bnrImg = $(document.body).create("img", {id: "bnr"+curr, src:bnrSrc, width:648, height:168, border: 0});
	
	$$("banner").replaceContent(bnrLink);
	$$("bnrCta").addContent(bnrImg);
	
	
	if(banners.length > 1){
	
		fadeInTween = new OpacityTween($$("banner"), Tween.regularEaseOut, 0, 100, 1);
		fadeInTween.start();
	
		timerID = setTimeout("fadeBanner()", rotatingTimer);
	
	}
}


function fadeBanner(){
	clearTimeout(timerID);	
	fadeOutTween = new OpacityTween($$("banner"), Tween.regularEaseOut, 100, 0, 1);
	tweenObj.onMotionFinished = rotateBanner;
	fadeOutTween.addListener(tweenObj);
	fadeOutTween.start();
}


function rotateBanner(){
	fadeOutTween.removeListener(tweenObj);
	if(curr < banners.length){
		curr++;
	}
	
	if(curr == banners.length) {
		curr = 0;
	}
	loadBanner();	
}


// starto!
var surveyIsDisplayed = false;
DOMAssistant.DOMReady(init);
var popwindow;
var prevTab;
var flashPath = "";	

// banner variables
var timerID;
var curr = 0;
var rotatingTimer = 5000;
var lang = $$(document).elmsByTag("meta")[2].content;

var fadeInTween;
var fadeOutTween;
var tweenObj = new Object();
