// JavaScript Document
//window.onload = init;
var curTitle = document.title;
var MSIE = false;
var Opera = false;
var Safari = false
var MSIE_5_5 = false;
var MSIE_5_0 = false;
var MSIE_6_0 = false;
var MSIE_7_0 = false;
if(navigator.userAgent.indexOf('MSIE')>=0 && navigator.userAgent.indexOf('Opera')<0)MSIE=true;
if(navigator.userAgent.indexOf('Opera')>=0)Opera=true;
if(navigator.userAgent.indexOf('Safari')>=0)Safari=true;
if(navigator.userAgent.indexOf('MSIE')>=0 )
{
	var agent = navigator.userAgent.indexOf('MSIE');
	var version = navigator.userAgent.substring( agent+4, navigator.userAgent.length  );
	version = version.substring(0, version.indexOf(';'));
	
	if( parseFloat(version) < 5.5 )
		MSIE_5_0 = true;
	else if( parseFloat(version) == 5.5 && parseFloat(version) < 6.0 )
		MSIE_5_5 = true;
	else if( parseFloat(version) == 6.0 )
		MSIE_6_0 = true;
	else if( parseFloat(version) == 7.0 )
		MSIE_7_0 = true;
}

function init()
{
	initSelect("Deck");
	initSelect("Deck-ap");
	initSelect("Deck-cb");
	initSelect("Deck-co");
	initSelect("Deck-cp");
	initSelect("Deck-di");
	initSelect("Deck-dp");
	initSelect("Deck-ep");
	initSelect("Deck-gp");
	initSelect("Deck-ip");
	initSelect("Deck-kp");
	initSelect("Deck-np");
	initSelect("Deck-pa");
	initSelect("Deck-rp");
	initSelect("Deck-ru");
	initSelect("Deck-sa");
	initSelect("Deck-sp");
	initSelect("Deck-ta");
	initSelect("Deck-tp");
	initSelect("Destinations");
	initSelect("BridgeCams");
	initSelect("AllCams");
	initSelect("Bridge Cams");
	initSelect("WeddingCams");
	initSelect("Ships");
	initSelect("PortsofCall");
	initSelect("Ita");
	initSelect("Shorex-Alaska");
	initSelect("Shorex-HawaiiTahiti");		
	initSelect("Shorex-AustraliaNZAsia");
	initSelect("Shorex-Caribbean");
	initSelect("Shorex-Canada");
	initSelect("Shorex-Coastal");
	initSelect("Shorex-Europe");
	initSelect("Shorex-SouthAmerica");
	initSelect("Shorex-Panama");
	initSelect("Shorex-World");
	initSelect("Shorex-AfricaIndia");
	initSelect("PortsofCall-Brochures");
	ConvertRowsToLinks("deckPlan");
	initSelect("select-ship");
	initSelect("activity");
	initSelect("Video-DVD");
	initSelect("Port-Directions");
	
}

function expandCol(theTR){
	var dataTR = eval('document.getElementById("' + theTR +'")');
	var dataTDarr;
	
	if( dataTR )
	{
		dataTDarr = document.getElementsByName('row' + theTR);
		if( dataTDarr.length == 0 )
		{
			dataTDarr = getElementsByName_iefix('td', 'row' + theTR);
		}
		if (dataTR.style.display=="block" || dataTR.style.display=="" )
		{
			for( var i = 0; i < dataTDarr.length; i++ )
			{
				if( dataTDarr[i].className == "align-center btm-no-border" || dataTDarr[i].className == "align-center" )
				{
					dataTDarr[i].className = "align-center";
					continue;
				}	
				dataTDarr[i].className = "";	
			}
			dataTR.style.display="none";
		}
		else
		{
			for( var i = 0; i < dataTDarr.length; i++ )
			{
				if( dataTDarr[i].className == "align-center" )
				{
					dataTDarr[i].className = "align-center btm-no-border";
					continue;
				}	
				dataTDarr[i].className = "btm-no-border";	
			}
			dataTR.style.display="";	
		}
	}
}

function collapseColAll( )
{
	var TDs;
	var table;
	var i;
	
	var tableId = 'deckPlan';
	
	if( document.getElementById(tableId) )
	{
		table =  document.getElementById(tableId);
		TDs = table.getElementsByTagName( "td" );
		for( i = 0; i < TDs.length; i++ )
		{	
			if( TDs[i].id !="" )
			{
				if ( TDs[i].style.display=="block" || TDs[i].style.display=="" )					
					expandCol( TDs[i].id );
			}
		}
	}
}
function expand( id )
{
	
	collapseColAll( );
	expandCol( id );
	var strUserAgent = navigator.userAgent ;	
	if ( strUserAgent.indexOf( "Safari" ) != -1 )
	{
		moveToTop( "#jumpto" ) ;
	}
	document.title = curTitle;
}
	
