$(document).ready(function(){

	var origTitle = document.title;
	var newTitle = origTitle.replace(" | Meet4Less", "");
	//alert( newTitle) ;
	document.title = 'Meet4Less - '+newTitle;

	/*	Adding classes for IE6 and IE7
	- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -	*/
	if ( $.browser.msie && $.browser.version < "8" ) {
		$("#menu ul li:last-child").addClass("last-child");
		//$(".lastVisitors > div:last-child").addClass("last-child");
	}

	/*	Google Chrome Frame
	- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -	*/
	if ( $.browser.msie && $.browser.version < "7" ) {
		jQuery.facebox( { ajax: '/misc/googleChromeFrame.html' } );
	}

	$('span.friend.tooltip').click( function() {
		location.href= '/list-of-friends';
	});

	$('span.fav.tooltip').click( function() {
		location.href= '/list-of-favourites';
	});

	if ( $.browser.msie && $.browser.version < "7" ) {
        /* Script to enable hover function */
              $('.hoverable').hover(
                   function() {
                       $(this).addClass('hover');
              }, function() {
					$(this).removeClass('hover');
              }
           );
	}

	/*if ( $.browser.msie && $.browser.version < "7" ) {
		$("input[type=radio]").addClass("radio");
		$("input[type=checkbox]").addClass("checkbox");
		$("input[type=submit]").addClass("submit");
		$("input[type=text]").addClass("text");
		$("textarea").addClass("textarea");
		$(".box .highlight > div").addClass("divChild");
	}*/

	/*	.box toggle - TODO - this little shit below won't run on IE6 - we need to fix those grandparents...
	- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -	*/
	$( ".box .content h3 a, .boxDC .content h3 a" ).click( function () {
		$( this ).parent().siblings(".toggleContent").slideToggle( "slow", function(){} );
		$(this).parent().parent().parent().parent().toggleClass( "hidden" );
		$(this).parent().siblings(".chats").find(".chatsWrapper .toggleHighlight").hide("normal");

		if( $(this).parent().parent().parent().parent().hasClass('hidden') ) {
			setCookie( $(this).parent().parent().parent().parent().attr('id')+'State', 0);
		} else {
			if( $(this).parent().parent().parent().parent().attr('id') == 'quickSearchBox' ) {
				if( $(this).parent().parent().find(".jNiceWrapper").length == 0 ) {
					$(this).parent().parent().find("form.jNice").jNice();
				}
			}
			setCookie( $(this).parent().parent().parent().parent().attr('id')+'State', 1);
		}
		return false;
	} );

	$( ".boxDC .summaryDC a" ).click( function () {
		$( this ).parents(".toggleContent").slideToggle("slow");
		$( this ).parents(".toggleContent").siblings(".toggleContent").slideToggle("slow");
		$( this ).parents("#directChatBox").toggleClass("hidden");
	});

	/*	.box2 (Control Panel ) toggle
	- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -	*/
	$( ".controlPanel .content h2 a.toggleIco" ).click( function () {

		if ( $( this ).parents(".controlPanel").is(".hidden") ) {
			$( ".controlPanel .shortContent" ).slideUp( "fast", function() {
				$( ".controlPanel .toggleContent" ).slideDown( "slow");
				$( ".controlPanel" ).toggleClass( "hidden" );
			} );
			setCookie( 'controlPanelBoxState', 1);
		}

		else {
			$(  ".controlPanel .content .toggleContent" ).slideUp( "slow", function() {
				$( ".controlPanel .shortContent" ).slideDown( "slow");
				$( ".controlPanel" ).toggleClass( "hidden" );
			} );
			setCookie( 'controlPanelBoxState', 0);
		}

		return false;
	} );

	/*	facebox rel
	- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -	*/
	$('a[rel*=facebox]').facebox()

	/*	.button
	- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -	*/
	$( ".highlight.buttonOpen div a:first" ).click( function () {

		if ( $( this ).parent().parent( ".highlight" ).hasClass( "button" ) && $( this ).parent().siblings().hasClass( "toggleButton" ) ) {
			$( this ).parent().parent( ".highlight" ).toggleClass( "button" );
			$( this ).parent().parent( ".highlight" ).addClass( "buttonOpen" );
			$( this ).parent().siblings( ".toggleButton" ).slideToggle( "normal" );
		}
		else if ( $( this ).parent().parent( ".highlight" ).hasClass( "buttonOpen" ) && $( this ).parent().siblings().hasClass( "toggleButton" )) {
			$( this ).parent().parent( ".highlight" ).removeClass( "buttonOpen" );
			$( this ).parent().siblings( ".toggleButton" ).slideToggle( "normal", function () {
				$( this ).parent( ".highlight" ).toggleClass( "button" );
			});
		}

		return false;
	});

	/*	Tooltip
	- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -	*/
	/*$('.tooltip').tooltip({
		track: true,
		delay: 0,
		showURL: false,
		fade: 100
	});*/

	$('.tooltip').tooltip({
		track: true,
		delay: 0,
		showURL: false,
		opacity: 1,
		showBody: " - ",
		extraClass: "pretty	fancy small",
		top: 5,
		left: -30
	});

	$(".tooltipBig").tooltip({
		track: true,
		delay: 0,
		showURL: false,
		opacity: 1,
		showBody: " - ",
		extraClass: "pretty	fancy",
		top: 5,
		left: -23
	});

	$('.tooltipImg').tooltip({
		track: true,
		delay: 0,
		showURL: false,
		opacity: 1,
		topMargin: 10,
		showBody: " - ",
		bodyHandler: function() {
        return $("<div class='loading' />").append("<img src="+$( this ).attr("rel")+"/>");
		},
		extraClass: "",
		top: 25,
		left: -30
	});

	/*	Fancy Box
	- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -	*/
	$(".fancyBox > a").fancybox();

	/*	Testing Growl
	- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -	*/
	$.growl.settings.displayTimeout = 3500;

	if(typeof SignalBus != "undefined") {
		SignalBus.listen( 'GROWL_NOTIFICATION', function ( args ) {
			title = args['title'];
			content = args['content'];
			time = args['time'];
			if(time) {
				$.growl(title, content, time);
			} else {
				$.growl(title, content);
			}
			return false;
		} );

		SignalBus.listen( 'SEND_EMAIL_FACEBOX', function ( args ) {
			title = args['title'];
			content = args['content'];
			to = args['to'];
			time = args['time'];

			/*$.facebox('Email message to <strong>'+to+'</strong></br><input type="hidden" name="to" id="messageTo" value="'+to+'" /><br /><input type="text" name="title" id="messageTitle" value="'+title+'" /><br /><textarea id="messageContent" name="content">'+content+'</textarea><a href="javascript:void(0);" onClick="sendEmailMessage();">Send</a><a href="javascript:void(0);" onClick="jQuery(document).trigger(\'close.facebox\');">Cancel</a>');*/
			$.facebox('<div class=" grid-60">'+
						'<a href="javascript:void(0);" onClick="jQuery(document).trigger(\'close.facebox\');" class="exitFacebox" id="close">Close</a>'+
						'<div class="clear"></div>'+
						'<div class="highlight">'+
							'<div class="divChild" style="padding-bottom:19px;">'+
								'<h4>Email message to '+to+'</h4>'+
								'<div class="jNice">'+
								'<p><input type="hidden" name="to" id="messageTo" value="'+to+'" /><label for="messageTitle">Title</label><input type="text" name="title" id="messageTitle" value="'+title+'" /><span class="clear"></span></p>'+
								'<p><label for="messageContent">Message</label><textarea id="messageContent" name="content" style="width:58%; height:15em;">'+content+'</textarea><span class="clear"></span></p>'+
								'<p><label> </label><a href="javascript:void(0);" onClick="sendEmailMessage();" class="submit marginRightDouble">Submit Message</a><a href="javascript:void(0);" onClick="jQuery(document).trigger(\'close.facebox\');" class="submit">Cancel</a></p>'+
								'<div class="clear"></div>'+
							'</div>'+
							'<div class="clear"></div>'+
						'</div>'+
					'</div>');

			return false;
		} );

		SignalBus.listen( 'SIGNAL_EDIT_NOTE', function ( args ) {
			jQuery.facebox('<div id="faceboxInnerDiv"><div style="padding-left:100px;"><a id="close" onClick="jQuery(document).trigger(\'close.facebox\');" class="exitFacebox" href="javascript:void(0);">Close</a><h1>Loading...</h1><p>Please wait...</p></div></div>');
			/// get note with ajax
			paramArray = new Object();
			paramArray['profilesId'] = args.profileId;
			ah.sendImmediately( 'notes', 'get-notes', paramArray, 'putContentsIntoNoteForm' );
			return false;
		} );
	}
}); //endof Document Ready

