//Domready
var global;
var slide = 0;
var t, o, tmp;


$(document).ready(function(){
//    $('#listaProduse li').hover(function(){$(this).addClass('over')}, function(){$(this).removeClass('over')});

    var galleryPopup =	$(".imagini a").overlay({
                    oneInstance: false,
                    api: true,
                    expose: {
                    color: '#9ECF9B',
                    loadSpeed: 'fast',
                    closeSpeed:'fast',
                    opacity: 0.9
                }
        });
	$("div.overlay_thumbs_scrolable").scrollable({
			vertical: true,
			size:2,
			items: '.overlay_thumbs'
	});

	$(".overlay_thumbs_scrolable li img").animate({opacity: "0.4"});

    $("a#top_creare_cont[rel],a#top_login[rel],a#aut_creare_cont[rel],a#aut_login[rel]").overlay({
                expose: {
                color: '#9ECF9B',
                loadSpeed: 'fast',
                closeSpeed:'fast',
                opacity: 0.9
            }
    });
    set_input_cifre();

	if ($('#carousel').length){

	$('#carousel').infiniteCarousel({
	transitionSpeed : 500,
	displayTime : 5000,
	displayProgressBar : false,
	displayThumbnailNumbers: true,
	displayThumbnailBackground: false,
	showControls: true
});}

    $(document).mousemove(function(e){
	 o = $('#left').offset();
	 if ((e.pageX>=o.left) && (e.pageX<=o.left+$('#left').width())) tmp = 1;
	 else $('#left ul.over').not('.selectat').removeClass('over');
   });

   $('ul.produse li.sumar .pret_redus').not('.nopromo').parent().append('<img src="images/promo.gif" alt="Promotie" class="promo" />');
   $('#produs .pret_vechi').not('.nopromo').parent().parent().append('<img src="images/promo.gif" alt="Promotie" class="promo" />');

})


function openGallery(img, id){
	$('#largeImage').attr('src', img);
	$('.imagini a').overlay().load();
	$(".overlay_thumbs_scrolable li img").animate({opacity: "0.4"}, 200);
	$('#thumb' + id).addClass('active');
	$('#thumb' + id).find("img").animate({opacity: "1"}, 200);
}
function loadImage(node, img){
	node = node.parentNode;
  $(".overlay_thumbs_scrolable li").removeClass('active');
	$(node).addClass('active');
	$(node.firstChild.firstChild).animate({opacity: "1"}, 200);
	$('#largeImage').attr('src', img);
	$('#img_title').html(''+$(node.firstChild).attr('title'));

	$(".overlay_thumbs_scrolable li:not(.active) img").animate({opacity: "0.4"}, 200);
}


function set_input_cifre(){
    $("input.numeric,input.cifre").keypress(function (e)
    {
        if (e.which==44 || e.which==46) if ($(this).val().indexOf('.')!=-1) return false;
        if( e.which!=8 && e.which!=0 && (e.which<48 || e.which>57) && (e.which!=46)) return false;
    });
}

function verifica_creare_cont(frm){
    n = $('#'+frm.id+' .input_txt').length;
    total_erori = 0;
    $('#'+frm.id+' .msg').removeClass('error').html('').hide();
    for (i=0; i<n; i++){
        obj = $('#'+frm.id+' .input_txt:eq('+i+')');
        if ($(obj).val()==''){
            $(obj).addClass('error');
            $(obj).bind('keypress', function(e){$(this).removeClass('error');});
            total_erori++;
        }
    }

    if (total_erori){
        $('#'+frm.id+' .msg').html('Completati toate campurile');
        $('#'+frm.id+' .msg').addClass('error').show();
        return false;
    }

    if (!isValidEmailAddress($('#cont_email').val())){
        $('#cont_email').addClass('error');
        $('#cont_email').bind('keypress', function(e){$(this).removeClass('error');});
        $('#'+frm.id+' .msg').html('Adresa de email este invalida');
        total_erori++;
    }else{
        verifica_email($('#cont_email').val());
        if(global=='1'){
            $('#cont_email').addClass('error');
            $('#cont_email').bind('keypress', function(e){$(this).removeClass('error');});
            $('#'+frm.id+' .msg').html('Adresa de email este deja inregistrata');
            total_erori++;        
            global = "";
        }        
    }

    if ($('#cont_parola').val()!=$('#cont_repeta').val()){
        $('#cont_parola,#cont_repeta').addClass('error');
        $('#cont_parola,#cont_repeta').bind('keypress', function(e){$(this).removeClass('error');});
        if (!total_erori) $('#'+frm.id+' .msg').html('Parola nu a fost repetata corect');
        else $('#'+frm.id+' .msg').html($('#'+frm.id+' .msg').html()+'<br />Parola nu a fost repetata corect');
        total_erori++;
    }

    if (total_erori){
        $('#'+frm.id+' .msg').addClass('error').show();
        return false;
    }

    return true;
}