function openCategory( id )
{
	collapseColAll( );
	expandCol( id );
	var strUserAgent = navigator.userAgent;	
	//alert(strUserAgent.indexOf( "Safari" ))
	var locationHref = window.location.href;
	if( locationHref.indexOf('#') == -1 )	 
	window.location.href=locationHref+"#jumpto";
	document.title = curTitle;
}


function getElementsByName_iefix(tag, name) {
     
     var elem = document.getElementsByTagName(tag);
     var arr = new Array();
     for(i = 0,iarr = 0; i < elem.length; i++) 
	 {
          att = elem[i].getAttribute("name");
          if(att == name) 
		  {
               arr[iarr] = elem[i];
               iarr++;
          }
     }
     return arr;
}

function setSiteCatalystOnClickVariable(LinkName) {
   s_linkType='o';
    s_linkName=LinkName;
    s_lnk=s_co(this);
    s_gs('princessmain');
}

function siteCatalystOnClickVariable(linkName, location ) {
    s_linkType='o';
    s_linkName=linkName;
    s_lnk=s_co(this);
    s_gs(location);
}

function initSelect( id )
{ 
	$("#"+id).change(function(e) {
		if( this.id == "WeddingCams" || this.id =="BridgeCams" || this.id == "Port-Directions" 
	   	|| this.id.indexOf("Shorex") > -1 )
		{
			if (window.focus()) {
				self.focus();
			}
			if( this.id == "Port-Directions"){
				window.open($(this).val(),"_blank");
			} else if( this.id.indexOf("Shorex") > -1){
				window.open($(this).val(),"_blank");
			} else{
				window.open($(this).val(),"_blank", "resizable=yes,toolbar=no,location=no,width=650,height=460,directories=no,status=yes,scroll=no,scrollbars=no,menubar=no");
			}
			return;
		}		  
    	window.location.href = $(this).val();
  	});
}

function getRefToDivMod( divID, oDoc ) 
{
	if( !oDoc ) { 
		oDoc = document; 
	}
  	if( document.layers ) {
    	if( oDoc.layers[divID] ) { 
			return oDoc.layers[divID]; 
		} 
		else {
      		for( var x = 0, y; !y && x < oDoc.layers.length; x++ ) {
        		y = getRefToDivMod(divID,oDoc.layers[x].document); 
			}
			return y; 
		} 
	}
  	if( document.getElementById ) { 
		return oDoc.getElementById(divID); 
	}
  	if( document.all ) { 
		return oDoc.all[divID]; 
	}
  	return document[divID];
}

function resizeWinTo( idOfDiv ) 
{
	
		//check for flash object
	var obj = document.getElementsByTagName("object");
	var container = document.getElementById('popContent');
	if( obj && container )
	{
		
		var container = document.getElementById('popContent');
		var img = document.createElement('IMG');
		img.src="/images/spacer.gif"
		img.height = obj[0].getAttribute('height');
		img.width = 1;
		container.appendChild(img);
	}
	
  	var oH = getRefToDivMod( idOfDiv ); 
  	if( !oH ) { 
  		return false; 
  	}
  	var oW = oH.clip ? oH.clip.width : oH.offsetWidth;
  	var oH = oH.clip ? oH.clip.height : oH.offsetHeight; 
  
  	if( !oH ) { 
  		return false; 
	}
  	var x = window; x.resizeTo( oW + 200, oH + 200 );
  	var myW = 0, myH = 0, d = x.document.documentElement, b = x.document.body;
  	if( x.innerWidth ) { 
		myW = x.innerWidth; 
		myH = x.innerHeight; 
	}
  	else if( d && d.clientWidth ) { 
		myW = d.clientWidth; 
		myH = d.clientHeight; 
	}
  	else if( b && b.clientWidth ) { 
		myW = b.clientWidth; 
		myH = b.clientHeight; 
	}
  	if( window.opera && !document.childNodes ){ 
		myW += 16; 
	}	
  	x.resizeTo( oW + ( ( oW + 200 ) - myW ), oH + ( (oH + 200 ) - myH ) );
}



function ConvertRowsToLinks(xTableId)
{
	
	if( document.getElementById(xTableId) ){ 
		var rows = document.getElementById(xTableId).getElementsByTagName("tr");
		for(i=0;i<rows.length;i++){
			var link = rows[i].getElementsByTagName("a");
			if(link.length == 1 ){
				//link[0].href ="#";
				rows[i].onclick = link[0].onclick;
				rows[i].title = link[0].title;
				cols = rows[i].getElementsByTagName("td");
				if( cols.length > 1 ){
					rows[i].onmouseover = new Function("this.className='highlight'");
					rows[i].onmouseout = new Function("this.className=''");
				}
				link[0].onclick = "";
			}
			
		}
	}
}

