
var loadScrollTop = true;
var tmp = false;

function var_dump(obj) {
   if(typeof obj == "object") {
      return "Type: "+typeof(obj)+((obj.constructor) ? "\nConstructor: "+obj.constructor : "")+"\nValue: " + obj;
   } else {
      return "Type: "+typeof(obj)+"\nValue: "+obj;
   }
}

function reloadLoginBox() {
	$('#loginBox').load('?p=loginBox&m=1',{},function(){
		loginForm();
		quitBtn();
	});
}

function reload() {

	hash = document.location.hash;
	hash = hash.replace('#','');
	getContent('?p='+hash);

}

function openTrBox() {
	$('.regHideBox').toggle();
	if ($('#sz_open').val() == '1') {
		$('#sz_open').val('0');
	} else {
		$('#sz_open').val('1');
	}
}

function productShowBImage(ID) {
	$('#bImageLink').attr('href','image.php?id='+ID+'&frame=1');
	$('#bImageImg').attr('src','image.php?id='+ID+'&w=300&c=0');
}

function saveCartWindow() {

	var toParam = '';
	$('.cartUnitInput').each(function(){
		var name = $(this).attr('name');
		var value = $(this).attr('value');
		toParam += name+':'+value+',';
	});
	toParam = toParam.substr(0,toParam.length-1);
	getContent('?p=cart&a='+toParam);
	
}
function openCartWindow() {

	var win = new wb_window(400,400);
	win.load('cartWindow');
	
}

function getContent(id) {
	$.historyLoad(prepareHref(id));		
}

$(document).ready(function(){

	checkHrefContainAttributes();
	$.historyInit(pageload);
	bindClickEvents($("a[@rel='a']"));
	attachForms();
	loginForm();
	quitBtn();
	initSlider();
	
});

function prepareHref(href) {
	href = href.replace('?p=','');
	href = href.replace(host,'');
	return href;
}

function checkHrefContainAttributes() {
	href = document.location.href;	
	href = href.replace(host,'');
	if (href.indexOf('?') != -1) {
		var tmp = href.split('#');
		href = tmp[0];
		href = href.replace('?p=','');
		href = href.replace('index.php','');		
		document.location.href = host+(href ? '#'+href : '');
	}
}

function pageload(hash) {
	setLoaderPos();
	$("#loader").fadeIn('fast',function() {
		$("#centerContent").load('?m=1&p='+hash,function() {			
			$("#loader").fadeOut('fast',function() {
				bindClickEvents($("#centerContent a[@rel='a']"));
				attachForms();
				initSlider();
			});
		});
	});
}

function bindClickEvents(obj) {
	obj.click(function(){
		if (loadScrollTop == true) window.scrollTo(0,0);
		$.historyLoad(prepareHref(this.href));
		return false;
	});
}

function loginForm() {
	bindClickEvents($("#loginBox a[@rel='a']"));
	$('#loginForm').submit(function(){		
		$(this).ajaxSubmit({
			target:'#loginBox',
			url:'?p=loginBox&m=1',
			success:function() {
				loginForm();				
				quitBtn();
			}
		});
		return false;
	});
}

function quitBtn() {
	$('#quitBtn').click(function(){
		$('#loginBox').load('?m=1&p=loginBox&quit=1',function() {						
			loginForm();
		});
		return false;
	});
}

function attachForms() {
	$('.ajaxForm').submit(function(){
		if (loadScrollTop == true) window.scrollTo(0,0);
		var url = $(this).attr('action')+'&m=1';		
		$(this).ajaxSubmit({
			target:'#centerContent',
			url:url,
			success:function() {
				bindClickEvents($("#centerContent a[@rel='a']"));
				attachForms();
			}
		});
		return false;
	});
	
	$('.toCartInput').mousedown(function(){
		if (!$(this).val()) {
			$(this).val(1);
		}
	});
	
	$('.toCartInput').dblclick(function(){
		if (!$(this).val()) {
			$(this).val(1);
		} else {
			var v = 0;
			v = $(this).val();
			v++;
			$(this).val(v);
		}
	});
	
	$('#searchForm').submit(function (){
		if (tmp == false) {
			tmp = true;
			if (loadScrollTop == true) window.scrollTo(0,0);
			var s = $('#searchInput').val();
			s = s.replace(' ','+');
			$.historyLoad('products&s='+s);
		}
		return false;
	});
}

function putToCart(id) {
	var amount = $('#amount_'+id).val();
	$.ajax({
		type: "GET",
		url: "services.php?p=insertToCart&pid="+id+"&a="+amount,
		success: function(msg){
			if (msg == '1') {
				$('#main').append('<div style="display:none;" "id="toCartCntr">'+lang.toCartMsg+'</div>');
				$('#toCartCntr').fadeIn("fast", function(){								
					setTimeout('removeCartMsg()',1000);
				});
			} else {
				alert(lang.error1);
			}
		}
	});
}

function removeCartMsg() {
	$('#toCartCntr').fadeOut("slow", function(){
		$(this).remove();
	});
}

function checkUserName() {
	if ($('#userCheckMsg').get() == '') {
		$('#userNameInputTd').append('<br><div id="userCheckMsg"></div>');
	}
	var u = $('#userName').val();
	if (u.length<5) {
		$('#userCheckMsg').html('');
	} else {
		$.getJSON(host+"services.php?p=checkUserName&u="+$('#userName').val(),function(data){		
			if (data.u) {
				$('#userCheckMsg').html(lang.error2);
			} else {
				$('#userCheckMsg').html(lang.error3);
			}
		});
	}
}

function scrollToEl(elementId) {

	var theElement = document.getElementById(elementId);
	var selectedPosY = 0;
	while(theElement != null){
		selectedPosY += theElement.offsetTop;
		theElement = theElement.offsetParent;
	}           		      
	window.scrollTo(0,selectedPosY);
}

function setLoaderPos() {
	$('#loader').css('left',($(window).width()/2)-($('#loader').width()/2));
	$('#loader').css('top',($(window).height()/2)-($('#loader').height()/2));	
}

function checkSearchInput(obj) {
	if (obj.value == lang.search) {
		obj.value = '';
	}
}