function verifica_concurs(frm){
    n = $('#'+frm.id+' .mandator').length;
    total_erori = 0;
    $('#'+frm.id+' .msg').removeClass('error').html('').hide();
    for (i=0; i<n; i++){
        obj = $('#'+frm.id+' .mandator:eq('+i+')');
        if ($(obj).val()==''){
            $(obj).addClass('error');
            $(obj).bind('keypress change', function(e){$(this).removeClass('error');});
            total_erori++;
        }
    }

    if (total_erori){
        $('#'+frm.id+' .msg').html('Completati marcate ca necompletate');
        $('#'+frm.id+' .msg').addClass('error').show();
        return false;
    }

	if (!$('.agree').attr('checked')){
        $('#'+frm.id+' .msg').html('Trebuie sa fiti de acord cu termenii si conditiile de participare');
        $('#'+frm.id+' .msg').addClass('error').show();
        return false;
	}

    if (!isValidEmailAddress($('#email').val())){
        $('#email').addClass('error');
        $('#email').bind('keypress', function(e){$(this).removeClass('error');});
        $('#'+frm.id+' .msg').html('Adresa de email este invalida');
        total_erori++;
    }

    if (total_erori){
        $('#'+frm.id+' .msg').addClass('error').show();
        return false;
    }

    return true;
}

function verifica_email(email_address){
    jQuery.ajax({
        type: 'POST',
        url:  'index.php',
        data: 'jax=adresa_email&email='+email_address,
        success: function(result) {global = result;},
        async:   false
    });
//    $.post('index.php',{jax:'adresa_email', email:email_address}, function(data){console.log(data);return data;}, 'json')
}

function verifica_cont(email_address, parola){
    
	jQuery.ajax({
        type: 'POST',
        url:  'index.php',
        data: 'jax=verifica_cont&email='+email_address+'&parola='+parola,
        success: function(data) {global = data;},
        async:   false
    });
	
	
    //$.post('index.php',{jax:'verifica_cont', email:email_address, parola:parola}, function(data){console.log(data);return data;}, 'json');
	//global = 0;
}

function logout(){
    $.cookie('usses', null, {path:'/'});
    location.href=location.href;
}

function isValidEmailAddress(emailAddress) {
    var pattern = 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 pattern.test(emailAddress);
}

function sterge_din_favorite(id){
    $.post('index.php',{jax:'sterge_favorit', id:id}, function(data){$('#fav_'+id).remove();})
}

function verifica_login(frm){
    n = $('#'+frm.id+' .input_txt').length;
    total_erori = 0;
    $('#'+frm.id+' .msg').removeClass('error').html('').hide();
    for (i=0; i<n; i++){
        obj = $('#'+frm.id+' .input_txt:eq('+i+')');
        if ($(obj).val()==''){
            $(obj).addClass('error');
            $(obj).bind('keypress', function(e){$(this).removeClass('error');});
            total_erori++;
        }
    }

    if (total_erori){
        $('#'+frm.id+' .msg').html('Completati toate campurile');
        $('#'+frm.id+' .msg').addClass('error').show();
        return false;
    }

    if (!isValidEmailAddress($('#login_email').val())){
        $('#login_email').addClass('error');
        $('#login_email').bind('keypress', function(e){$(this).removeClass('error');});
        $('#'+frm.id+' .msg').html('Adresa de email este invalida');
        total_erori++;
    }else{
		
        verifica_cont($('#login_email').val(),$('#login_parola').val());
        if(global=='0'){
            $('#login_email,#login_parola').addClass('error');
            $('#login_email,#login_parola').bind('keypress', function(e){$(this).removeClass('error');});
            $('#'+frm.id+' .msg').html('Adresa de email sau parola este invalida');
            total_erori++;
            global = "";
        }
    }

    if (total_erori){
        $('#'+frm.id+' .msg').addClass('error').show();
        return false;
    }

    location.href=location.href;

    return false;
}

