var vurl = window.top.location.toString();

function number_format(a, b, c, d) {
// number_format(number, decimals, comma, formatSeparator)
a = Math.round(a * Math.pow(10, b)) / Math.pow(10, b);
e = a + '';
f = e.split('.');
if(!f[0]) f[0] = '0';
if(!f[1]) f[1] = '';
if(f[1].length < b){
g = f[1];
for(i = f[1].length + 1; i <= b; i++) {
g += '0';
}
f[1] = g;
}
if(d != '' && f[0].length > 3) {
h = f[0];
f[0] = '';
for(j = 3; j < h.length; j += 3) {
i = h.slice(h.length - j, h.length - j + 3);
f[0] = d + i +f[0] + '';
}
j = h.substr(0, (h.length % 3 == 0) ? 3 : (h.length % 3));
f[0] = j + f[0];
}
c = (b <= 0) ? '': c;
return f[0] + c + f[1];
}


function format_number(nr) {
nr = nr.toString();
var exp = nr.split('.');
var cnt = exp.length;
return (cnt >= 2 ? exp[0]+'.'+(exp[1].length == 1 ? exp[1]+'0' : exp[1].substr(0, 2)) : nr+'.00');
}


function is_numeric(sText){
var ValidChars = "0123456789";
var IsNumber = true;
var Char;

for (i = 0; i < sText.length && IsNumber == true; i++){
 Char = sText.charAt(i);
 if (ValidChars.indexOf(Char) == -1){
IsNumber = false;
}
}
return IsNumber;
}
function isEmail(str) {
var filter= /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
return (filter.test(str) ? true : false);
};
function fitSuc(){
$("span.success.fit").fadeOut(1500);
}
function fitErr(){
$("span.error.fit").fadeOut(1500);
}



function get_comanda() {
if(vurl.indexOf('/produs/') == -1) {
$('.hasProdus').click(function(){
alert('Pentru a adauga acest voucher in cos, trebuie sa finalizati comanda produselor!');
return false;
});

$('.hasVoucher').click(function(){
alert('Pentru a adauga acest produs in cos, trebuie sa finalizati comanda voucherului!');
return false;
});


$('a.comanda').click(function(){
var vareCadou = $(this).attr('class').split(' ')[1];

if(vareCadou != undefined && vareCadou == 'areCadou') {
alert('Pentru a adauga acest produs in cos, trebuie sa finalizati comanda cadoului aniversar!');
return false;
} else if(vareCadou != undefined && vareCadou == 'areProdus') {
alert('Pentru a adauga acest produs in cos, trebuie sa finalizati comanda curenta!');
return false;
} else return true;
});
}

$('a.comanda2').click(function(){
alert('Ne pare rau, nu puteti adauga acest produs in cos deoarece este indisponibil!');
return false;
});
}



wait2 = null;
total2 = 1;
current_promo2 = 1;

function showPromo2(promo2, h1) {
var vh1 = h1;
if(vh1.indexOf('~') > 1) {
clearTimeout(wait2);
current_promo2 = promo2;

vh1s = vh1.split('~');

$('#topbar h1 span').fadeOut();
$('#topbar h1 span').empty().html(vh1s[promo2-1]).fadeIn();

wait2 = setTimeout("nextPromo2('"+h1+"')", 4000);
}
}

function nextPromo2(h1) {
vh1s = h1.split('~');
total2 = vh1s.length;
if(current_promo2 < total2) showPromo2(parseInt(current_promo2)+1, h1);
else showPromo2(1,h1);
}


