window.siteurl='http://www.bricotop.ro/';
var BASEHREF='http://www.bricotop.ro/';
$(document).ready(function(){
	$(".scrollable")
	.scrollable({
		globalNav: true,
		size: 1,
		items: '.items',
		next: '#next',
		prev: '#prev',
		clickable: false
	})
	.autoscroll({
		autoplay: true,
		autopause: true,
		interval: 6000
	})
	.navigator({
		navi:'.navi',
		naviItem:'button',
		activeClass:'current',
		indexed:true
	});
	$(".bestContent")
	.scrollable({
		globalNav: false,
		items: '.itemz',
		circular: true,
		size:3
	})
	.autoscroll({
		autoplay: true,
		autopause: true,
		interval: 2396
	});
	if ($('#product-gallery .items-gallery').length>1) {
		$("#product-gallery")
		.scrollable({
			globalNav: false,
			items: '.items-gallery',
			circular:true,
			next: '#next',
			prev: '#prev'
		})
		.autoscroll({
			autoplay: true,
			autopause: true,
			interval: 2396
		});
	}
	$(".datepicker").datepicker({
		dateFormat :'mm-dd-yy',
		changeYear:true,
		yearRange:'1930:1995',
		dayNames:['Duminica','Luni','Marti','Miercuri','Joi','Vineri','Sambata'],
		dayNamesMin: ['Du','Lu','Ma','Mi','Jo','Vi','Sa'],
		monthNames: ['Ian','Feb','Mar','Apr','Mai','Iun','Iul','Aug','Sep','Oct','Noi','Dec'],
		monthNamesShort: ['Ian','Feb','Mar','Apr','Mai','Iun','Iul','Aug','Sep','Oct','Noi','Dec'],
		firstDay:1,
		minDate:new Date(1930,1-1,1),
		defaultDate:new Date(1980,1-1,1),
		nextText:'urmatoarea luna',
		prevText:'precedenta luna'
	});
	$("a.group").fancybox({
		'opacity':true,
		'cyclic':true,
		'centerOnScroll':true,
		'overlayColor':'#333',
		'overlayOpacity':0.6,
		'titlePosition':'over',
		'speedIn':600,
		'speedOut':200,
		'transitionIn':'elastic',
		'easingIn':'swing',
		'transitionOut':'elastic',
		'easingOut':'swing',
		'cyclic':true
	});
	$('#tabs ul a').click(function(){
		var e=$(this);
		if (e.attr('href')) {
			var id=e.attr('href').split('#')[1];
			$('#tabs>ul>li:not(.c)').each(function(){
				$(this).removeAttr('class');
			});
			e.parent().addClass('active');
			$('#tabs>div').each(function(){
				var el=$(this);
				el.removeAttr('class');
				if (el.attr('id')==id) { el.addClass('active'); }
			});
			return false;
		}
	});
	$("a[rel='external'],#footerLinks a").click(function(){
		window.open($(this).attr('href'));
		return false;
	});
	$('#s').keyup( function(e) {
	$("#select_search").load(BASEHREF+'/content/ajax/product_search.php?name='+this.value);
	$("#select_search").show();
	return this;
	});

	$.get(BASEHREF+"includes/token.php",function(txt){$("#validation").val(''+txt+'');});
	$('form input,form textarea,form select').live('focus',function(){
		var id=$(this).parent().parent().parent().parent().attr('id');
		if (id.length==0) var id=$(this).parent().parent().parent().parent().parent().parent().attr('id');
		var thiseq=$('dd').index($(this).parent());
		$('#'+id+' input,#'+id+' textarea,#'+id+' select').removeClass('focused');
		$('#'+id+' dt').removeAttr('class');
		if($(this).attr('type')!='checkbox') $(this).addClass('focused');
		$('dt:eq('+thiseq+')').addClass('focused');
	});
	$('.clearfield').each(function() {
		var el=$(this);
		if(el.attr('rel')==undefined) { el.attr('rel', el.val()).addClass('cf-blur'); }
		el.focus(function() { if(el.val()==el.attr('rel')) { el.val('').removeClass('cf-blur').addClass('cf-blur'); } });
		el.blur(function() { if(el.val()=='') { el.val(el.attr('rel')).removeClass('cf-blur').addClass('cf-blur'); } });
	});
	$("form").validationEngine({
		inlineValidation:true,
		promptPosition:"centerRight"
	});
	$("input").attr('autocomplete', 'off');
	$("body").append("<div id=\"window_alerta\"></div><div id=\"AjaxLoading\"></div>");
});
if (window.location=='http://bricotop.ro/') {
	window.location=window.siteurl;
}
function eurl(v) {
	return encodeURIComponent(v);
}
function refreshShopcart() {
	var trim2='';
	trim2+='oper=ajax';
	$('#shopcart-count').load(window.siteurl+'content/ajax/shopcart_box.php?refresh=header');
	EjaxCb(window.siteurl+'content/ajax/shopcart_box.php',trim2,'',function (r) {
		$html('shopcartBoxContent',r);
	});
}
var order_trim_add='';
var order_trim_id_method=0;
function shopcart_order(type,val,val2) {
	order_trim_add='&from_order=1&id_method='+order_trim_id_method;
	shopcart(type,val,val2);
}

