/**
 * @author Brayn
 */

	/**
	*		outOfFocus - input effects for focus/blur
	*/
	jQuery.fn.outOfFocus = function(){
		var obj = jQuery(this[0]);
		var text = arguments[0];

		if (text)
		{
			jQuery(obj).val(text);

			jQuery(obj).blur(function(){
				if (!jQuery(obj).val())
					jQuery(obj).val(text);
			});

			jQuery(obj).focus(function(){
				if (jQuery(obj).val() == text)
					jQuery(obj).val("");
			})
		}
	}

	jQuery(document).ready(function(){
		function IsNumeric(sText)
		{
		   var ValidChars = "0123456789.";
		   var IsNumber=true;
		   var Char;


		   for (i = 0; i < sText.length && IsNumber == true; i++)
		      {
		      Char = sText.charAt(i);
		      if (ValidChars.indexOf(Char) == -1)
		         {
		         IsNumber = false;
		         }
		      }
		   return IsNumber;


	   }
       $('a[name=modal]').click(function(e)
        {
            e.preventDefault();
            var id = $(this).attr('href');

            var maskHeight = $(document).height();
            var maskWidth = $(window).width();

            $('#maskYM').css({'width':maskWidth,'height':maskHeight});
            $('#maskYM').fadeIn(1000);
            $('#maskYM').fadeTo("slow",0.8);

            var winH = $(window).height();
            var winW = $(window).width();

            var scrollTop = document.documentElement.scrollTop;
            $(id).css('top',  winH/2-$(id).height()/2+scrollTop);
            $(id).css('left', winW/2-$(id).width()/2);

            $(id).fadeIn(2000);
        });

        $('.window .close').click(function (e)
        {
            e.preventDefault();
            $('#maskYM').hide();
            $('.window').hide();
        });

        $('#mask').click(function () {
            $(this).hide();
            $('.window').hide();
        });
		/*
		//Lightbox
			jQuery('a[rel*=lightbox]').lightBox({
				keyToClose:"c",
				imageLoading: '/js/libraries/lightbox/images/lightbox-ico-loading.gif',
				imageBtnClose: '/js/libraries/lightbox/images/lightbox-btn-close.gif',
				imageBtnPrev: '/js/libraries/lightbox/images/lightbox-btn-prev.gif',
				imageBtnNext: '/js/libraries/lightbox/images/lightbox-btn-next.gif'
			});
		//############
		*/

    $('.tooltip[title]').bt({
        trigger: 'hover',
        padding: 10,
        width: 200,
        spikeLength: 5,
        spikeGirth: 5,
        cornerRadius: 5,
        //fill: 'rgba(97, 158, 225, .9)',
        strokeWidth: 1,
        //strokeStyle: '#CCC',
        //cssStyles: {color: '#FFF', fontWeight: 'bold'}
    });
    $('.v-tooltip[title]').bt({
        trigger: 'hover',
        positions: ['top'],
        padding: 10,
        width: 200,
        spikeLength: 5,
        spikeGirth: 5,
        cornerRadius: 5,
        fill: 'rgba(97, 158, 225, .9)',
        strokeWidth: 1,
        strokeStyle: '#CCC',
        cssStyles: {color: '#FFF', fontWeight: 'bold'}
    });
    $('.lg-tooltip[title]').bt({
        trigger: 'hover',
        padding: 10,
        width: 450,
        spikeLength: 5,
        spikeGirth: 5,
        cornerRadius: 5,
        fill: 'rgba(255, 255, 255, 1)',
        strokeWidth: 1,
        strokeStyle: '#CCC',
        cssStyles: {color: '#000', fontWeight: 'bold'}
    });
	});
	//jQuery('a[rel*=lightbox]').lightbox();



