var _prefix = '/';

$(window).load(function() 
{

	$("a.new_user_login").fancybox({type: 'iframe', height: 319, onStart: function(){ $(".formError").remove();}});
	
	$('#slider').nivoSlider({effect: 'fade', directionNav: true, directionNavHide: true, controlNav: true, controlNavThumbs: false, controlNavThumbsFromRel: false, pauseOnHover: true, pauseTime: 6000});

	$("a.imagineLink").fancybox({titlePosition: 'over', titleFormat: 'formatTitle'});
  
  $("a.adauga").fancybox({type: 'inline', autoDimensions: true, modal: true, showCloseButton: false, href: "#cos_pop_up", speedIn: 100, speedOut: 100});
	
	// cos
	$("a.adauga").click(function(e){
		e.preventDefault();
		var id = $(this).attr('rel');
		var from = $(this).attr('rev');
		addToCart(id, from);
	});
	$("a.sterge").click(function(e){		
		e.preventDefault();
		if(confirm('Esti sigur ca doresti sa stergi acest produs?'))
		{
			var id = $(this).attr('rel');
			window.location.href = _prefix + 'cos/sterge/' + id;
		}
	});
	$("a#modifica").click(function(e){
		e.preventDefault();
		$("form#frmCart").submit();
	});
	$("a#goleste").click(function(e){
		e.preventDefault();
		if(confirm('Esti sigur ca doresti sa golesti cosul?'))
		{
			window.location.href = _prefix + 'cos/goleste/';
		}		
	});
	$("select#judet").change(function(){
		var id = $(this).val();
		$.post(_prefix + 'ajax/getCities', {id: id}, function(data){
	    var options = '';
	    for (var i = 0; i < data.length; i++) {
	      options += '<option value="' + data[i].id + '">' + data[i].nume + '</option>';
	    }
	    $("select#localitate").html(options);
		});	
	});
	$("select#judet_livrare").change(function(){
		var id = $(this).val();
		$.post(_prefix + 'ajax/getCities', {id: id}, function(data){
	    var options = '';
	    for (var i = 0; i < data.length; i++) {
	      options += '<option value="' + data[i].id + '">' + data[i].nume + '</option>';
	    }
	    $("select#localitate_livrare").html(options);
		});	
	});
	$("select#facturare_judet").change(function(){
		var id = $(this).val();
		$.post(_prefix + 'ajax/getCities', {id: id}, function(data){
	    var options = '';
	    for (var i = 0; i < data.length; i++) {
	      options += '<option value="' + data[i].id + '">' + data[i].nume + '</option>';
	    }
	    $("select#facturare_localitate").html(options);
		});	
	});
	
	$("div#comanda_date_personale").click(function(){
		var display = $("div#date_personale").css('display');
		if(display == 'none')
		{
			$("div#date_personale").slideDown();
			$("div#comanda_date_personale").css('background-image', 'url(images/minus_comanda.png');
		}
		else
		{
			$("div#date_personale").slideUp();
			$("div#comanda_date_personale").css('background-image', 'url(images/plus_comanda.png');
		}
	});

	$("div#comanda_continut_cos").click(function(){
		var display = $("div#continut_cos").css('display');
		if(display == 'none')
		{
			$("div#continut_cos").slideDown();
			$("div#comanda_continut_cos").css('background-image', 'url(images/minus_comanda.png');
		}
		else
		{
			$("div#continut_cos").slideUp();
			$("div#comanda_continut_cos").css('background-image', 'url(images/plus_comanda.png');
		}		
	});
	
	$("div#filtre .top").click(function(){
		var display = $("div#filtre .bot").css('display');
		if(display == 'none')
		{
			$("div#filtre .bot").slideDown();
			$("div#filtre .top").css('background-image', 'url(images/minus_comanda.png');
		}
		else
		{
			$("div#filtre .bot").slideUp();
			$("div#filtre .top").css('background-image', 'url(images/plus_comanda.png');
		}
	});
	
	$('a.btn-crescator').click(function(e){
		e.preventDefault();
		var nr = parseInt($(this).prev().children('input').val());
		$(this).prev().children('input').attr('value', nr+1);
	});
	$('a.btn-descrescator').click(function(e){
		e.preventDefault();
		var nr = parseInt($(this).prev().prev().children('input').val());
		if(nr > 0)
			$(this).prev().prev().children('input').attr('value', nr-1);
	});
	
	$("form input[type=text],form textarea,form input[type=password]").watermark();
	
	$("input#adresa_diferita").click(function(){
		$("div#adresa_diferita_container").toggle('fast');		
	});
	$("input#facturare_diferita").click(function(){
		$("div#facturare_diferita_container").toggle('fast');
	});
	
	$("a#sendOrder").click(function(e){
		e.preventDefault();
		$(this).closest('form').submit();
	});
	
	$("#comanda_rapida").tooltip({ position: "top center", effect: 'slide'});
	
	$("input#newsletterAdd").click(function(e){
		e.preventDefault();		
		$.post(_prefix + 'ajax/addEmail', {email: $("input#newsletterEmail").attr('value')}, function(data){
			eval(data);
		});
	});
	
	$("div.clasa_proprietati div:last-child").prev().css('border', 'none');
	
	$("div#ordonare").hoverIntent({
		sensitivity: 10,
		interval: 60,
		over: function(){
			//$("a#categorii").addClass('hover');
			$('div#ordonare ul').slideDown();
		},
		timeout: 500,
		out: function(){
			//$("a#categorii").removeClass('hover');
			$('div#ordonare ul').slideUp();
		}		
	});
	
	//$("div#right_cart").middle();
	$("div#right_cart_link a").click(function(e){
		e.preventDefault();
		if($("div#right_cart_content").css("display")=="block")
			$("div#right_cart_content").slideUp();
		else
			$("div#right_cart_content").slideDown();
		});
	
  $('a.print').click(function(e){
    e.preventDefault();
    w=window.open();
    w.document.write('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head>');
    w.document.write('<link href="style/reset.css" rel="stylesheet" type="text/css" /><link href="style/style.css" rel="stylesheet" type="text/css" /></head><body style="background: #fff; width: 720px">');
    w.document.write($('#print_produs').html());
    w.document.write('</body></html>');
    w.print();
    //w.close();
  });
	
	$("#sendContact").click(function(e){
		e.preventDefault();
		$("#frmContact").submit();
	});

	$("form.validate").validationEngine({promptPosition : "topLeft:0,7", scroll: false});

	$(".slide_likebox").hover(function(){
		$(".slide_likebox").stop(true, false).animate({right:"0"},"medium");
		},
		function(){
		$(".slide_likebox").stop(true, false).animate({right:"-222"},"medium");
	},500); 
	
});

function addToCart(id, from)
{
	$.post(_prefix + 'ajax/addToCart', {id: id, from: from}, function(data){
		$('#prod_cnt').html(data.cnt);
		$("#prod_total").html(data.total  + ' RON');
		
		if((data.cnt)==0)
		{
			$("#cos").removeClass("cos_plin");
			$("#cos").addClass("cos_gol");
		}
		else
		{
			$("#cos").removeClass("cos_gol");
			$("#cos").addClass("cos_plin");
		}
		//$.fancybox({padding: 0, margin: 0, showCloseButton: false, showNavArrows: false,href:'#cos_pop_up',modal: 'true', overlayShow: true, 'overlayColor'  : '#090909', 'overlayOpacity' : 0.2});
	});
}

jQuery.fn.middle = function () {
    this.css("top", (($(window).height() - this.outerHeight()) / 2) + $(window).scrollTop() + "px");
    return this;
}

function formatTitle(title, currentArray, currentIndex, currentOpts) {
    return '<div id="tip7-title">' + (title && title.length ? '<b>' + title + '</b>' : '' ) + 'Imaginea ' + (currentIndex + 1) + ' din ' + currentArray.length + '</div>';
}
