function wpsiccGetPageScroll() {
  var xScroll, yScroll;
  if (self.pageYOffset) {
    yScroll = self.pageYOffset;
    xScroll = self.pageXOffset;
  } else if (document.documentElement && document.documentElement.scrollTop) {
    yScroll = document.documentElement.scrollTop;
    xScroll = document.documentElement.scrollLeft;
  } else if (document.body) {// all other Explorers
    yScroll = document.body.scrollTop;
    xScroll = document.body.scrollLeft;
  }
  return new Array(xScroll,yScroll);
}

function wpsiccFindPos(obj, toId) {     
    var curleft = curtop = 0;        
    if (obj.offsetParent) {
        	curleft = parseInt(obj.offsetLeft);
     	curtop = parseInt(obj.offsetTop);
     	obj = obj.offsetParent;
     	while (obj && obj.id!=toId) {         			
     			offLeft = obj.offsetLeft;
     			offTop = obj.offsetTop;
	 	       	curleft += (typeof(offLeft)!="undefined"?parseInt(offLeft):0);
	        	curtop += (typeof(offTop)!="undefined"?parseInt(offTop):0);
	        	obj = obj.offsetParent;
     	}
  	 } else {
  	 		if(obj.x) {
											curleft = obj.x;
									} 
									if(obj.y) {
											curtop = obj.x;
									}
							}
		  return [curleft,curtop];
  }

function wpsiccPopup(attachElem, ajaxUrl, data) {
  var elemPos = wpsiccFindPos(attachElem, "body"); //jQuery(attachElem).position();
  var pagePos = wpsiccGetPageScroll();  
  var dialog = jQuery('<div><div id="loading"></div></div>').appendTo('body');
  dialog.dialog({modal:true,width:460,height:600,close:function() {jQuery(this).dialog('destroy').remove();},
   position:[elemPos[0]-pagePos[0],elemPos[1]-pagePos[1]]}); //position:[elemPos.left+refElemPos[0],elemPos.top+refElemPos[1]]});
  dialog.load(ajaxUrl, data);
}

function wpsiccShowShipDetails(lang, baseUrl, elem, cruiseLine, shipCode) {
  wpsiccPopup(elem, baseUrl+"/views/ship-detail.view.php",
   {"ICL_LANG":lang,
	"cruiseLine":cruiseLine,
	"shipCode":shipCode});
  return false;
}

function wpsiccShowTerms(lang, baseUrl, elem, pageId) {
  wpsiccPopup(elem, baseUrl+"/views/terms.view.php",
   {"ICL_LANG":lang,
	"pageId":pageId});
  return false;
}

function wpsiccShowRoute(lang, baseUrl, elem, sailingId, cruiseLine, itineraryCode, shipCode, depDate, duration) {
  wpsiccPopup(elem, baseUrl+"/views/route.view.php",		  
   {"ICL_LANG":lang,
	"sailingId":sailingId,
	"cruiseLine":cruiseLine,
	"itineraryCode":itineraryCode,
	"shipCode":shipCode,
	"depDate":depDate,
	"duration":duration});
  return false;
}

function wpsiccShowDeckplans(lang, baseUrl, elem, cruiseLine, shipCode, deckName) {
  wpsiccPopup(elem, baseUrl+"/views/deckplans.view.php",
   {"ICL_LANG":lang,
	"cruiseLine":cruiseLine,
	"shipCode":shipCode,
	"deckName":deckName});
  return false;
}

function wpsiccShowVirtualView(url) {	   
  jQuery("#wpsicc-category-detail").html("<iframe width='100%' height='550px' src='"+url+"'></iframe>");  
}

String.prototype.trim = function () {
  return this.replace(/^\s*/, "").replace(/\s*$/, "");
}
  
String.prototype.isEmpty = function () {
  return this.trim().length<=0;
}
  
String.prototype.pad = function(len, pad) {
  var str = this;
  if (typeof(len) == "undefined") { var len = 0; }
  if (typeof(pad) == "undefined") { var pad = ' '; }
  if (len + 1 >= str.length) {   
    str = Array(len+1 - str.length).join(pad) + str;
  }
  return str;
}