$(document).ready(function(){
	$.ajaxSetup({
        scriptCharset: "utf-8" 
    });
	
	// set correct width for categories blocks on main page
	setPCategItemsWidth();
	$(window).bind('resize', function(){setPCategItemsWidth();});
	
	showCartSum();
	
	// cbox
	$("a[rel='example1']").colorbox();
	
	// treeview

	persist = /products|catalog/i.test(location.pathname) ? 'cookie' : 'location';

	$("#navigation").treeview({
		collapsed: true,
		unique: true,
		persist: persist,
		cookieId: "treeview-black"
	});

		
	$('input[name=what]').watermark('Маршрутизатор Cisco 3945 ...',{className: 'jwatermark'});

	$('.Queston').colorbox();
	
// Вслытие хинтов в названии категорий

	$('.item_wrap').mouseover(function(){
		$(this).find('.item_descr').stop().animate({bottom:0},150);
	}).mouseout(function(){
		$(this).find('.item_descr').stop().animate({bottom:'-89px'},150);
	});

	if($('nindex a:last').attr('href'))
	{
		var request_uri = $('nindex a:last').attr('href').replace('products','catalog');

		$('#categoriesMenu a[href]').each(function(i,item){
			href = $(item).attr( 'href' );
			if(href == request_uri)
				$(item).addClass('current_url') ;
		});
	}
});

function setPCategItemsWidth(){
	var itemWidth=260;
	var clearWidth= document.body.clientWidth-640;
	var inLineCount=Math.floor(clearWidth/itemWidth);
	var newItemWidth=Math.floor(clearWidth/inLineCount);
	if(newItemWidth<itemWidth){newItemWidth=clearWidth;}
	
	if(location.pathname == '/')
		$("#productsCategories .items").css('width', newItemWidth+'px');
}

function checkContactForm(){
	var formObj=$("form[name='contactForm']");
	formObj.ajaxSubmit(function(data){
		if(Math.round(data)==data){
			formObj.clearForm();
			$(".forms").html('<b>Сообщение оправлено</b>');
		}else{
			alert(data);
		}
	});
}

function faqFormSubmit(){
	var formname='faqForm';
	var obj=$("form[name='"+formname+"']");
	var options={
			url: '/ajax/check_faq_form.php',
			success:faqFormSendResult
		}
	obj.ajaxSubmit(options);
}

function faqFormSendResult(response){
	var button=$("form[name='faqForm'] input[type='button']");
	if(response){
		// if order sended
		if(Math.round(response)==response){
			alert('Ваш вопрос принят и будет обработан в ближайшее время');
			document.location.href='/faq/';
		}else{
			alert(response);
		}
	}
	changeButtState(button, true);
}

function contactFormSubmit(){
	var formname='contactForm';
	var obj=$("form[name='"+formname+"']");
	var options={
			url: '/ajax/check_contact_form.php',
			success:contactFormSendResult
		}
	obj.ajaxSubmit(options);
}

function contactFormSendResult(response){
	var button=$("form[name='contactForm'] input[type='button']");
	if(response){
		// if order sended
		if(Math.round(response)==response){
			alert('Спасибо, Ваш запрос отправлен. Наши менеджеры свяжутся с Вами в ближайшее время');
			document.location.href='/';
		}else{
			alert(response);
		}
	}else{
		alert('No response');
	}
	changeButtState(button, true);
}

function switchCurrency(loc, curr){
	var t = false;
	var a = '?';
	if(/\?/i.test(loc))
		a = '&';
		
	if(t = /currency=([1,2])/i.test(loc)){
		loc = loc.replace(/currency=[1,2]/, 'currency='+curr);
	}else{
		loc += a+'currency='+curr;
	}
	document.location.href=loc;
}

