$(document).ready(function()
{
	var cdv="/cabindiv.php?ship="+shipVar+"&cabin=";
	function hideMenu()
	{
		$(".menu-item-body").hide();
		$(".menu_body").hide();
		$(".sub-menu-body").hide();
	}
	function showMenuEntry(a)
	{
		if(!a) return;
		hideMenu();
		$(a).show().parent().show().parent().show();
	}
	function scabSubmit()
	{
		$("#cabin-info").load(cdv+$("#scabin")[0].value,null,imagePreview);
		return false;
	}
	$(".menu-item-head").click(function()
	{
		$(this).next(".menu-item-body").toggle().siblings(".menu-item-body").hide();
	});
	$(".menu_head").click(function()
 	{
		$(this).next(".menu_body").toggle().siblings(".menu_body").hide();
 	});
	$(".sub-menu-head").click(function()
	{
		$(this).next(".sub-menu-body").toggle().siblings(".sub-menu-body").hide();
	});
	// $("area").click(function(){$("#cabin-info").load(cdv+this.href.match(/[a-zA-Z0-9]+$/)[0],null,imagePreview);return false;});
	$("#Map area").easyTooltip();
	hideMenu();
	if(shipVar&&$("#"+shipVar).length>0) showMenuEntry($("#"+shipVar));
	else showMenuEntry($(".menu-item-body")[0]);
	$("#scabSubmit").click(scabSubmit);
	$("#scabin").keydown(function(e){if(e.keyCode==13) scabSubmit();return true;})
	imagePreview();
	
	if(shipVar&&$(".ship-info").length>0) flickrSearch();
});

var flickrPage=1,flickrDir=1;
function flickrPageNext()
{
	flickrDir=1;
	flickrPage++;
	flickrSearch();
}
function flickrPagePrev()
{
	if(flickrPage<=1) return;
	flickrDir=-1;
	flickrPage--;
	flickrSearch();
}
function flickrSearch(s)
{
	if($("#ship-photos").length==0)
	{
		var t=$("<table>").appendTo(".ship-info").attr({cellpadding:0,cellspacing:0});
		t=$("<tr>").appendTo(t);
		$("<div>").appendTo(t).attr("id","flickrLeft").css({margin:"10px 0",width:50,height:50,background:"url(/css/images/arrow1_left.png) no-repeat"}).click(flickrPagePrev).wrap("<td>");
		$("<div>").appendTo(t).attr("id","ship-photos").css({height:70,textAlign:"center",position:"relative",overflow:"hidden"}).wrap("<td>");
		$("<div>").appendTo(t).attr("id","flickrRight").css({margin:"10px 0",width:50,height:50,background:"url(/css/images/arrow1_right.png) no-repeat"}).click(flickrPageNext).wrap("<td>");
	}
	$("#ship-photos").css({width:$(".ship-info").width()-100});
	$.get("/flickrproxy.php",{q:clineVar+" "+shipVar,p:flickrPage},function(r){eval(r)});
}
function jsonFlickrApi(r)
{
	if(flickrPage==1) var o=0,v="hidden";
	else var o=1,v="visible";
	$("#flickrLeft").animate({opacity:o},200,function(){$(this).css("visibility",v)});
	if(!r||r.stat!="ok") return;
	var w=$("#ship-photos").width();
	$("#ship-photos img").animate({left:-w*flickrDir},500,function(){$(this).remove()});
	var p=r.photos.photo;
	for(var i in p)
	{
		var c=p[i];
		var u="http://farm"+c.farm+".static.flickr.com/"+c.server+"/"+c.id+"_"+c.secret;
		var ut=u+"_s.jpg";
		var um=u+"_m.jpg";
		u+=".jpg";
		var a=$("<a>").appendTo("#ship-photos").attr({title:c.title,href:u,onclick:"popImage('"+u+"','"+c.title+"');return false;"}).addClass("preview");
		$('<img>').appendTo(a).attr({src:ut,alt:c.title,big:um});
	}
	$("#ship-photos img").css({margin:10,width:50,height:50,position:"relative",left:w*flickrDir}).animate({left:0},600);
	imagePreview();
}

function imagePreview()
{
	var xOffset=175;
	var yOffset=30;
	$("a.preview").unbind().hover(function(e)
	{
		this.t=this.title;
		this.title="";
		var c=(this.t!="")?"<br/>"+this.t:"";
		var u=$(this).children(0).attr("big");
		if(!u) u=$(this).children(0).attr("src");
		$("body").append("<p id=\"preview\"><img src=\""+u+"\" alt=\"Image preview\"/>"+c+"</p>");
		$("#preview").css({top:(e.pageY-xOffset)+"px",left:(e.pageX+yOffset)+"px"}).fadeIn("fast");
	},function()
	{
		this.title=this.t;
		$("#preview").remove();
	}).mousemove(function(e)
	{
		$("#preview").css({top:(e.pageY-xOffset)+"px",left:(e.pageX+yOffset)+"px"});
	});
}

