$(document).ready(function() {
    //$('#copyright').width($(window).width());

    if($('#drawer').length != 0) {
	categs =  ["fete","baieti","universale"];
	var c = categs[Math.floor(Math.random()*categs.length)];
	$('a.trigger').click(function(){
	    drawer($(this).attr('id'));
	});
	drawer(c);
    }
    if($('#mainimage').length != 0 ) {
	if($('#mainimage').width() < 250) {
	    $('#mainimage').css({
		marginTop: Math.floor( ($('#productimage').height()-$('#mainimage').height()) /2 )
	    });
	}
    }
    if($('a.gallery').length != 0) {
	$('a.gallery').fancybox();
    }
    
    $('#search_input').focus(function() {
	if($('#search_input').val() == 'Cauta în tara jucariilor') {
	    $('#search_input').val('');
	}
    });
    $('#search_input').blur(function() {
	if($('#search_input').val() == '') {
	    $('#search_input').val('Cauta în tara jucariilor');
	}
    });
    $('#searchform').submit(function(){
	if($('#search_input').val() == '' ||
	    $('#search_input').val() == 'Cauta în tara jucariilor'){
	    return false;
	}
	else {
	    return true;
	}
    });
    $('#loginemail').focus(function() {
	if($('#loginemail').val() == 'nume@email.ro') {
	    $('#loginemail').val('');
	}
    });
    $('#loginemail').blur(function() {
	if($('#loginemail').val() == '') {
	    $('#loginemail').val('nume@email.ro');
	}
    });
    $('#loginpass').focus(function() {
	if($('#loginpass').val() == 'foobar') {
	    $('#loginpass').val('');
	}
    });
    $('#loginpass').blur(function() {
	if($('#loginpass').val() == '') {
	    $('#loginpass').val('foobar');
	}
    });
    if($(window).height() >= $(document).height()) {
      $('#footer').css({
	  position: "absolute",
	  bottom: 0,
	  width: "100%"
      });
    }
});

function drawer(c) {
    $('#rows').empty();
    $('.drawerSelected').removeClass('drawerSelected');
    $('#'+c).addClass('drawerSelected');
    $.getJSON(base_url+"categorii/ajax/"+c,
	function(data) {
	    jQuery.each(data,function(){
		var t = '<div class="drawerproduct" id="d_p_'+this.idProdus+'">\
			<span class="imgtitle" id="title_'+this.idProdus+'">'+this.nume+'</span>\
			<a id="anc_'+this.idProdus+'" href="'+base_url+this.categs+'_'+this.slug+'.html">\
			<img onmouseover="anim('+this.idProdus+')" onmouseout="animBack('+this.idProdus+')" src="'+base_url+'images/products/'+this.thumb3+'" alt="'+this.nume+'" /></a>\
			</div>';
		$('#rows').append(t);
		$('#d_p_'+this.idProdus).fadeIn('slow');
	    });
	}
    );
}
function anim(n) {
/*    $('#anc_'+n+' img').animate({
	width: "", //$(this).width()+2, //Math.floor($(this).width()/10),
	height: "" //$(this).height()+2 //Math.floor($(this).height()/10)
    }, 300); */
    $('#title_'+n).css({
	opacity: 0,
	marginLeft: 0-Math.floor($('#title_'+n).width()/2)+30,
	display: "block"
    });
    $('#title_'+n).animate({
	marginTop: "-30px",
	opacity: 0.9
    },400);
}
function animBack(n) {
/*    $('#anc_'+n+' > img').css({
	width: "",
	height: ""
    }); */
    $('#title_'+n).animate({
	marginTop: "0px",
	opacity: 0
    },400);
}
function del(rowid) {
    $.get(base_url+'cart/delete/'+rowid,function(data){
	if(data == 'success') {
	    $('#id_'+rowid).hide('slow');
	    updateTotal();
	    return true;
	}
	else {
	    return false;
	}
    });
}
function updateCart() {
    $('.qtyinputs').each(function() {
	var id = $(this).attr('id');
	var value = $(this).val();
	if(value == 0) {
	   del(id);
	}
	else {
	    $.post(base_url+'cart/edit',{rowid : id , qty : value});
	    updateTotal();
	}
    });
    $('#message').show('slow');
    setTimeout('$(\'#message\').hide("slow")',2000);
}
function updateTotal() {
    $.get(base_url+'cart/total',function(data){
	$('#totalprice').html(data+' Lei');
    });
}
