var movement = false;
var total_pages = 1;
var current_page = 1;
var f_ex = 'false';
var act_f_ex = 'false';
var count_cloop = 0;
var link_param = '';
var index_1 = 0;
var onSubnav = false; // true if mouse is over ATN subnav.

jQuery.preloadImages = function()
{
	for(var i = 0; i<arguments.length; i++)
	{
		jQuery("<img>").attr("src", arguments[i]);
	}
}

function is_array( mixed_var ) {
	return ( mixed_var instanceof Array );
}

function str_replace(search, replace, subject) {
    var f = search, r = replace, s = subject;
    var ra = is_array(r), sa = is_array(s), f = [].concat(f), r = [].concat(r), i = (s = [].concat(s)).length;
    while (j = 0, i--){
        while (s[i] = s[i].split(f[j]).join(ra ? r[j] || "" : r[0]), ++j in f){};
    }
    return sa ? s : s[0];
}

function getURLParam(strParamName,link){
	var strReturn = "";
	//var strHref = window.location.href;
	var strHref = link.toString();
	if ( strHref.indexOf("?") > -1 ){
		var strQueryString = strHref.substr(strHref.indexOf("?")).toLowerCase();
		var aQueryString = strQueryString.split("&");
		for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
			if (aQueryString[iParam].indexOf(strParamName + "=") > -1 ){
				var aParam = aQueryString[iParam].split("=");
				strReturn = aParam[1];
				break;
			}
		}
	}
	return strReturn;
}

function browser_is_ie(version){
	var appVer = navigator.appVersion.toLowerCase();
	var iePos = appVer.indexOf('msie'); 
	var is_ie = ((iePos!=-1));
	var ver = appVer.charAt(iePos+5);
	var is_ie6 = (is_ie && ver== 6);
	var is_ie7 = (is_ie && ver== 7);
	
	if ((version=="any") && (is_ie)){return true;}
	else if ((version=="6") && (is_ie6)){ return true;}
	else if ((version=="7") && (is_ie7)){ return true;}
	else {return false;}
}

function detectMAC_FF(){
	if((navigator.userAgent.indexOf("Firefox")!=-1) && (navigator.userAgent.indexOf("Macintosh")!=-1)){
		return true;
	}
	else{
		return false;
	} 
}

var hide = false;
var shown = false;
var movement_f = false;
var movement_b = false;

var li_hide = [false,false];
var li_shown = [false,false];
var li_movement_f = [false,false];
var li_movement_b = [false,false];

var k = 0;

function clickAnywhereCreate(){	
	var _height = jQuery(document).height();
	var _width = ((jQuery.browser.msie)&&(parseInt(jQuery.browser.version)<7))?jQuery(document).width()-20:jQuery(document).width();
	
	if(!jQuery('div#bg-cover').attr('tagName')){
	    jQuery('<div id="bg-cover" style="padding:0;margin:0;position:absolute;top:0;left:0;border:0;width:'+_width+'px;height:'+_height+'px;">&nbsp;</div>')
	    .appendTo('body');
    }
    else{
		jQuery('div#bg-cover').show();
    }
}

function initSignIn(){
	//Initialze signin form to hide
	$('ul.menu-client li.active').toggleClass('active');
	$('#lgn_jsmode').val('1');
	$('#fgt_jsmode').val('1');
	$('#password_form').hide();
	
	$('ul.menu-client li div.sign-popup').prev().click(function() {
		li = $(this).parent();
		li.toggleClass('active');
		if (li.attr('class') == 'active'){
			if(!detectMAC_FF()){
				$('div.sign-popup').fadeIn(500);
				$('#password_sent').hide();
				$('#password_init').fadeIn(500);
			}
			else{
				$('div.sign-popup').show();
				$('#password_sent').hide();
				$('#password_init').show();
			}
			
		    clickAnywhereCreate();
		    jQuery('div#bg-cover').click(function(){
		     //$(li).removeClass('active');
		     jQuery('div#bg-cover').hide();
		    });
		}
		else{
			if(!detectMAC_FF()){
				$('div.sign-popup').css("display","none");
				$('#password_sent').hide();
				$('#password_init').css("display","none");
			    clickAnywhereCreate();
			}
		}
		return false;
	});
	
	//stopper for signup form 
	
	$('ul.menu-client li div.sign-bottom .img-bottom1 a').click(function(){
		$(this).parent().parent().hide().next().show().children().children('input[@type="text"]').focus(function(){  //onfocus for email input
			if ($(this).val() == 'Enter email')
				$(this).val('');
		}).blur(function() { //onblur for email input
			if ($(this).val() == '')
				$(this).val('Enter email');
		}).next('input[@type="submit"]').click(function(){
			$(this).parent().parent().hide().next().show();
			return false;
		});
		return false;
	});
}