function select_shipping_method(id_method) {
	order_trim_id_method=id_method;
	$("#order_total").load(BASEHREF+"content/ajax/order_total.php?id_method="+id_method);
}
function shopcart(type,val,val2) {
	if (type=='add') {
		var trim='';
		trim+='type='+type;
		trim+='&id='+val;
		trim+='&quant='+val2;
		Ejax(window.siteurl+'content/ajax/shopcart.php',trim+order_trim_add,'shopcartContent');
	}
	if (type=='del') {
		if ($val('quantity_'+val)==1) {
			if (confirm('Esti sigur ca vrei sa stergi produsul?')) {
				var trim='';
				trim+='type='+type;
				trim+='&id='+val;
				trim+='&quant='+val2;
				Ejax(window.siteurl+'content/ajax/shopcart.php',trim+order_trim_add,'shopcartContent');
			}
		} else {
			var trim='';
			trim+='type='+type;
			trim+='&id='+val;
			trim+='&quant='+val2;
			Ejax(window.siteurl+'content/ajax/shopcart.php',trim+order_trim_add,'shopcartContent');
		}
	}
}
function Ejax(phpfile,params,divelem){
	EjaxCb(phpfile,params,divelem);
}
function alerta(mesaj, error_var, width) {
	if (error_var == null) error_var = false;
	if (width == null) width = 642;
	options={
	'modal' :true,
	'width' :width,
	'minWidth' :642,
	'title' : 'Mesaj atentionare!'
	};
	var mesaj_new="";
	if (error_var) {
		mesaj_new="<div class=\"ui-widget\"><div class=\"ui-state-error ui-corner-all\" style=\"padding:20px; margin:12px;\"><span class=\"ui-icon ui-icon-alert\" style=\"float: left; margin-right: 0.3em;\" ></span>" + mesaj + "</div></div>";
	} else {
		mesaj_new="<div class=\"ui-widget\"><div class=\"ui-state-highlight ui-corner-all\" style=\"padding:20px;  margin:12px;\"><span class=\"ui-icon ui-icon-info\" style=\"float: left; margin-right: 0.3em;\" ></span>" + mesaj + "</div></div>";
	}
	do_win(mesaj_new,options);
}
function do_win(mesaj,options,callback) {
	if ($("#window_alerta").dialog('isOpen')!=undefined) $("#window_alerta").dialog('destroy');
	$("#window_alerta").html(mesaj);
	$("#window_alerta").dialog(options);
	if(callback!=null) callback();
}
function confirma(title,mesaj,button,callback) {
	options={
	'modal' :true,
	'width' :240,
	'buttons': { " Renunta ": function() { $(this).dialog("close"); } ,  " Confirma " : function() { callback(); $(this).dialog("close"); }   },
	'title' : title
	};
	do_win(mesaj,options);
}
function error(mesaj) {
	alerta(mesaj, true);
}
function $val(a){
	try{
		return document.getElementById(a).value;
	}
	catch(e){
		alert('Elementul '+a+' nu poate fi gasit reporniti browserul si incercati din nou');
		return false;
	}
}