/*	Open Video Chat popup
	- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -	*/
	function openVideoChat() {
		window.open("http://meet4less.co.uk/chat/index.html","VideoChat","menubar=0,resizable=1,locationbar=0,width="+screen.width+",height="+screen.height );
	}

function sendEmailMessage() {
	paramObject = new Object();
	paramObject['title'] = $('#messageTitle').val();
	paramObject['to'] = $('#messageTo').val();
	paramObject['content'] =$('#messageContent').val();

	jQuery(document).trigger('close.facebox');

	ah.sendImmediately( 'profile-email', 'send-email', paramObject, 'sendEmailMessageRecieve' );
}

function sendEmailMessageRecieve( paramArray ) {
	if( paramArray.status == 'ok' ) {
		$.growl('Notification email sent successfuly!', '');
	}

}


/* TODO: IE6 doesn't like this function name called getCookie - we need to rename it and change everywhere it is executed */
function getaCookie(c_name) {
	if (document.cookie.length>0) {
		c_start=document.cookie.indexOf(c_name + "=");
		if (c_start!=-1) {
			c_start=c_start + c_name.length+1;
			c_end=document.cookie.indexOf(";",c_start);
			if (c_end==-1) c_end=document.cookie.length;
			return unescape(document.cookie.substring(c_start,c_end));
		}
	}
	return "";
}

