var loading = '<option value="Loading">Loading...<\/option>';
var x_loading = "<h3>LOADING...<\/h3>";
var url = "http://www.findchatndate.com/";
var url_img = "http://www.findchatndate.com/images/";
var block_img = url_img+"foo_panel_icons/icons/cross-circle-frame.png";
/*register button swap image*/
var submitInProgress = false;
function submitStopper(buttonId){
	if(submitInProgress) return false;
	submitInProgress = true;
	var button = document.getElementById(buttonId);
	button.src = "button_update_over.jpg";
}
$.ui.dialog.defaults.bgiframe = true;//function to show dialog 
//SETUP DIALOG STANDARD OPTIONS
var dialogOpts = {
	modal: true,
	position: 'center',
	autoOpen:false,
	width:'auto',
	overlay: { opacity: 0.7, background: 'black'}		
};
//END DIALOG
/* LAUNCH WINDOW ON LOAD*/

 
function launchWindow(id) {	
		//Get the screen height and width
		var maskHeight = $(document).height();
		var maskWidth = $(window).width();	
		//Set heigth and width to mask to fill up the whole screen
		$('#mask').css({'width':maskWidth,'height':maskHeight});		
		//transition effect		
		$('#mask').fadeIn(1000);	
		$('#mask').fadeTo("slow",0.8);		
		//Get the window height and width
		var winH = $(window).height();
		var winW = $(window).width();              
		//Set the popup window to center
		$(id).css('top',  winH/2-$(id).height());
		$(id).css('left', winW/2-$(id).width()/2);	
		//transition effect
		$(id).fadeIn(2000);  
}
/* END OF LAUNCH WINDOW*/
$(document).ready(function(){	
	$("#dialog_div").dialog(dialogOpts);
	/*get message for reading*/
	$(function() {
		$("a.read_message").click(function() {
			var target = "message_details";
			var rel = this.rel;
			$("#"+target).load(url+"messagesread/?time="+Math.random()+"&msgid="+rel);
		});
	});
	/* FORGOT PASSWORD */
	$('a.pwd_forgot').live('click',function() {
			$('#dialog_div').dialog('option', 'title', 'Forgot Password Help');
			$('#dialog_div').dialog('option', 'buttons', { "Close": function() { $(this).dialog("close"); } });
			$("#dialog_div").load("pwd-help/?time="+Math.random(), [], function(){
																   $("#dialog_div").dialog("open");
																} 
			);
			return false;
		}
	);
	/*ADD TO INVITE IMAGES*/
	$('a.invitation').live('click',function() {
			var userid = this.id;
			userid = userid.replace(/[^0-9]/g, "");
			$('#dialog_div').dialog('option', 'title', 'Invite to Private Images');
			$("#dialog_div").load("add-invitation/?userid="+userid, [], function(){
																   $("#dialog_div").dialog("open");
																} 
			);
			setTimeout(function(){ $("#dialog_div").dialog("close"); }, 3000); 
			$('#li_invited_'+userid).html('');
			$('#li_invited_'+userid).append('<a href=\'#\' title=\'Remove Invitation\' class=\'remove_invitation\' id=\'remove_invitation_'+userid+'\'><img src="'+url_img+'foo_panel_icons/icons/minus-white.png" class="img_icon" alt="remove invitation" /> Remove Invitation</a>');
			
			return false;
		}
	);
	/*REMOVE FROM INVITE IMAGES*/
	$('a.remove_invitation').live('click',function() {
			var userid = this.id;
			userid = userid.replace(/[^0-9]/g, "");
			$('#dialog_div').dialog('option', 'title', 'Remove Invitation From Private Images');
			$("#dialog_div").load("add-invitation/?action=remove&userid="+userid, [], function(){
																   $("#dialog_div").dialog("open");																   
																} 
			);
			setTimeout(function(){ $("#dialog_div").dialog("close"); }, 3000); 
			$('#li_invited_'+userid).html('');
			$('#li_invited_'+userid).append('<a href=\'#\' title=\'Add to My Invitation List\' class=\'invitation\' id=\'invitation_'+userid+'\'><img src="'+url_img+'foo_panel_icons/icons/plus-white.png" class="img_icon" alt="add invitation" /> Invite To Images</a>');
			
			return false;
		}
	);
	/*ADD TO FAVOURITES*/
	$('a.favourites').live('click',function() {
			var userid = this.id;
			userid = userid.replace(/[^0-9]/g, "");
			$('#dialog_div').dialog('option', 'title', 'Add To Favourites');
			$("#dialog_div").load("add-favourites/?userid="+userid, [], function(){
																   $("#dialog_div").dialog("open");
																} 
			);
			setTimeout(function(){ $("#dialog_div").dialog("close"); }, 3000); 
			$('#li_fav_'+userid).html('');
			$('#li_fav_'+userid).append('<a href=\'#\' title=\'Remove From My Favourites\' class=\'remove_favourites\' id=\'remove_favourites_'+userid+'\'><img src="'+url_img+'foo_panel_icons/icons/minus-white.png" class="img_icon" alt="remove Favourites" /> Remove From My Favourites</a>');

			return false;
		}
	);
	/*REMOVE FROM FAVOURITES*/
	$('a.remove_favourites').live('click',function() {
			var userid = this.id;
			userid = userid.replace(/[^0-9]/g, "");
			$('#dialog_div').dialog('option', 'title', 'Remove From Favourites');
			$("#dialog_div").load("add-favourites/?action=remove&userid="+userid, [], function(){
																   $("#dialog_div").dialog("open");
																} 
			);
			setTimeout(function(){ $("#dialog_div").dialog("close"); }, 3000); 
			$('#li_fav_'+userid).html('');
			$('#li_fav_'+userid).append('<a href=\'#\' title=\'Add to My Favourites\' class=\'favourites\' id=\'favourites_'+userid+'\'><img src="'+url_img+'foo_panel_icons/icons/plus-white.png" class="img_icon" alt="add Favourites" /> Add To My Favourites</a>');
			return false;
		}
	);
	/*TELL A FRIEND */
	$('a.tell_friend').live('click',function() {
			var userid = this.id;
			userid = userid.replace(/[^0-9]/g, "");
			$('#dialog_div').dialog('option', 'title', 'Tell A Friend');
			//add button
			$('#dialog_div').dialog('option', 'buttons', { "Close": function() { $(this).dialog("close"); } });
			$("#dialog_div").load("tell-friend/?userid="+userid, [], function(){
																   $("#dialog_div").dialog("open");
																} 
			);
			return false;
		}
	);
	/* SEND MESSAGE */
	$('a.send_message').live('click',function() {
			var userid = this.id;
			var nickname = this.rel;
			var msgid = this.title;
			msgid = msgid.replace(/[^0-9]/g, "");
			userid = userid.replace(/[^0-9]/g, "");
			$('#dialog_div').dialog('option', 'title', 'Send A Message To '+nickname);
			//add button
			$('#dialog_div').dialog('option', 'buttons', { "Close": function() { $(this).dialog("close"); } });
			$("#dialog_div").load("send-message/?userid="+userid+"&nickname="+nickname+(msgid != "" ? "&msgid="+msgid : ""), [], function(){
																   $("#dialog_div").dialog("open");
																} 
			);
			return false;
		}
	);
	/* REPORT USER */
	$('a.report_user').live('click',function() {
			var userid = this.id;
			var nickname = this.rel;
			userid = userid.replace(/[^0-9]/g, "");
			$('#dialog_div').dialog('option', 'title', 'REPORT USER '+nickname);
			//add button
			$('#dialog_div').dialog('option', 'buttons', { "Close": function() { $(this).dialog("close"); } });
			$("#dialog_div").load("report-user/?userid="+userid+"&nickname="+nickname, [], function(){
																   $("#dialog_div").dialog("open");
																} 
			);
			return false;
		}
	);
	/* BLOCK USER */
	$('a.block_user').live('click',function() {
			var userid = this.id;
			var rel = this.rel;
			var x_array = rel.split('|');
			var nickname = x_array[0];
			var method = x_array[1];
			userid = userid.replace(/[^0-9]/g, "");
			$('#dialog_div').dialog('option', 'title', 'BLOCK / UN-BLOCK USER '+nickname);
			//add button
			$('#dialog_div').dialog('option', 'buttons', { "Close": function() { $(this).dialog("close"); } });
			$("#dialog_div").load("block-user/?method="+method+"&nickname="+nickname, [], function(){
																   $("#dialog_div").dialog("open");
																} 
			);
			setTimeout(function(){ $("#dialog_div").dialog("close"); }, 3000); 
			$('#li_block_'+userid).html('');
			if(method == "block"){
				$('#li_block_'+userid).append("<a href='#' title='UNBLOCK USER' class='block_user' id='block_user"+userid+"' rel='"+nickname+"|unblock'><img src='"+block_img+"' class='img_icon' alt='unblock user' /> UNBLOCK USER</a>");
			}else{
				$('#li_block_'+userid).append("<a href='#' title='Block User' class='block_user' id='block_user"+userid+"' rel='"+nickname+"|block'><img src='"+block_img+"' class='img_icon' alt='block user' /> BLOCK USER</a>");
			}
			return false;
		}
	);
	//chat_with_user_not
	$('a.chat_with_user_not').live('click',function() {
			var userid = this.id;
			var nickname = this.rel;
			userid = userid.replace(/[^0-9]/g, "");
			$('#dialog_div').dialog('option', 'title', 'CHAT WITH '+nickname);
			//add button
			$('#dialog_div').dialog('option', 'buttons', { "Close": function() { $(this).dialog("close"); } });
			$("#dialog_div").html("<h4>You must be register & loged in<br />to open a chat session with<br />"+nickname+"</h4><a href='http://www.findchatndate.com/dating-register.html' title='register'><b>Click Here to Register &raquo;</b></a>");
			$("#dialog_div").dialog("open");
			return false;
		}
	);
	//chat_with_user
	$('a.chat_with_user').live('click',function() {
			var userid = this.id;
			var nickname = this.rel;
			userid = userid.replace(/[^0-9]/g, "");
			chatWith(nickname);
		}
	);
	
	//PANEL FOOTER
	//Adjust panel height
	$.fn.adjustPanel = function(){ 
		$(this).find("ul, .subpanel").css({ 'height' : 'auto'}); //Reset subpanel and ul height
		
		var windowHeight = $(window).height(); //Get the height of the browser viewport
		var panelsub = $(this).find(".subpanel").height(); //Get the height of subpanel	
		var panelAdjust = windowHeight - 100; //Viewport height - 100px (Sets max height of subpanel)
		var ulAdjust =  panelAdjust - 25; //Calculate ul size after adjusting sub-panel (27px is the height of the base panel)
		
		if ( panelsub >= panelAdjust ) {	 //If subpanel is taller than max height...
			$(this).find(".subpanel").css({ 'height' : panelAdjust }); //Adjust subpanel to max height
			$(this).find("ul").css({ 'height' : ulAdjust}); //Adjust subpanel ul to new size
		}
		else if ( panelsub < panelAdjust ) { //If subpanel is smaller than max height...
			$(this).find("ul").css({ 'height' : 'auto'}); //Set subpanel ul to auto (default size)
		}
	};
	
	//Execute function on load
	$("#chatpanel").adjustPanel(); //Run the adjustPanel function on #chatpanel
	$("#alertpanel").adjustPanel(); //Run the adjustPanel function on #alertpanel
	//$("#userchatpanel").adjustPanel(); //Run the adjustPanel function on #alertpanel
	
	//Each time the viewport is adjusted/resized, execute the function
	$(window).resize(function () { 
		$("#chatpanel").adjustPanel();
		$("#alertpanel").adjustPanel();
		//$("#userchatpanel").adjustPanel();
	});
	
	//Click event on Chat Panel + Alert Panel	
	$("#chatpanel a:first, #alertpanel a:first,").click(function() { //If clicked on the first link of #chatpanel and #alertpanel... #userchatpanel a:first"
		if($(this).next(".subpanel").is(':visible')){ //If subpanel is already active...
			$(this).next(".subpanel").hide(); //Hide active subpanel
			$("#footpanel li a").removeClass('active'); //Remove active class on the subpanel trigger
		}else{ //if subpanel is not active...
			$(".subpanel").hide(); //Hide all subpanels
			$(this).next(".subpanel").toggle(); //Toggle the subpanel to make active
			$("#footpanel li a").removeClass('active'); //Remove active class on all subpanel trigger
			$(this).toggleClass('active'); //Toggle the active class on the subpanel trigger
		}
		return false; //Prevent browser jump to link anchor
	});
	
	//Click event outside of subpanel
	$(document).click(function() { //Click anywhere and...
		$(".subpanel").hide(); //hide subpanel
		$("#footpanel li a").removeClass('active'); //remove active class on subpanel trigger
	});
	$('.subpanel ul').click(function(e) { 
		e.stopPropagation(); //Prevents the subpanel ul from closing on click
	});
	
	//Delete icons on Alert Panel
	$("#alertpanel li").hover(function() {
		var liid = $(this).find("a.alert_delete");
		liid.css({'visibility': 'visible'}); //Show delete icon on hover		
		//alert(liid);
		var alerts_count = $('#alert_count').text();// alert(alerts_count);
		
		$(function() {
		liid.click(function() {
			var target = $(this).attr("rel");
			var alertdid = target.replace(/[^0-9]/g, "");			
			$("."+target).load(url+"alerts-update/?time="+Math.random()+"&action=delete&alertid="+alertdid);
			//decrease count
			$('#alert_count').html( (alerts_count > 0 ? alerts_count-1 : 0) );
		});
	});
		//alert($(this));
	},function() {
		$(this).find("a.alert_delete").css({'visibility': 'hidden'}); //Hide delete icon on hover out
		//update alerts as viewed
		var target = $(this).attr("id");
		var alertdid = target.replace(/[^0-9]/g, "");
		var alerts_count = $('#alert_count').text();// alert(alerts_count);
		if(alertdid != ""){
			$.ajax({ url: url+"alerts-update/?time="+Math.random()+"&action=update&alertid="+alertdid});
			$(this).css({'background-color' : 'white', 'font-weight' : 'normal'});//change background color
			$(this).removeAttr("id");//remove id
			//decrease count
			$('#alert_count').html( (alerts_count > 0 ? alerts_count-1 : 0) );

		}
	});	
	//END PANEL FOOTER 
	//pretty photo
	$("a[rel^='prettyPhoto']").prettyPhoto({theme:'facebook'});
	
	/*get edit photo page*/
	$(function() {
		$("a.photo_edit_link").click(function() {
			var target = $(this).attr("id");
			target = "div_" + target.replace(/[^0-9]/g, "");
			$("#"+target).load(url+"edit_img/?time="+Math.random()+"&edit=true&photoid="+$(this).attr("id"));
		});
	});
	/* delete photo link*/
	$(function() {
		$("a.photo_delete_link").click(function() {
			var target = $(this).attr("id");
			target = "div_" + target.replace(/[^0-9]/g, "");
			$("#"+target).load(url+"edit_img/?time="+Math.random()+"&delete=true&photoid="+$(this).attr("id"));
		});
	});
	
	/*get province select menu*/
	$(function() {
		$("select#cor").change(function() {
			/*$("#province_div").html(loading);*/
			//var x_cor = document.getElementById('cor');	
			$("#province_div").load(url+"province-list/?time="+Math.random()+"&ajax=true&cor="+$(this).val());
		});
	});
	/*get province select menu for search form*/
	$(function() {
		$("select#search_country").change(function() {
			$("#search_province_div").html(loading);
			//var x_cor = document.getElementById('search_country');	
			$("#search_province_div").load(url+"province-list/?time="+Math.random()+"&ajax=search_true&cor="+$(this).val());
		});
	});
	/*get province select menu for ideal match*/
	$(function() {
		$("select#country_of_residence").change(function() {
			/*$("#province_div").html(loading);*/
			var x_cor = document.getElementById('country_of_residence');	
			$("#cor").load(url+"province-list/?time="+Math.random()+"&ajax=x_true&cor="+ escape($(x_cor).val()));
		});
	});
	/*HOVER FUNCTION PROFILE VIEW*/
	/*clear about grey*/
	$(".clear_about_grey").hover(function () {
	  	$(this).css({'background-color' : 'yellow', 'font-weight' : 'bolder'});
	}, function () {
	 	 var cssObj = {
		'background-color' : '#f5f5f5',
		'font-weight' : '',
		'color' : '#000000'
	  	}
	  	$(this).css(cssObj);
	});
	/*clear about*/
	$(".clear_about").hover(function () {
	  	$(this).css({'background-color' : 'yellow', 'font-weight' : 'bolder'});
	}, function () {
	 	 var cssObj = {
		'background-color' : '#ffffff',
		'font-weight' : '',
		'color' : '#000000'
	  	}
	  	$(this).css(cssObj);
	});
	/*SHOW HIDE DIV PROFILE's
	more_about_user
	ideal_match*/
	$("#more_about_user").hide();
	$("#ideal_match").hide();
	$("#user_photos").hide();
	$("#about_user_link").addClass("user_about_active");
	//user_photos
	/*show about_user*/
	$("#about_user_link").click(function(){
		$("#ideal_match").hide();
		$("#about_user").hide();
		$("#user_photos").hide();
		$("#about_user").animate({width:"100%",height:"toggle"},500);
		$(this).addClass("user_about_active");
		$("#ideal_match_link").removeClass("user_about_active");
		$("#user_photos_link").removeClass("user_about_active");
	});
	/*show ideal_match*/
	$("#ideal_match_link").click(function(){
		$("#about_user").hide();
		$("#user_photos").hide();
		$("#ideal_match").animate({width:"100%",height:"toggle"},500);
		$(this).addClass("user_about_active");
		$("#about_user_link").removeClass("user_about_active");
		$("#user_photos_link").removeClass("user_about_active");
	});
	/*show user photos*/
	$("#user_photos_link").click(function(){
		$("#about_user").hide();
		$("#ideal_match").hide();
		$("#user_photos").animate({width:"100%",height:"toggle"},500	);
		$(this).addClass("user_about_active");
		$("#about_user_link").removeClass("user_about_active");
		$("#ideal_match_link").removeClass("user_about_active");
	});
});/*end $(document).ready(function(){*/