function $v(a,v){
	try{
		document.getElementById(a).value=v;
		return true;
	}
	catch(e){
		alert('Elementul '+a+' nu poate fi gasit reporniti browserul si incercati din nou');
		return false;
	}
}
function $html(a,v){
	try{
		document.getElementById(a).innerHTML=v;
		return true;
	}
	catch(e){
		alert('Elementul '+a+' nu poate fi gasit reporniti browserul si incercati din nou');
		return false;
	}
}
function $id(a){
	var rez;
	try{
		rez=document.getElementById(a);
		return rez;
	}
	catch(e){
		alert('Elementul '+a+' nu poate fi gasit reporniti browserul si incercati din nou');
		return false;
	}
}
function creazaXmlHttp(){
	var xmlHttp;
	if(window.ActiveXObject){
		try{
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch (e){
			xmlHttp = false;
		}
	}
	else{
		try{
			xmlHttp = new XMLHttpRequest();
		}
		catch (e){
			xmlHttp = false;
		}
	}
	if (!xmlHttp) alert("Nu s-a putut crea obiectul XMLHttpRequest .");
	else return xmlHttp;
}
var xmainpath='images/';
var xmlHttp=creazaXmlHttp();
function EjaxCb(phpfile,params,divelem,callback){
	if(divelem!=''){
		var xdiv=$id(divelem);
		var dispwait=true;
	}
	else {
		var xdiv='';
		var dispwait=false;
	}
	//var dispwait=false; // sa nu mai faca loading
	this.phppath=phpfile;
	var xphp=this.phppath;
	if(params!=''){
		var xparam='?'+params+"&sid="+Math.random();
	}
	else{
		var xparam="?sid="+Math.random();
	}
	var xmlHttp=creazaXmlHttp();
	this.cerere=function(){
		if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0){
			xmlHttp.open("GET", xphp+xparam, true);
			xmlHttp.onreadystatechange = xrez;
			xmlHttp.send(null);
			if(dispwait){
				//xdiv.innerHTML= '<img src="'+xmainpath+'wait.gif'+'" width=25 height=25>'+'PLEASE WAIT!!!';
				document.getElementById('AjaxLoading').style.display='block';
			}
		}
		else{
			if(dispwait){
				//xdiv.innerHTML= '<img src="'+xmainpath+'wait.gif'+'" width=25 height=25>'+'PLEASE WAIT!!!';
				document.getElementById('AjaxLoading').style.display='block';
			}

		}
	};
	var xcer=this.cerere;
	this.rezultat=function(){
		if (xmlHttp.readyState == 4){
			if (xmlHttp.status == 200){
				var raspuns = xmlHttp.responseText;
				if (callback) {callback(raspuns);}
				else if(xdiv==''){
					alert(raspuns);
				}
				else{
					document.getElementById('AjaxLoading').style.display='none';
					xdiv.innerHTML =  raspuns;
				}
			}
			else{
				alert("Problema la accesare: " +xmlHttp.statusText);
			}
		}
		else{
			if(dispwait){
				//xdiv.innerHTML= '<img src="'+xmainpath+'wait.gif'+'" width=25 height=25>'+'PLEASE WAIT!!!';
				document.getElementById('AjaxLoading').style.display='block';
			}

		}
	};
	var xrez=this.rezultat;
	xcer();

}
function delete_uploaded(file,id_form) {
	if (confirm('Sunteti sigur ca doriti sa stergeti imaginea')) {
		$(id_form).parent().remove();
		$.post('ajax/base.php', 'delete_file=1&file=' + file);
	}
}