function setCookie( c_name, value ) {
	expiredays = 1024;
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+";path=/;expires="+exdate.toGMTString();
}

var time = 200;

function putContentsIntoNoteForm( paramArray ) {
	$( '#facebox .body.td .content').html( paramArray.content );
	$( "#noteFormInput" ).focus( function () {
		$( this ).animate({
			"height" : "7em"
		}, time);
		$( this ).siblings("#noteFormSend").animate({
			"height" : "8.2em"
		}, time);
		$( this ).css("line-height", "1.75em");
		if ( $( this ).val() == "Add a note about this user." ) {
			$( this ).val("");
			$( this ).removeClass("grey2");
		}
    });
	$( "#noteFormInput" ).blur( function () {
		$( this ).animate({
			"height" : "3.5em"
		}, time);
		$( this ).siblings("#noteFormSend").animate({
			"height" : "4.6em"
		}, time);
		$( this ).css("line-height", "3.5em");
		if ( $( this ).val() == "" ) {
			$( this ).val("Add a note about this user.");
			$( this ).addClass("grey2");
		}
    });

	$( ".noteRow .noteDetails .box .main, .noteRow .noteDetails .box .top, .noteRow .noteDetails .box .bottom").click( function() {
		$(this).parent().after('<span class="updateNote"><textarea class="">'+$(this).parent().find('p').text()+'</textarea><input type="button" value="save" onClick="updateNote('+$(this).parents('.noteRow').find("input[name='noteId']").val()+');" /></span>');

		/*$( '.noteRow textarea' ).blur( function() {
			$( this ).parents('tr.noteRow').find('.noteDetails .box').fadeIn();
			$( this ).parents('tr.noteRow').find('.updateNote').remove();
		});*/

		$(this).parent().hide();


		$(this).parent().parent().find('.updateNote').fadeIn( function() {
			$(this).parent().find('textarea').focus();
		} );
	});


}

function updateNote( noteId ) {
	paramArray = new Object();
	paramArray['noteId'] = noteId;
	paramArray['newContent'] = $("input[name='noteId'][value='"+paramArray.noteId+"']").parents('tr.noteRow').find('.updateNote textarea').val();
	ah.sendImmediately( 'notes', 'edit-note', paramArray, 'afterNoteIsEdited' );
	$("input[name='noteId'][value='"+paramArray.noteId+"']").parents('tr.noteRow').find('.updateNote input').val('Saving...');
	$("input[name='noteId'][value='"+paramArray.noteId+"']").parents('tr.noteRow').find('.updateNote input, .updateNote textarea').attr('disabled','disabled');
}

