//Domready
var global;
var slide = 0;
var t;


$(document).ready(function(){
    if ($('#pantofi').length){
        if ($.cookie('pantofi')==null){
            setTimeout('show_site()', 3000);
            $.cookie('pantofi','1');
        }

    }

    if ($('#big_menu').length){

        $('#main_menu li a.city_glam').parent('li').addClass('over');
        $('#main_menu li a').bind("mouseenter", function(e){
            $('#main_menu li').removeClass('over');
            class_name = $(this).attr("class");
            $('#big_menu li').not('.city_glam').hide();
            $('#big_menu li.'+class_name).show();
            $(this).parent('li').addClass('over');
        });

    }

    if ($('#slide_menu').length){
        var w_menu = $("#slide_menu").width();
        var nr_li = $("#slide_menu li").not('.curand').length;
        if (nr_li==0) return;
        var w_li = Math.round(w_menu/nr_li);
        var w_max = 336;
        var w_min = Math.round((w_menu-w_max)/(nr_li-1));
//        if (nr_li==3) w_min -= 1;
        var w_last = w_menu-(nr_li-1)*w_li-nr_li;
        var w_min_last = w_menu-w_max-(nr_li-2)*w_min-nr_li;
        $("#slide_menu li").width(w_li);
        $("#slide_menu li.last").width(w_last);
//        $("#slide_menu li:eq(8)").width(50);
        $("#slide_menu li").hover(
            function(){
                $(this).addClass('over');
                $('#slide_menu li').not('.over').animate({ width: w_min}, {queue:false, duration:"normal"} );
                $("#slide_menu li.last").not('.over').animate({ width: w_min_last}, {queue:false, duration:"normal"} );
                if ($(this).attr('class')=='last over') $(this).animate({ width: w_max-(nr_li*1.35) }, {queue:false, duration:"normal"} );
                else $(this).animate({ width: w_max }, {queue:false, duration:"normal"} );
                $(this).children("div").animate({opacity: "show"}, "fast");
            },
            function(){
                $(this).removeClass('over');
                $('#slide_menu li').not('last').animate({ width: w_li }, {queue:false, duration:"normal"});
                $("#slide_menu li.last").animate({ width: w_last }, {queue:false, duration:"normal"});
                $(this).children("div").animate({opacity: "hide"}, "fast");
            });
    }

    $('#listaProduse li').hover(function(){$(this).addClass('over')}, function(){$(this).removeClass('over')});

    var galleryPopup =	$(".imagini a").overlay({
                    oneInstance: false,
                    api: true,
                    expose: {
                    color: '#333',
                    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: '#333',
                loadSpeed: 'fast',
                closeSpeed:'fast',
                opacity: 0.9
            }
    });
    set_input_cifre();
    

    if ($('#produse_new_in').length>0) setInterval(function(){set_newin_slide();},5000);
})

function show_site(){
    $('#pantofi').animate({opacity: "hide"}, "normal");
    $('#wrapper').animate({opacity: "show"}, "normal");
}

function set_newin_slide(){
    n = $('#produse_new_in li').length;
    $('#produse_new_in li:eq('+slide+')').animate({opacity: "hide"}, "slow");
    slide = slide+1;
    if (slide==n) slide=0;
    $('#produse_new_in li:eq('+slide+')').animate({opacity: "show"}, "slow");
}

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);

	$(".overlay_thumbs_scrolable li:not(.active) img").animate({opacity: "0.4"}, 200);
}

function set_input_cifre(){
    $("input.numeric").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_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(result) {global = result;},
        async:   false
    });
//    $.post('index.php',{jax:'adresa_email', email:email_address}, function(data){console.log(data);return data;}, 'json')
}

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_confirmare(frm){
    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_contact(frm){
    $('#'+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()==''){
            $(obj).addClass('error');
            $(obj).bind('keypress', function(e){$(this).removeClass('error'); });
            total_erori++;
        }
    }
    if (total_erori){
        $('#'+frm.id+' .msg').html('Completati toate campurile').show();
        return false;
    }else if (!isValidEmailAddress($('#email').val())){
        $('#email').addClass('error');
        $('#email').bind('keypress', function(e){$(this).removeClass('error'); });
        $('#'+frm.id+' .msg').html('Adresa de email este invalida').show();
        return false;
    }


    return true;
}

function verifica_buget(frm){
    $('#'+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()==''){
            $(obj).addClass('error');
            $(obj).bind('keypress', function(e){$(this).removeClass('error'); });
            total_erori++;
        }
    }
    if (total_erori){
        $('#'+frm.id+' .msg').html('Completati campurile marcate cu rosu').show();
        return false;
    }else if (!isValidEmailAddress($('#email').val())){
        $('#email').addClass('error');
        $('#email').bind('keypress', function(e){$(this).removeClass('error'); });
        $('#'+frm.id+' .msg').html('Adresa de email este invalida').show();
        return false;
    }

    frm.submit();
}