$(document).ready(function() {
$('body').after('<div id="h1elm">'+$('#topbar h1 span').html()+'</div>');
var h1 = $('#h1elm').html();
$('#topbar h1 span').show();
showPromo2(1, h1);


/*if(vurl.indexOf('.html') == -1 || vurl.indexOf('/concurs') != -1) {
var so = new SWFObject("images/concurs.swf", "mymovie", "611", "144", "8", "#ffffff");
so.write("centertop");
}*/


$('#scrisoare').click(function() {
var vattr = $(this).attr('checked');

if(vattr == true) {
$('#scrisoare_text').show();
} else {
$('#scrisoare_text').hide();
}
});


var height = new Array(), mh = 0;

$('.vouchere dl dd p').each(function(){
mh = $(this).height() > mh? $(this).height() : mh;
});

$('.vouchere dl dd p').css('height', mh);

$('textarea[@name=v_mesaj]').charCounter(300, { container: '#v_mesaj_counter' });

get_comanda();

$('.logon').click(function() {
alert('Pentru a accesa aceasta sectiune trebuie sa va autentificati!');
return false;
});

$('#nota').change(function() {
var vnota = $(this).val();
if(vnota == '') {
alert('Va rugam alegeti o nota.');
} else {
var vurl = window.top.location.toString().split('/');
window.location.href = $('base').attr('href')+'produs/'+vurl[4]+'/'+vurl[5].split('.html')[0]+'/trimite_nota/'+vnota+'.html';
}
});

$('.comentariu textarea').blur(function(){verifica1(this)});

function verifica1(t) {
var vmesaj = $(t).val();

if (vmesaj.replace(/^\s+|\s$/g, '').length < 10) {
$('.errcomentariu').remove();
$('.comentariu form').prepend('<p class="errcomentariu">- Comentariul trebuie sa contina cel putin 10 caractere.</p>');
$(t).css({color: "#000", background: "#F0CED3", border: "1px solid #91020C"});
$(t).focus(function() { $(t).css({color:"#000", background: "#F5E0E4"}); });
return false;
} else {
$('.errcomentariu').remove();
$(t).css({color: "#000", background: "#E8FFE8", border: "1px solid #A4FFA4"});
$(t).focus(function() { $(t).css({color:"#000", background: "#F1FAF5"}); });
return true;
}
};


$('.comentariu form').submit(function() {
return (!verifica1('.comentariu textarea') ? false : true);
});

$('.announce').corner("10px");

if($('a.hasPics').length == 1) $('a.hasPics').lightBox();
else $('a.hasPics').lightBox({fixedNavigation:true});

$('#centertop').corner("10px");
$('#pages').transparentCorners( { cornerSize: 5 } );

if(!$.browser.msie)$('#pages a').transparentCorners( { cornerSize: 3 } );
$('div.box').corner("10px");
$('#centerright').corner("10px");

//if($('.vouchere').html() == null) $('#centerleft').corner("10px");

if(vurl.indexOf('/concurs') == -1) {
if($('.vouchere').html() == null) $('#centerleft').corner("10px");
}

$('.vouchereReg').corner("10px");

//$('#right').corner("10px");

$('#cartea_sapt').corner("top 10px");
$('.optimizat').prev().corner("bottom 10px");

$('#contact').corner("10px");
$('div.item').corner("10px");
$('#totalP').corner("10px");

var catH = $(".item").height();
if (catH/2 != Math.floor(catH/2)) {$(".item").css({paddingBottom:1})};
var catH2 = $("#centerright").width();
if (catH2/2 != Math.floor(catH2/2)) {$("#centerright").css({paddingRight:2})};
var catH3 = $("#centerright").height();
if (catH3/2 != Math.floor(catH3/2)) {$("#centerright").css({paddingBottom:2})};
var catH4 = $("#right").height();
if (catH4/2 != Math.floor(catH4/2)) {$("#right").css({paddingBottom:1})};
var catH5 = $("#centerleft").height();
if (catH5/2 != Math.floor(catH5/2)) {$("#centerleft").css({paddingBottom:1})};

$('.confirm').click(function(){
if(confirm("Sunteti sigur?")) return true;
else return false;
});

$('#newsletter form').submit(function(){
var nlEmail = $('input[name=nlEmail]').val();
if(jQuery.trim(nlEmail) == '') {
alert("Introduceti o adresa de email!");
return false;
} else if(!isEmail(nlEmail)) {
alert("Introduceti o adresa de email valida!");
return false;
}
});

$('input.submit').hover(
function(){
$(this).addClass('hover');
},
function(){
$(this).removeClass('hover');
}
);

$(".btn").hover(
function () {
$(this).addClass('hover');
},
function () {
$(this).removeClass('hover');
}
);

$('#cautabtn').click(function(){
$('#cauta form').submit();
return false;
});

$('#cautadoar').change(function(){
$('#cauta form').attr('action', 'cautare/'+$('#cautadoar').val()+'.html');
});

$('a.bonus').click(function(){
return false;
});

$('#newsletter input.text').focus(function(){
if($('#newsletter input.text').val() == "adresa de email") {
$('#newsletter input.text').attr("value", "")
$('#newsletter input.text').css("color", "black");
}
});

$('#newsletter input.text').blur(function(){
if(jQuery.trim($('#newsletter input.text').val()) == "") {
$('#newsletter input.text').attr("value", "adresa de email")
$('#newsletter input.text').css("color", "grey");
}
});

setTimeout('fitSuc()', 3000);
setTimeout('fitErr()', 3000);

//Left sLideshow:

var height = new Array(), mh = 0;

$('#populare ul li').each(function(){
mh = $(this).height() > mh? $(this).height() : mh;
});

$('#populare ul').css('height', mh);

$('a.prev').click(function(){
return false;
});

$('a.prev').hover(
function(){
showProd(current_prodP-1);
clearTimeout(waitP);
},
function(){
waitP = setTimeout('nextProd()', 3000);
}
);

$('a.next').click(function(){
return false;
});

$('a.next').hover(
function(){
nextProd();
clearTimeout(waitP);
},
function(){
waitP = setTimeout('nextProd()', 3000);
}
);

$('#populare ul li').hover(
function(){
clearTimeout(waitP);
},
function(){
waitP = setTimeout('nextProd()', 3000);
}
);


});


/* start setari implicite */
waitP = null;
totalP = 1;
current_prodP = 1;
/* end setari implicite */

function showProd(prod) {
if(prod < 1) showProd(totalP);
else {

clearTimeout(waitP);
current_prodP = prod;

//Reset
$('#populare ul li').css('display', 'none');

//Current
$('#populare ul li:eq('+parseInt(current_prodP-1)+')').fadeIn('slow');


//$('#populare ul li:eq('+parseInt(current_prodP-1)+')').css('display', 'block');

waitP = setTimeout('nextProd()', 3000);
}
}

function nextProd() {
 if(current_prodP < totalP) {
$('#populare ul li:eq('+parseInt(current_prodP-1)+')').fadeOut('slow', function(){
showProd(parseInt(current_prodP)+1);
});
 }
 else showProd(1);
}