function afterNoteIsEdited( paramArray ) {
	if( paramArray.result == 'ok' ) {
		$("input[name='noteId'][value='"+paramArray.noteId+"']").parents('tr.noteRow').find('.box p').text( $("input[name='noteId'][value='"+paramArray.noteId+"']").parents('tr.noteRow').find('.updateNote textarea').val() );
	} else {
		$.growl( 'Could not update note', paramArray.result );
	}
	$("input[name='noteId'][value='"+paramArray.noteId+"']").parents('tr.noteRow').find('.noteDetails .box').fadeIn();
	$("input[name='noteId'][value='"+paramArray.noteId+"']").parents('tr.noteRow').find('.updateNote').remove();

}

function editNote( profileId ) {
	paramObject = new Object();
	paramObject['profileId'] = profileId;
	SignalBus.dispatch( 'SIGNAL_EDIT_NOTE', paramObject );
	return false;
}

function removeNote( noteId ) {
	if( confirm( "Are you sure you want to remove this note?" ) ) {
		paramArray = new Object();
		paramArray['noteId'] = noteId;
		ah.sendImmediately( 'notes', 'delete-note', paramArray, 'afterNoteIsRemoved' );
	}
}

function afterNoteIsRemoved( paramArray ) {
	if( paramArray.result == 'ok' ) {
		$("input[name='noteId'][value='"+paramArray.noteId+"']").parents('tr.noteRow').fadeOut();
	} else {
		$.growl( 'Could not remove note', paramArray.result );
	}
}


function saveNote( profileId ) {
	$.growl( 'Saving Note...', 'Your note for this profile is beeing saved right now.' );
	paramArray = new Object();
	paramArray['profilesId'] = profileId;
	paramArray['note'] = $( '#noteFormInput' ).val();

	$( ".profileSummary .icoProfileNote, .userList tr[rel="+profileId+"] .note.tooltip" ).attr( "title", $('#noteFormInput').val() );
	$('.profileSummary .icoProfileNote.tooltip, .userList tr[rel='+profileId+'] .note.tooltip').tooltip({
		track: true,
		delay: 0,
		showURL: false,
		opacity: 1,
		showBody: " - ",
		extraClass: "pretty	fancy small",
		top: 5,
		left: -30
	});
	ah.sendImmediately( 'notes', 'add-note', paramArray, 'afterNoteIsSaved' );
}

function afterNoteIsSaved( paramArray ) {
	if( paramArray['status'] == 'ok') {
		$.growl('Note Saved!', 'Your note has been saved successfuly');
		jQuery(document).trigger('close.facebox');
	}
}


	/*	Feature Videos
		- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -	*/

$(document).ready(function(){


	$('.helpVideo').click(function () {

			jQuery.facebox( '<a href="javascript:void(0);" onClick="closeFacebox();" class="exitFacebox" id="close">Close</a><div class="clear"></div>'+'<div id="showMeVideo"><p>Please Install <a href="http://get.adobe.com/flashplayer/">Adobe Flash Player</a></p></div>' ,'wideVideo');
			var videoUrl = $(this).attr("rel");
			swfobject.embedSWF("/swf/fPlayerIn.swf", "showMeVideo", "855", "480", "9.0.0","/swf/expressInstall.swf", {playList:"/views/xml/"+videoUrl+".xml"}, {wmode:"transparent"} );

			return false;

	});


});

function showNotesHelpVideo() {

			jQuery.facebox( '<a href="javascript:void(0);" onClick="closeFacebox();" class="exitFacebox" id="close">Close</a><div class="clear"></div>'+'<div id="showMeVideo"><p>Please Install <a href="http://get.adobe.com/flashplayer/">Adobe Flash Player</a></p></div>' ,'wideVideo');
			var videoUrl = 'feature_notes';
			swfobject.embedSWF("/swf/fPlayerIn.swf", "showMeVideo", "855", "480", "9.0.0","/swf/expressInstall.swf", {playList:"/views/xml/"+videoUrl+".xml"}, {wmode:"transparent"} );

			return false;

	}

	function closeFacebox() {
			$( "#facebox .content" ).html("");
			jQuery(document).trigger('close.facebox');
	}