var attachUploadButton = function(fileToLoad)
{
	if (document.getElementById(fileToLoad))
	{
		jQuery(function(){
			new AjaxUpload(fileToLoad, {
				action: 'ajax/upload_logo.php?file2load='+fileToLoad,
				onComplete: function(file, response){
					document.getElementById(fileToLoad+"_container").innerHTML = "Upload complet.";
					document.getElementById("logo_creare_cont").value = response;
				}
			});
		});
	}
}
var saveAccount = function()
{
	__fields 		= ["user_type", "username", "password", "password2", "name", "email", "judet", "phone"];
	__values 		= ["tip utilizator", "nume utilizator", "parola", "repetare parola", "nume", "email", "judet", "telefon", "prezentare", "prezentare", "denumire agentie", "descriere agentie", "comision inchiriere", "comision luat vanzatorului", "comision luat cumparatorului", "localitate", "adresa"];
	__compares 		= [0, "", "", "", "", 0, "", "", "", "", "", "", "", "", ""];
	__errors 		= "";
	__first_err_fld = "";

    for (i=0;i<__fields.length;i++)
    {
        if (document.getElementById(__fields[i]).value==__compares[i])
        {
            __errors += "\t"+__values[i]+"\n";
            if (__first_err_fld=="")
                __first_err_fld = __fields[i];
        }
    }

	if (__errors!="")
	{
		alert("Unul sau mai multe din urmatoarele campuri sunt incomplete:\n\n"+__errors+"\nVa rugam verificati.")
		document.getElementById(__first_err_fld).focus();
	}
	else
	{
		if (document.getElementById("password").value!=document.getElementById("password2").value)
		{
			alert("Cele doua parole nu coincid. Va rugam verificati.")
			document.getElementById("password2").focus();
		}
        else if (!document.getElementById('username').value.match(/^[a-zA-Z0-9_]*$/))
        {
            alert("Numele de utilizator poate contine doar cifre, litere si caracterul underscore (_).")
            document.getElementById("username").focus();
        }
		else
		{
			if (!isEmail(document.getElementById("email").value))
			{
				alert("Adresa de email este incorecta.")
				document.getElementById("email").focus();
			}
			else
			{
                if (!document.getElementById('accept_t_c').checked)
                {
                    alert('Trebuie sa acceptati termenii si conditiile');
                    return false;
                }
				$.post("ajax/save_account.php", $("#accountForm").serialize(),
					function(data){
						if (data=="ok")
						{
                            document.location.href = '/login.php';
                            return true;
							$('#accountContainer').fadeIn(function() {
								$('#accountContainer').html('<div style="padding:5px;">Contul a fost creat cu succes.<br />Pentru activare, un email de confirmare a fost transmis pe adresa specificata.<br />Verificati atat in mesajele din inbox, cat si in Spam, mailul transmis de noi pentru a va activa acest cont.</div>');
							})
						}
						else
						{
                            if ('Recaptcha' in window)
                                Recaptcha.reload();
							alert(data);
						}
				});
			}
		}
	}
}
//creare cont



var sendToIM = function()
{
	var id_mess = document.getElementById("id_mess").value
	var message = document.getElementById("message").value
	if (id_mess=="")
	{
		alert("Introdu ID-ul de messenger mai intai.");
		document.getElementById("id_mess").focus();
	}
	else
	{
		document.location='ymsgr:sendim?'+id_mess+"&m="+message;
	}
}

var sendToMail = function(articleID)
{
	var emailSend = document.getElementById("emailSend").value
	var emailSubject = document.getElementById("emailSubject").value
	var yourName = document.getElementById("yourName").value

	if (emailSend=="" || emailSubject=="" || yourName=="")
	{
		alert("Toate campurile sunt obligatorii");
		document.getElementById("emailSend").focus();
	}
	else
	{
		$.get('ajax/sendToMail.php?emailSend='+emailSend+'&emailSubject='+emailSubject+'&yourName='+yourName+"&id="+articleID, function(data)
		{
			alert(data);
			document.getElementById("emailSend").value = '';
		});
	}
}

var checkIfSendToIM = function(e)
{
	if (e.keyCode==13)
		sendToIM();
}

window.onload = function()
{
	showHidePresentation();
	attachUploadButton('file_logo');
}