function autoResize(id)
{
	var newheight;
  	var newwidth;
    if(document.getElementById)
	{
		//alert( document.getElementById(id).contentWindow.baseURI)
        //newheight=document.getElementById(id).contentWindow.document.body.scrollHeight;
    	//newwidth=document.getElementById(id).contentWindow.document.body.scrollWidth;
   	}

    document.getElementById(id).height= (newheight+20) + "px";
  	document.getElementById(id).width= (newwidth) + "px";
}

function expandCol2(theTR){
	var dataTR = eval('document.getElementById("' + theTR +'")');
	if (dataTR.style.display=="block" || dataTR.style.display=="" ){
		dataTR.style.display="none";
	}else{
		dataTR.style.display="";
		dataTR.style.position="absolute";
		dataTR.style.posTop=95;
		dataTR.style.zIndex=15;

		}
}

function equalHeight(left,right){
	var leftContainer = eval('document.getElementById("' + left +'")');
	var rightContainer = eval('document.getElementById("' + right +'")');
	leftContainer.offsetHeight >= rightContainer.offsetHeight ? rightContainer.style.height = leftContainer.offsetHeight + "px" : leftContainer.style.height = rightContainer.offsetHeight + "px";
	}

function preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.p) d.p=new Array();
    var i,j=d.p.length,a=preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.p[j]=new Image; d.p[j++].src=a[i];}}
}
function swapImgRestore() { //v3.0
  var i,x,a=document.sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function swapImage() { //v3.0
  var i,j=0,x,a=swapImage.arguments; document.sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=findObj(a[i]))!=null){document.sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function showHide(ele1,ele2) {
	var srcElement1 = document.getElementById(ele1);
	var srcElement2 = document.getElementById(ele2);
	if(srcElement1 != null) {
		if(srcElement1.style.display == "block") {
		   srcElement1.style.display= 'none';
		}
		else {
		   srcElement1.style.display='block';
		}
	}
	if(srcElement2 != null) {
		if(srcElement2.style.display == "block") {
		   srcElement2.style.display= 'none';
		}
		else {
		   srcElement2.style.display='block';
		}
	}
}

function visInvis(ele1,ele2) {
	var srcElement1 = document.getElementById(ele1);
	var srcElement2 = document.getElementById(ele2);
	if(srcElement1 != null) {

		if(srcElement1.style.visibility == "visible" || srcElement1.style.visibility =="" ) {
		   srcElement1.style.visibility= 'hidden';
		}
		else {
		   srcElement1.style.visibility='visible';
		}
	}
	showHide(null,ele2);
	
}


function moveToTop( poundAnchr )
{
	
	locationHref = window.location.href;
	if(locationHref.indexOf(poundAnchr,0)==-1) 
		window.location.href=locationHref+poundAnchr;

	else 
		window.location.href=locationHref;
	
}

var browser=navigator.appName
	function activeBg(ele){
	   	var srcElement = document.getElementById(ele);
		srcElement.style.backgroundPosition="-82px -427px";
		if (browser=="Microsoft Internet Explorer")
		  {
		  	srcElement.style.cursor="hand";
		  }
	}
	
	function normalBg(ele){
		var srcElement = document.getElementById(ele);
		srcElement.style.backgroundPosition="-82px -40px";
		}
		
function QueryString(qs) { // optionally pass a querystring to parse
	this.params = new Object()
	this.get=QueryString_get
	
	if (qs == null)
		qs=location.search.substring(1,location.search.length)

	if (qs.length == 0) return

// Turn <plus> back to <space>
// See: http://www.w3.org/TR/REC-html40/interact/forms.html#h-17.13.4.1
	qs = qs.replace(/\+/g, ' ')
	var args = qs.split('&') // parse out name/value pairs separated via &
	
// split out each name=value pair
	for (var i=0;i<args.length;i++) {
		var value;
		var pair = args[i].split('=')
		var name = unescape(pair[0])

		if (pair.length == 2)
			value = unescape(pair[1])
		else
			value = name
		
		this.params[name] = value
	}
}

function QueryString_get(key, default_) {
	// This silly looking line changes UNDEFINED to NULL
	if (default_ == null) default_ = null;
	
	var value=this.params[key]
	if (value==null) value=default_;
	
	return value
}
function adjustSelectWidth( )
{
	var maxWidth = 478;
	var minWidth = 0;
	var maxCharWidth =5;
	var curMaxWidth = 0;
	var selects = document.getElementsByTagName("select");
	
	for( var numSelect = 0; numSelect < selects.length; numSelect++ )
	{
		for( var numOpt = 0; numOpt < selects[numSelect].length; numOpt++ )
		{
			numChars = selects[numSelect].options[numOpt].innerHTML.length;
			var calcWidth = numChars*maxCharWidth;
			curMaxWidth = calcWidth > curMaxWidth ? calcWidth : curMaxWidth;
		}
	}
	curMaxWidth = curMaxWidth > maxWidth ? maxWidth : curMaxWidth;
	for( numSelect = 0; numSelect < selects.length; numSelect++ )
		selects[numSelect].style.width = curMaxWidth;

}


function rand( maxNum )
{
	return Math.floor( Math.random() * maxNum  ) + 1;
}
function tip_rotator( num_tips ) 
{
	
	var path = '/images/romance/tips/';
	var prefix = 'Romance_Tips_';
   	var ctr = rand( num_tips );
	var src = 'src="' + path + prefix + ctr + '.jpg" ';
	var dim = '';
    var imgStr = '<img ' + src + dim + ' alt="romance tip no.' + ctr + '">';
    document.write(imgStr); document.close();
}


function promoRotator( elm, numRotate, path, prefix )
{
	if( !document.getElementById(elm) )
		return;
		
	var elem = document.getElementById(elm);

	var ctr = rand( numRotate );
	var sURL = getPath();

	if( sURL == "index.html" && sURL.indexOf("/") == -1){
		elem.onclick  = function(){
				if( ctr == 1 ){
					pageTracker._trackPageview ('/home/promotion/caribbeanescapecompletely');
				}
				else
					pageTracker._trackPageview ('/home/promotion/caribbeanrelaxedalternative');
			};
	}
	
	var img = elem.firstChild;
	img.src = path + prefix + ctr + '.jpg';	

	
	if( img.style.display == 'none' )
		img.style.display = "";
	
}

function flashOnClick( elem, optional )
{
	if( MSIE )
		document.getElementById(elem).click();
	else
		document.getElementById(elem).onclick();
	if( optional && optional != "" )
		eval(optional);
}
function flashSiteCatalyst( js, linkName  )
{
	//setSiteCatalystOnClickVariable(linkName);
	eval(js);
	
}
function resizeToTextOffset( )
{
	
	var sizeWH = getInnerSize();
	var approxWrapLen = 125;
	var linePxDx = 16, tagIndex = 0;
	var wdx = 46;
	var adjustment = 5;
	
	sizeWH[0] =740 , sizeWH[1] =540;
	
	if( MSIE_6_0 ){ wdx = 60; }
	if( MSIE_7_0 ){ wdx = 70; }
	if( Safari ){ wdx = 55; }
	if( Opera ){ wdx = 35; }
	
	wdx += adjustment;
	
	var copyTxt = document.getElementsByTagName("p");
	var copyOffset = Math.ceil((copyTxt[tagIndex].innerHTML.length)/approxWrapLen);
	window.resizeTo( sizeWH[0]+15, sizeWH[1] + ((copyOffset-1)*linePxDx)+wdx );
}

function getInnerSize () {
	var x,y;
	if (self.innerHeight){ // all except Explorer
		x = self.innerWidth;
		y = self.innerHeight;
	}

	else if (document.documentElement && document.documentElement.clientHeight){// Explorer 6 Strict Mode	
		x = document.documentElement.clientWidth;
		y = document.documentElement.clientHeight;
	}
	else if (document.body){ // other Explorers
		x = document.body.clientWidth;
		y = document.body.clientHeight;
	}
	return [x,y];
}

function printTAHeader(){
	document.write( '  <div id="header-container">' );
	document.write( '      <div id="header">' );	
	document.write( '        <div class="float"><img src="/images/global/princess_logo.gif" alt="Princess Cruises Logo" /></div>' );		
	document.write( '      </div>' );
	document.write( '  </div>' );
	document.write( '  <div id="no-navigation"></div>' );
	document.write( '  <div style="clear:both"></div>' );
}

function printTAFooter(){
	document.write('<br>');
	document.write('<div id="footer">');
	document.write('  <ul>');	
	document.write('    <li><a href="' + getWWWUrl(getUrl()) + 'legal/" title="Legal &amp; Privacy">Legal &amp; Privacy</a></li>');	
	document.write('    <li><a href="' + getWWWUrl(getUrl()) + 'aboutus/" title="About Us">About Us</a></li>');	
	document.write('    <li><a href="' + getWWWUrl(getUrl()) + 'customer_care/faqs/" title="FAQ">FAQ</a></li>');	
	document.write('    <li class="noLinkBg"> <a href="' + getWWWUrl(getUrl()) + 'customer_care/contact/index.jsp" title="Contact Us">Contact Us</a> </li>');
	document.write('  </ul>');
	document.write('</div>');
	document.write('<div id="copyRight">');
	document.write('  <p> &copy; 2010, <a href="' + getWWWUrl(getUrl()) + 'index.html" title="Go to Princess Home">Princess Cruise Lines, Ltd.</a> All Rights Reserved. Site content may not be reproduced without express written permission.</p>');
	document.write('</div>');	
	document.write('<br>');
}