//vortex
function toggleCatLink(el) {
	var id = el.attr('href');
	if (($(id).hasClass('level-two')) || (el.hasClass('final-level') && el.parents('ul').hasClass('level-one'))) {
		$('ul.level-one li.active, ul.level-two, ul.level-two li.active, ul.level-three, ul.level-three li.active, ul.level-four, ul.level-four li.active, ul.level-five, ul.level-five li.active').removeClass('active');
	}
	if (($(id).hasClass('level-three')) || (el.hasClass('final-level') && el.parents('ul').hasClass('level-two'))) {
		$('ul.level-two li.active, ul.level-three, ul.level-three li.active, ul.level-four, ul.level-four li.active, ul.level-five, ul.level-five li.active').removeClass('active');
	}
	if (($(id).hasClass('level-four')) || (el.hasClass('final-level') && el.parents('ul').hasClass('level-three'))) {
		$('ul.level-three li.active, ul.level-four, ul.level-four li.active, ul.level-five, ul.level-five li.active').removeClass('active');
	}
	if (($(id).hasClass('level-five')) || el.hasClass('final-level') && el.parents('ul').hasClass('level-four')) {
		$('ul.level-four li.active, ul.level-five, ul.level-five li.active').removeClass('active');
	}
	if (el.hasClass('final-level') && el.parents('ul').hasClass('level-five')) {
		$('ul.level-five li.active').removeClass('active');
	}
	if ($(id).hasClass('level-five') || el.closest('ul').hasClass('level-five')) {
		$('#categories').animate({marginLeft:'-201px'}, 200);
	} else {
		$('#categories').animate({marginLeft:'0'}, 200);
	}
	$(id).addClass('active');
	el.parent('li').toggleClass('active');
	$('ul#category-notes li.active').removeClass('active');
	if (el.hasClass('final-level')) {
		var catNum = /\d+/.exec(el.attr('href'));
		$('#id_category').val(catNum);
	} else {
		$('#id_category').val('');
	}
}
function utf8_encode(string) {
	string = string.replace(/\r\n/g,"\n");
	var utftext = "";
	for (var n = 0; n < string.length; n++) {
		var c = string.charCodeAt(n);
		if (c < 128) {
			utftext += String.fromCharCode(c);
		} else if((c > 127) && (c < 2048)) {
			utftext += String.fromCharCode((c >> 6) | 192);
			utftext += String.fromCharCode((c & 63) | 128);
		} else {
			utftext += String.fromCharCode((c >> 12) | 224);
			utftext += String.fromCharCode(((c >> 6) & 63) | 128);
			utftext += String.fromCharCode((c & 63) | 128);
		}
	}
	return utftext;
}
function utf8_decode(utftext) {
	var string = "";
	var i = 0;
	var c = c1 = c2 = 0;
	while ( i < utftext.length ) {
		c = utftext.charCodeAt(i);
		if (c < 128) {
			string += String.fromCharCode(c);
			i++;
		} else if((c > 191) && (c < 224)) {
			c2 = utftext.charCodeAt(i+1);
			string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
			i += 2;
		} else {
			c2 = utftext.charCodeAt(i+1);
			c3 = utftext.charCodeAt(i+2);
			string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
			i += 3;
		}
	}
	return string;
}
function delete_file(file,el) {
	if (confirm('Sunteti sigur ca doriti sa stergeti imaginea')) {
		$(el).parent().remove();
		$.post(BASEHREF+'upload.php','delete_file=1&file='+file);
	}
}
//function account_pop(action,id) {
//	options={'modal':true,'width':982,'minWidth':982,'title':'Editeaza informatii personale'};
//	do_win('',options,function() {
//		var $frame=$('<iframe style="width:200px; height:100px;">');
//		$('#window_alerta').html($frame);
//		setTimeout( function() {
//			var doc=$frame[0].contentWindow.document;
//			var $body=$('body',doc);
//			$body.html('aaaaa');
//			$body.load('http://www.bricotop.ro/contul-meu/ #myAccount');
//		}, 1 );
//		$("#window_alerta").load(BASEHREF+"content/ajax/account.php?action="+action+"&user="+id);
//	});
//}
