/**
 *	MAIN JS
 *	http://www.ioscoop.com
 *
 *	Copyright 2011, Nescio Hermitage
 *	
 *	@version 2011/02/21
 *	@author Thijs Zandvliet
 */

	function main()
	{
	
		/* set the location of the ajax file */
		this.dest = '/engine/ajax/ajax.php';
		
	
		/**
		 *	Initialize
		 */
		this.init = function()
		{
			$('#dialog').dialog({
				autoOpen: false,
				title: 'Dialog',
				width: 400,
				closeOnEscape: true,
				draggable: true,
				resizable: false,
				modal: true
			});
			
			$("ul.sf-menu").supersubs({ minWidth: 14, maxWidth: 30, extraWidth: 1 }).superfish();
			$(".rouwinner").css('opacity', 0.9);
		}
		
		
		this.openUpload = function()
		{
			window.location = "/upload/" + $('#album').val();
		}
		
		
		this.changeLanguage = function(lang)
		{
			if(lang == 'nl' || lang == 'en') value = lang;
			else value = $('#profile_language').val();
		
			req = "f=profile&action=language&lang=" + value;
			$.get(this.dest, req, function(){
				location.reload();
			})
		}
    
    
    this.showOverlay = function(title,data)
    {
			$('#dialog').html(data);
      $('#dialog').dialog('option', 'title', title);
      setTimeout("$('#dialog').dialog('open')", 100);
    }
		
		
		this.dialogBusy = function(msg)
		{
			$('#dialog').dialog('option', 'buttons', {  });
			data = '<center>' + msg + '<br /><br /><img src="/images/spinner.gif" /></center>';
			main.showOverlay('Bezig...',data);
		}
		
		
		this.fadeOut = function(id)
		{
			$("#item" + id).fadeOut("slow",function() {
				$("#item" + id).remove();
				$("#line" + id).remove();
			});
		}
		
		
		this.initEmail = function()
		{
			$('#btnContinue').click(function() {
				$('#tdContinue').html('<img src="/images/spinner.gif" /><br />Uw gegevens worden gecontroleerd...');
				$.fancybox.resize();
				main.retrieveEmails();
			});
			
			$('#selectAll').click(function() {
				main.toggleChecked(this.checked);
			});
			
			$('#btnSend').click(function() {
				main.sendInvitations();
			});
		}
		
		
		this.retrieveEmails = function()
		{
			var results = $('#frmEmail').serialize();
			results = decodeURI(results);
		
			req = 'f=emails&action=retrieve&' + results;
			$.get(this.dest, req, function(data) {
				if(data == 1)
				{
					$('.fouttekst').html('Inloggevens onjuist !');
					$('#tdContinue').html('<input id="btnContinue" class="subbut" type="button" name="continue" value="Ga verder" />');
					main.initEmail();
					$.fancybox.resize();
				}
				else $.fancybox(data);
			});
		}
		
		
		this.sendInvitations = function()
		{
			req = 'f=emails&action=send';
			$(".chkbox").each(function() {
				if($(this).attr("checked")) req += '&email[]=' + $(this).parent().parent().children('.email').html();
			});
			
			$.get(this.dest, req, function(data) {
				$.fancybox(data);
				$.fancybox.resize();
				setTimeout("window.location = '/'", 1000);
			});
		}
		
		
		this.toggleChecked = function(status)
		{
			$(".chkbox").each(function() {
				$(this).attr("checked",status);
			})
		}
	
	}
	
	
	/**
	 *	Create a new main js object
	 */
	$(document).ready(function() {
		main = new main();
		main.init();
	});
