var path = document.location.pathname;
if ( path.charAt(path.length-1) != '/' ) path = path+'/';

var epos = document.location.pathname.indexOf('/',1) == -1 ? document.location.pathname.length : document.location.pathname.indexOf('/',1)-1;
var section = document.location.pathname.substr(1, epos );	

//var leftMenuItemsState = new Object();

$(document).ready(init);

function init() {
	$("#menu a").hover (
		function () {
			var img_src = $("img", $(this)).attr("src");
			$("img", $(this)).attr({src: img_src.replace("default", "hover") });
		},
		function () {									
			var img_src = $("img", $(this)).attr("src");
			$("img", $(this)).attr({src: img_src.replace("hover", "default") });
		}
	);
	
	$("#catalogue-menu a").each(
		function() {
			var href = $(this).attr("href").replace('http://'+document.domain,'');							
			if ( href.charAt(href.length-1) != '/' ) href = href+'/';
			
			if ( path.indexOf(href) == 0 )
				$(this).parent().addClass("selected");
				
			if ( $(this).attr("rel") > 0 ) {
				$(this).bind("click", onCategoryClick );														
				
				if ( document.location.pathname == '/' )					
					return;				
				
				if ( path.indexOf(href) == 0 )					
					$(this).click();
			}
		}
	);
	
	$("#product a.2cart").click(product2cart);
	$("#product .preview2 a").click(onPreview2Click);
	$("#product .preview2 a").dblclick(onPreview2DblClick);
	
	$("#product select").change (
		function () {
			var selected = $("option:selected", $(this));
			
			var price_id = selected.attr('value');
			
			$("#product .price").attr('rel',price_id);			
			
			$("#product .price-item").hide();
			$("#price-"+price_id).show();
		}
	).change();
	
	$("#section-title").attr({src:"/images/titles/"+section+".gif"});	

	updateCart();
	correctImages();
}

var ajaxRes = new Array();
var catStatus = new Array();

function onCategoryClick( e ) {
	var pid = $(this).attr("rel");
	
	if ( catStatus[pid] ) {
		$("#pid-"+pid).css("display","none");
		$(this).parent().removeClass('selected');
	}
	else {	
		if ( ajaxRes[pid] ) {	
			$("#pid-"+pid).css("display","block");
		}
		else {
			$(this).parent().after($('<div id="pid-'+pid+'">Загрузка...</div>'));
			$("#pid-"+pid).load("/ajax/catalogue-menu.php", { 'pid': pid }, onAjaxSuccess);
		}
		$(this).parent().addClass('selected');
	}
	catStatus[pid] = !catStatus[pid];

	return false;
}

function onAjaxSuccess(data) {	
	ajaxRes[$(this).attr("id").replace('pid-','') ] = data;
	//$("#"+$(this).attr("id")+" a[@rel]").bind("click", onCategoryClick );
	
	$("#"+$(this).attr("id")+" a").each(
		function () {
			var href = $(this).attr("href").replace('http://'+document.domain,'');
			
			//alert(path);
			
			if ( $(this).attr("rel") > 0 ) {				
				$(this).bind("click", onCategoryClick );
				if ( path.indexOf(href) == 0 )
					$(this).click();
			}
			else if ( path.indexOf(href) == 0 )
				$(this).parent().addClass("selected");
		}
	);
}

function product2cart( eventObject ) {
	
	var prod_id = $(this).parent().attr("rel");
	var price_id = $("#product .price").attr("rel");
	
	/*alert(prod_id+' '+price_id);
	return false;*/
	
	var win = window.open('/cart/add/'+prod_id+'/'+price_id+'/',
		'add2cart',
		"scrollbars=1, resizable=1, titlebar=1, height=520, width=700, left="+(screen.width/2-700/2)+", top=20");
	
	return false;
}

function popupWindow( url, w, h ) {
	var win = window.open(
		url,
		'PopupWindow',
		"scrollbars=1, resizable=1, titlebar=1, height="+h+", width="+w+", left="+(screen.width/2-w/2)+", top=20");	
}

function updateCart() {
	$.get("/ajax/cart.php", {}, onUpdateCart);
	setTimeout('updateCart()',5000);
}

function onUpdateCart( data ) {
	$("#all_count").text($("cart",data).attr('all_count'));
	$("#all_cost").text($("cart",data).attr('all_cost'));	
}

function onPreview2Click( eventObject ) {
	
	if ( !( /preview/.test($(this).attr('href')) ) )
		$(this).attr('href', $(this).attr('href').replace('catalogue/','catalogue/preview_'));

	$("#main_photo a").attr('src',$(this).attr('href').replace('preview_',''));
	$("#main_photo img").attr('src',$(this).attr('href'));
	
	return false;
}

function onPreview2DblClick( eventObject ) {
	$(this).attr('href', $(this).attr('href').replace('preview_',''));		
	$(this).lightbox({start:true,events:false}); 
	return false;	
}

function sendForm( name ) {
	try {
		var n = document.forms[name].length;		
		
		var i = 0;
		while ( i < n ) {		
			if ( document.forms[name].elements[i].value == '' ) {
				alert('Не все поля заполнены');
				document.forms[name].elements[i].focus();
				return;
			}
		
			i++;
		}
		document.forms[name].submit();		
	}
	catch ( ex ) {
		alert(ex);
	}
}

function correctImages() {
	//return;
	if ( !(/MSIE/.test(navigator.userAgent)) )
		return;
	
	$("div.img").each (
		function () {
			var w = $("img[src^='/files']", this).attr("width");
			var h = $("img[src^='/files']", this).attr("height");
			
			if ( w % 2 != 0 ) {
				$("b[class='trc']", this).css("right",-1);
				$("b[class='brc']", this).css("right",-1);
			}
			if ( h % 2 != 0 ) {
				$("b[class='blc']", this).css("bottom",-1);
				$("b[class='brc']", this).css("bottom",-1);
			}
			/*$("img[src^='/files']", this).attr("width",w);
			$("img[src^='/files']", this).attr("height",h);*/
			/*$(this).attr('width',w-1);
			$(this).attr('height',h-1);
			$(this).css({'width':w-1, 'height':h-1});*/
			
			/*alert(h);
			alert($(this).attr('height'));*/
		}
	);
}