function validateForm() {

var errStr = "";


if (document.revform.loc_rating.value <1) {
errStr +="Please rate cabin location\n";
		}	
if (document.revform.noise_rating.value  <1) {
errStr +="Please rate cabin noise\n";
}
if (document.revform.cabin_condition.value  <1) {
errStr +="Please rate cabin condition\n";
}
if (document.revform.oa_rating.value  <1) {
errStr +="Please rate cabin overall\n";
}
if (document.revform.trip_month.selectedIndex == 0) {
errStr +="Please select cruise month \n";
}
if (document.revform.trip_year.selectedIndex == 0) {
errStr +="Please select cruise year\n";
}
if (document.revform.ReviewTitle.value == "") {
errStr +="Please enter a title\n";
}
if (document.revform.ReviewText.value == "") {
errStr +="Please enter your review text\n";
}
if (document.revform.certify.checked == false) {
errStr +="Please certify your review\n";
}



//  alert(document.revform.certify.checked);

if (errStr != "") {
alert (errStr);
return (false);
} else {
return (true);
}
}

function isCabin(elem){
	var alphaExp = /^([A-Za-z]{1,2}[0-9]{1,4}|[0-9]{1,5})/;
	if(elem.value.match(alphaExp)){
		return true;
	}else{
		if (elem.value ==''){
	var helperMsg= 'Cabin number cannot be blank\nPlease enter valid cabin number';
	}
	else
	{
	var helperMsg= 'Cabin ' + elem.value +' not valid.\nPlease enter valid cabin number';
	}
		alert(helperMsg);
		elem.focus();
		return false;
	}
}

function popImage(imageURL,imageTitle){
// Set the horizontal and vertical position for the popup

PositionX = 100;
PositionY = 100;

// Set these value approximately 20 pixels greater than the
// size of the largest image to be used (needed for Netscape)

defaultWidth  = 500;
defaultHeight = 500;

// Set autoclose true to have the window close automatically
// Set autoclose false to allow multiple popup windows

var AutoClose = true;

// Do not edit below this line...
// ================================
if (parseInt(navigator.appVersion.charAt(0))>=4){
var isNN=(navigator.appName=="Netscape")?1:0;
var isIE=(navigator.appName.indexOf("Microsoft")!=-1)?1:0;}
var optNN='scrollbars=no,width='+defaultWidth+',height='+defaultHeight+',left='+PositionX+',top='+PositionY;
var optIE='scrollbars=no,width=150,height=100,left='+PositionX+',top='+PositionY;

if (isNN){imgWin=window.open('about:blank','',optNN);}
if (isIE){imgWin=window.open('about:blank','',optIE);}
with (imgWin.document){
writeln('<html><head><title>Loading...</title><style>body{margin:0px;}</style>');writeln('<sc'+'ript>');
writeln('var isNN,isIE;');writeln('if (parseInt(navigator.appVersion.charAt(0))>=4){');
writeln('isNN=(navigator.appName=="Netscape")?1:0;');writeln('isIE=(navigator.appName.indexOf("Microsoft")!=-1)?1:0;}');
writeln('function reSizeToImage(){');writeln('if (isIE){');writeln('window.resizeTo(300,300);');
writeln('width=300-(document.body.clientWidth-document.images[0].width);');
writeln('height=300-(document.body.clientHeight-document.images[0].height);');
writeln('window.resizeTo(width,height);}');writeln('if (isNN){');       
writeln('window.innerWidth=document.images["George"].width;');writeln('window.innerHeight=document.images["George"].height;}}');
writeln('function doTitle(){document.title="'+imageTitle+'";}');writeln('</sc'+'ript>');
if (!AutoClose) writeln('</head><body bgcolor=000000 scroll="no" onload="reSizeToImage();doTitle();self.focus()">')
else writeln('</head><body bgcolor=000000 scroll="no" onload="reSizeToImage();doTitle();self.focus()" onblur="self.close()">');
writeln('<img name="George" src='+imageURL+' style="display:block"></body></html>');
close();		
}}
function openpopup(url, windowtarget) 
{
    (window.open(
        url, 
        windowtarget,
        'width=640,height=600,toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1')|| window).focus();
}

function bookmark(){

var url = location.href;  // url of the site
var title = document.title; // title/description

if (window.sidebar) // firefox
    window.sidebar.addPanel(title, url, "");
else if(window.opera && window.print){ // opera
    var elem = document.createElement('a');
    elem.setAttribute('href',url);
    elem.setAttribute('title',title);
    elem.setAttribute('rel','sidebar');
    elem.click();
}
else if(document.all)// ie
    window.external.AddFavorite(url, title);
//alert("Bookmark "+document.title+" set ");
}
function ChangeColor(tableRow, highLight)
    {
    if (highLight)
    {
      tableRow.style.backgroundColor = '#dcfac9';
    }
    else
    {
      tableRow.style.backgroundColor = 'white';
    }
  }
function DoNav(theUrl)
  {
  document.location.href = theUrl;
  }
  