function verifica_parola_uitata(frm){
    n = $('#'+frm.id+' .input_txt').length;
    total_erori = 0;
    $('#'+frm.id+' .msg').removeClass('error').html('').hide();
    for (i=0; i<n; i++){
        obj = $('#'+frm.id+' .input_txt:eq('+i+')');
        if ($(obj).val()==''){
            $(obj).addClass('error');
            $(obj).bind('keypress', function(e){$(this).removeClass('error');});
            total_erori++;
        }
    }

    if (total_erori){
        $('#'+frm.id+' .msg').html('Completati adresa de email');
        $('#'+frm.id+' .msg').addClass('error').show();
        return false;
    }

    if (!isValidEmailAddress($('#p_email').val())){
        $('#p_email').addClass('error');
        $('#p_email').bind('keypress', function(e){$(this).removeClass('error');});
        $('#'+frm.id+' .msg').html('Adresa de email este invalida');
        total_erori++;
    }else{
        verifica_adresa_email($('#p_email').val());
        if(global=='0'){
            $('#p_email').addClass('error');
            $('#p_email').bind('keypress', function(e){$(this).removeClass('error');});
            $('#'+frm.id+' .msg').html('Aceasta adresa de email nu exista');
            total_erori++;
            global = "";
        }
    }

    if (total_erori){
        $('#'+frm.id+' .msg').addClass('error').show();
        return false;
    }

	$('#'+frm.id+' .msg').html('Noua parola a fost trimisa pe adresa dvs. de email<br />Verificati casuta dvs. de email inclusiv in &quot;Junk&quot;');
	$('#'+frm.id+' .msg').addClass('error').show();
	$('#'+frm.id+' .white').hide();

	return false;
    
}

function verifica_adresa_email(email_address){
    jQuery.ajax({
        type: 'POST',
        url:  'index.php',
        data: 'jax=verifica_email&email='+email_address+'&rand='+Math.random(),
        success: function(result) {global = result;},
        async:   false
    });
	
}


function verifica_confirmare(frm){
	if ($('#'+frm.id+' input[name="mod_plata"]:checked').val()=='3') frm.action.value = 'romcard';
	else frm.action.value = 'finalizare';

    if ($('#tip_pf').is(':checked')) except = "form_pj";
    else except = "form_pf";

    $('#'+frm.id+' .mandator').removeClass('error');
    n = $('#'+frm.id+' .mandator').length;
    total_erori = 0;
    $('#'+frm.id+' .msg').removeClass('error').html('').hide();
    for (i=0; i<n; i++){
        obj = $('#'+frm.id+' .mandator:eq('+i+')');
        if ($(obj).val()=='' && except!=$(obj).parent().parent().attr('id')){
            $(obj).addClass('error');
            $(obj).bind('keypress', function(e){$(this).removeClass('error');});
            total_erori++;
        }
    }
    if (total_erori) $('#'+frm.id+' .msg').html('Completati toate campurile<br/>');
    if ($('#'+frm.id+' input[name="mod_plata"]:checked').length==0){
        total_erori++;
        $('#'+frm.id+' .msg').html($('#'+frm.id+' .msg').html()+'Alegeti modul de plata<br>');
    }
    if ($('#'+frm.id+' input[name="mod_livrare"]:checked').length==0){
        total_erori++;
        $('#'+frm.id+' .msg').html($('#'+frm.id+' .msg').html()+'Alegeti modul de livrare<br>');
    }

    if (total_erori){
        $('#'+frm.id+' .msg').show();
        return false;
    }

    frm.submit();
}

function verifica_contulmeu(frm, formid){
    $('#'+formid+' .mandator').removeClass('error');
    n = $('#'+formid+' .mandator').length;
    total_erori = 0;
    $('#'+formid+' .msg').removeClass('error').html('').hide();
    for (i=0; i<n; i++){
        obj = $('#'+formid+' .mandator:eq('+i+')');
        if ($(obj).val()==''){
            $(obj).addClass('error');
            $(obj).bind('keypress', function(e){$(this).removeClass('error');});
            total_erori++;
        }
    }
    if (total_erori){
        $('#'+formid+' .msg').html('Completati toate campurile').show();
        return false;
    }

    return true;
}

function click_cauta(frm){
	if (frm.q.value==''){
		frm.q.focus();
		return false;
	}else{
		window.location.href=$('#logo').attr('href')+'c/'+frm.q.value.replace(" ", "+");
		return false;
	}
}
