/**
 * @author Ivor@netjam.nl
 */

var oSexcontact;

function Sexcontact() {
		 
	/**
	 * Main init function
	 */
	this.init = function() {
		this.initGallery();
		this.initWysiwyg();
		this.initSignup();
		this.initSearch();
		this.initAddressbook();
		this.initMessageSelectors();
		this.initMessageButtons();
		this.initListSorter();
	}

	/**
	 * Init profile list sorter
	 */
	
	this.initListSorter = function() {
		$('#listsorter').change( function() {
			var sSorter = $(this).attr('value');
			if(sSorter != '') {
				document.location.href = LINKROOT+'/do/profile/listsort/'+sSorter;
			}
		});
	},
	
	/**
	 * Init message buttons
	 */
	
	this.initMessageButtons = function() {
		
		$('#messagedelmultiple').click( function () {
			if(confirm('Weet je zeker dat je de geselecteerde berichten wilt verwijderen?')) {
				document.messagemultiple.submit();
			}
		});
		
	},
	
	/**
	 * Init message selectors
	 */
	
	this.initMessageSelectors = function() {
		$('a.messageselector').click( function() {
			var sType = $(this).attr('id').replace('message_select_','');
			
			// Deselect all
			$('input.messagecheck').attr({checked:false});
			
			// Select requested
			switch(sType) {
				case 'all':
					$('input.messagecheck').attr({checked:true});
					break;
				
				case 'read':
					$('tr.read input.messagecheck').attr({checked:true});
					break;
				
				case 'unread':
					$('tr.unread input.messagecheck').attr({checked:true});
					break;
			
			}
		});
	},
	
	
	/**
	 * Init search buttons
	 */
	
	this.initSearch = function() {
		
		$('img.search').click( function() {
			
			$('#searchdiv').fadeOut();
			
			var sTarget = $(this).attr('id').replace('search_','');
			var sUrl = LINKROOT+'/view/profile/search/'+sTarget;
			
			$('#searchwrap').load( sUrl+' #searchcontent', function() {
				oSexcontact.initSearch();
				//oSexcontact.initWysiwyg();
				$('#searchdiv').fadeIn();
			});
		});
		
	},
	
	/**
	 * Init addressbook
	 */
	
	this.initAddressbook = function() {
		if($('#tbl_message_to_inline').attr('value') == '') {
			oSexcontact.showAddressbook();
		} else {
			$('#addressbook').click(oSexcontact.showAddressbook);
		}		
	},

	/**
	 * Show addressbook
	 */
	
	this.showAddressbook = function () {
		$('#to_inline').hide();
		$('#to_select').show();
		$('#tbl_message_to_inline').attr({name:'foo'});
		$('#tbl_message_to_select').attr({name:'tbl_message_to'});	
	},
	
	/**
	 * Init signup buttons
	 */
	
	this.initSignup = function() {
		
		$('img.signup').click( function() {
			
			$('#registerdiv').fadeOut();
			
			var sTarget = $(this).attr('id').replace('signup_','');
			var sUrl = LINKROOT+'/view/user/register/'+sTarget;
			
			$('#registerwrap').load( sUrl+' #registercontent', function() {
				oSexcontact.initSignup();
				oSexcontact.initWysiwyg();
				$('#registerdiv').fadeIn();
			});
		});
		
	},
	
	
	/**
	 * Init gallery
	 */
	 this.initGallery = function() {
		$('div.gallery a').lightBox(
				{
				fixedNavigation:true,
				imageBtnClose: LINKROOT+'/images/lightbox/lightbox-btn-close.gif',
				imageBtnPrev: LINKROOT+'/images/lightbox/lightbox-btn-prev.gif',
				imageBtnNext: LINKROOT+'/images/lightbox/lightbox-btn-next.gif',
				imageLoading: LINKROOT+'/images/lightbox/lightbox-ico-loading.gif',
				imageBlank: LINKROOT+'/images/lightbox/lightbox-blank.gif',
				containerResizeSpeed: 200,
				txtImage: LANG_IMAGE,
				txtOf: LANG_OF
				
				});		 
	 },
	 
	 
	 /**
	  * Init WYSIWYG editors
	  */
	 this.initWysiwyg = function() {
		 $('textarea.wysiwyg').each( function() {

			 
			 $(this).tinymce({
				 script_url : LINKROOT+'/extensions/tinymce/tiny_mce.js',
				 theme : "advanced",
				 plugins : "safari,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template",
				 theme_advanced_buttons1 : "save,newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,styleselect,formatselect,fontselect,fontsizeselect",
				 theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,|,insertdate,inserttime,preview,|,forecolor,backcolor",
				 theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr,|,print,|,ltr,rtl,|,fullscreen",
				 theme_advanced_buttons4 : "insertlayer,moveforward,movebackward,absolute,|,styleprops,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,template,pagebreak",
				 theme_advanced_toolbar_location : "top"
			 });
			 
		 });
	 }
	  
}
 
 
 
$(document).ready( function () {
	oSexcontact = new Sexcontact();
	oSexcontact.init();
});
 
 
 
 
