$(window).load(function() {
	// INI
	var title = 'Voitin.com Web Services'; 
	$('#list ul').jScrollPane();
	$("a[href*=http://]").attr("target", "_blank");
	$('a').toolTIP(); 
	
	if(init()) {
		var hoverOnBG = '#101010'; 
		var hoverOnBR = '#101010';
	} else { 
		var hoverOnBG = '#d3d3d3'; 
		var hoverOnBR = '#d3d3d3';
	}
	var hoverOffBG = '#FFFFFF';
	var hoverOffBR = '#A8A8A8';
	
	Cufon.replace('#footer h2, #footer h1, .pachet h3, .pachet .comandaPachet', { fontFamily: 'Myriad Pro', hover: 'true', textShadow: '#000 2px 2px' });
	
	// PACHETE  
	$('.pachet').hover(function() {
		$(this).toggleClass('hover');
		$('h3', this).slideToggle('fast', function() {
			$('.comandaPachet', $(this).parent()).slideToggle('fast');
		}); 
	}, function() {
		$(this).toggleClass('hover');
		$('.comandaPachet', this).slideToggle('fast', function() {
			$('h3', $(this).parent()).slideToggle('fast');
		}); 
	});
	$('.pachet a').click(function(e) {
		e.preventDefault();
		
		var pachetGoogle = $(this).attr('rel');
		_gaq.push(['_trackPageview', pachetGoogle]);
		
		if($('input[name$=pachetulAles]').length > 0) {
			$('input[name$=pachetulAles]').attr('value', 'Pachetul ales: ' + $(this).attr('rel'));
		} else {
			$('#cotatie form input[name$=nume]').parent().prepend('<input type="text" disabled value="Pachetul ales: ' + $(this).attr('rel') + '" name="pachetulAles" style="display: none" />');
			$('input[name$=pachetulAles]').slideToggle();
		}
		$("html").scrollTo($('#contact'), 800);
	});
	 

	// LISTA PROIECTE 
	$('#list ul li').hover(function() {
		$('img', this).css({'background-color': hoverOnBG, 'border-color': hoverOnBR});
		$('a.thumb', this).append('<span><\span>');
		$('a.thumb span', this).slideToggle();
	} , function() { 
		$('img', this).css({'background-color': hoverOffBG, 'border-color': hoverOffBR});
		$('a.thumb span', this).slideToggle(function() {
			$(this).remove();
		});
	});
	
	// FILTRU PROIECTE
	$('.selectieProiecte').click(function() {
		$('.detaliiProiect').fadeOut();
        $('.selectieProiecte').removeClass('active');  
        $(this).addClass('active');
		Cufon.refresh();
		var filtru = $(this).attr('rel');
		
		_gaq.push(['_trackPageview', filtru]);

		if (filtru != 'toate') {
			$('#list ul li[class='+filtru+']').stop().animate({'width' : '228px', 'opacity' : 1, 'marginRight' : '6px', 'marginLeft' : 0});
			$('#list ul li[class!='+filtru+']').stop().animate({'width' : 0, 'opacity' : '0.2', 'marginRight' : 0, 'marginLeft' : 0 });
		} else {
			$('#list ul li').stop().animate({'opacity' : 1, 'width' : '228px', 'marginRight' : '6px', 'marginLeft' : 0 });
		}
		$('#list ul').data('jsp').scrollToY(0);
		return false;
	});
	
	// DETALII PROIECTE
	$('#list li a').unbind('click').bind('click', function() { listareProiect($(this), $('img', this));	});
	if(window.location.hash) { 
		listareProiect($('a[href$='+window.location.hash+']:first'), $('a[href$='+window.location.hash+']:first').find('img')); 
	}  
	function listareProiect(proiect, imagine) {
		$(proiect).parent().parent().parent().before('<div id="containerProiect"></div>');
		document.title = $(imagine).attr('alt') + ' | ' + title;
		$(imagine).css({'opacity' : '0.6'});
		
		var proiectGoogle = $(proiect).attr('href');
		_gaq.push(['_trackPageview', proiectGoogle]);
		
		$.ajax({ 
			url: 'functions.php?proiect='+$(proiect).attr('rel'), 
			success: function(data){   
				$('#containerProiect').html(data);
				$('p.color').each(function() { $(this).css({ 'background-color' : $(this).html() }).html(''); });
				Cufon.replace('.detaliiProiect h1, .detaliiProiect h2', { fontFamily: 'Myriad Pro', hover: 'true', textShadow: '#000 2px 2px' });
				$('#containerProiect').fadeIn();
				$("#containerProiect a[href*=http://]").attr("target", "_blank");
				$('.back').unbind('click').bind('click', function () {  
					window.location.hash = ''; 
					$('#containerProiect').fadeOut(function(){ $(this).remove(); document.title = title; });
					return false;					
				});
			},
			error: function(){
				$('#containerProiect').html("<div class='detaliiProiect' style='font-size: 20px'><center>Serverul este suprasolicitat, ne pare rau pentru aceasta eroare.</center></div>");
				Cufon.replace('.detaliiProiect', { fontFamily: 'Myriad Pro', hover: 'true', textShadow: '#000 2px 2px' });
				$('#containerProiect').fadeIn();
			} 
		});
	}

	// COTATIE DE PRET
	$('#cotatie form .camp').each(function() {
		var default_value = this.value;
		$(this).focus(function() {
			if(this.value == default_value) {
				this.value = '';
			}
		});
		$(this).blur(function() {
			if(this.value == '') {
				this.value = default_value;
			}
		});
	}); 
	$('#cotatie form').prepend('<p id="feedbackForm"></p>').bind('submit', function() { 
	
		var pachet  = $('input[name$=pachetulAles]', this).val(); 
		var nume  = $('input[name$=nume]', this).val();
		var mail  = $('input[name$=mail]', this).val();
		var mesaj  = $('textarea', this).val();
		
		if (nume == '' || nume == 'Numele tau:') {
			if($('#feedbackForm').is(':visible')) { $('#feedbackForm').html('Completeaza numele tau.'); }
			else { $('#feedbackForm').html('Completeaza numele tau.').slideToggle(); }
			return false;
		}
		if (mail == '' || mail == 'Adresa de e-mail:') {
			if($('#feedbackForm').is(':visible')) { $('#feedbackForm').html('Completeaza adresa ta de e-mail.'); }
			else { $('#feedbackForm').html('Completeaza adresa ta de e-mail.').slideToggle(); }
			return false;
		}
		if (!validareEmail(mail)) {
			if($('#feedbackForm').is(':visible')) { $('#feedbackForm').html('Adresa de e-mail este invalida.'); }
			else { $('#feedbackForm').html('Adresa de e-mail este invalida.').slideToggle(); }
			return false;
		}
		if (mesaj == '' || mesaj == 'Detalii despre proiect:') {
			if($('#feedbackForm').is(':visible')) { $('#feedbackForm').html('Completeaza detaliile despre proiect.'); }
			else { $('#feedbackForm').html('Completeaza detaliile despre proiect.').slideToggle(); }
			return false;
		}
		if($('#feedbackForm').is(':visible')) { $('#feedbackForm').html('Te rugam sa astepti...'); }
		else { $('#feedbackForm').html('Te rugam sa astepti...').slideToggle(); }
		
		$.ajax({
			type: 'POST',
			url: '?action=send',
			data: 'mail='+mail+'&nume='+nume+'&mesaj='+mesaj+'&pachet='+pachet,
			success: function(){
				$('#feedbackForm').animate({opacity: 0}, 5000).slideToggle();
				$('#feedbackForm').html('Mesajul a fost trimis cu succes!');
				$('input[name$=nume]', this).attr('value', 'Numele tau:');
				$('input[name$=mail]', this).attr('value', 'Adresa de e-mail:');
				$('textarea', this).val('Detalii despre proiect:');
			},
			error: function(){
				$('#feedbackForm').animate({opacity: 0}, 5000).slideToggle();
				$('#feedbackForm').html('Ups! A aparut o eroare.');
			}
			
		});
		return false;
	}).bind('reset', function() {
		if($('#feedbackForm').is(':visible')) { $('#feedbackForm').slideToggle(); }
		$('input[name$=pachetulAles]').slideToggle('fast', function() {
			$(this).remove();
		});		
	});
	
});

function validareEmail(mail) {
	var ver = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	return ver.test(mail);
}

function init() {
	preGif = new Image(43, 11);
	preGif.src = '../images/load.gif'; 
	preX = new Image(19, 20);
	preX.src = '../images/x.png';
	
	var data = new Date();
	var ora = data.getHours();
	if((ora >= 21 && ora <= 24) && (ora >= 1 && ora <= 7)) {
		$('body').addClass('noapte');
		Cufon.replace('h1, .toate', { fontFamily: 'Myriad Pro', hover: 'true', textShadow: '#101010 1px 1px' });
		return false;
	} else {
		Cufon.replace('h1, .toate', { fontFamily: 'Myriad Pro', hover: 'true', textShadow: '#FFF 1px 1px' });
		return true;
	}
	 
} 