$(document).ready(function(){
	
	initSignIn();
	
	if($('#scrolling').attr('tagName')){
		$('#scrolling').jScrollPane({showArrows:true, dragMinHeight: 52});
	}
	
	if($('div.drop').attr('tagName')){
		$('div.drop div.logo').mouseover(function(){
			hide=false;
			if(!shown && !movement_b && !movement_f){
				movement_f = true;
				$('div.drop').animate({top:"0px"},700,'linear',function(){movement_f = false});
				shown = true;
			}
		});
		$('div.drop div.logo').mouseout(function(){
			hide = true;
			setTimeout( "if(hide && !movement_b){ movement_b = true; $('div.drop').animate({top:'-133px'},500,'linear',function(){movement_b = false});shown=false;}", 1000 );
		});
	}
	var show=false;
	$('div.about_drop').css('left','89px');
	$('div.about_drop').css('margin-left','50%');
	if($('div.menu').attr('tagName')){
		$('div.menu a#about').mouseover(function(){
			$('div.about_drop').animate({top:"0px"},1000,'linear',function(){movement_f = false});
		});
		
		$('div.menu a#about').mouseout(function(e){
			$.timer(1000, function (timer) {
				if(onSubnav == false) {
					$('div.about_drop').animate({top:'-170px'},700,'linear',function(){movement_b = false});
					timer.stop();
				}
			});
		});
		
	}

	if($('div.about_drop').attr('tagName')){
		$('div.about_drop').mouseover(function(){
			onSubnav = true;
			hide=false;
			if(!shown && !movement_b && !movement_f){
				movement_f = true;
				$('div.about_drop').animate({top:"0px"},700,'linear',function(){movement_f = false});
				shown = true;
			}
		});
		$('div.about_drop').mouseout(function(){
			onSubnav = false;
			hide = true;
			setTimeout( "if(hide && !movement_b){ movement_b = true; $('div.about_drop').animate({top:'-185px'},700,'linear',function(){movement_b = false});shown=false;}", 10 );
		});
	}
	
	if($('ul.info').attr('tagName')){
		$('div.meet-hold div.block1').mouseover(function(){
			li_hide[0]=false;
			if(!li_shown[0] && !li_movement_f[0] && !li_movement_b[0]){
				li_movement_f[0] = true;
				$('div.meet-hold ul.info li ul').slideDown(300,function(){li_movement_f[0] = false;});
				li_shown[0] = true;
			}
		});
		
		$('div.meet-hold div.block1').mouseout(function(){
			li_hide[0] = true;
			setTimeout( "if(li_hide[0] && !li_movement_b[0]){ li_movement_b[0] = true; $('div.meet-hold ul.info li ul').slideUp(300,function(){li_movement_b[0] = false;});li_shown[0]=false;}", 100 );
		});
		
		$('div.about-hold div.block1').mouseover(function(){
			li_hide[1]=false;
			if(!li_shown[1] && !li_movement_f[1] && !li_movement_b[1]){
				li_movement_f[1] = true;
				$('div.about-hold ul.info li ul').slideDown(300,function(){li_movement_f[1] = false;});
				li_shown[1] = true;
			}
		});
		
		$('div.about-hold div.block1').mouseout(function(){
			li_hide[1] = true;
			setTimeout( "if(li_hide[1] && !li_movement_b[1]){ li_movement_b[1] = true; $('div.about-hold ul.info li ul').slideUp(300,function(){li_movement_b[1] = false;});li_shown[1]=false;}", 100 );
		});
	}
	
	if($('ul.tab-menu').attr('tagName')){ // load ships
		if($('ul.tab-menu li a').attr('tagName')){
			$('ul.tab-menu li a').each(function(){
				$(this).click(function(){
					link_param = $(this);
					initShipsFotos(this);
					closePopup();
					return false;
				});
			});
		}		
	}
	
	if($('ul.tab-menu2').attr('tagName')){  // load activities
		if($('ul.tab-menu2 li a').attr('tagName')){
			$('ul.tab-menu2 li a').each(function(){
				$(this).click(function(){
					link_param = $(this);
					initActivitiesFotos(this);
					closePopup();
					return false;
				});
			});
		}		
		
	}
	
	if($('div.cont').attr('tagName')){
		if($('ul.menu li a.default_loads').attr('tagName')){ // load destinations
			loadContent($('ul.menu li a.default_loads'));
			$('ul.menu li a').each(function(){
				$(this).click(function(){
					link_param = $(this);
					loadContent(this);
					closePopup();
					return false;
				});
			});
		}
	}

	// Sign up form jquery functions
	$('#loginfrm').ajaxForm({ 
		dataType:  'json', 
		success: function(data){
			if(data.status == 'success'){
				//Omniture Track Successful log in
				s.linkTrackVars="events"; s.linkTrackEvents="event3";
				s.events="event3";
				s.tl(this,'o','WN:Site:Successful Login');
				window.location="../profile_edit.php?type=login";
			}
			else{
				if (data.error_type == 1){
					$('#error_mail_msg').html(data.msg)
					$('#error_mail').css("display", "block");
					$('#error_password').css("display", "none");
					$('.sign-bottom').css("top", "186px");
				}
				else 
					if (data.error_type == 2) {
						$('#error_password_msg').html(data.msg)
						$('#error_mail').css("display", "none");
						$('#error_password').css("display", "block");
						$('.sign-bottom').css("top", "186px");
					}
			}
		} 
	});
	/*
	$('#clicksignin').click(function(){
		"signinform"
	});
	$('#signinform').mouseout(function(){
		hide = true;
		setTimeout( "if(hide && !movement_b){ movement_b = true; $('div.drop').animate({top:'-133px'},500,'linear',function(){movement_b = false});shown=false;}", 10 );
	});
	
	$('#forgetfrm').ajaxForm({ 
		dataType:  'json', 
		success: function(data){
			if(data.status == 'success'){
				$('#forgetfrm').clearForm();
				$('#password_sent').show();
				$('#password_form').hide();
				$('#sentmsg').html(data.msg);
			}
			else 
				if( data.status == 'error'){
					$('#password_msg').html(data.msg);
				}
		} 
	});

	*/
	$('textarea[maxlength]').keyup(function(){ 
		if($(this).val().length > $(this).attr('maxlength')){ 
			$(this).val($(this).val().substr(0, $(this).attr('maxlength'))); 
		} 
	});
	
	$("#user_email").focus(function() {if($(this).val()=="Enter email") { $(this).val(""); }});
	$("#user_email").blur(function() {if($(this).val().length<3) { $(this).val("Enter email"); }});
});