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 );	

$(document).ready(init);

var cat_menu_arrow0 = new Image();
cat_menu_arrow0.src = "/images/site/cat_menu.grey.marker.gif";
var cat_menu_arrow1 = new Image();
cat_menu_arrow1.src = "/images/site/cat_menu.white.marker.gif";


function init() {	
	
	/*$(".stylize").stylize();*/
	
	$("#cat_menu div.level0").hover (
        function () {            
            if ( $(this).attr("rel") > 0 ) {                
                var pid = $(this).attr("rel");
                $(this).addClass("selected");
                if ( ajaxRes[pid] ) {	
        			$("#pid-"+pid).show();
        		}
        		else {                    
                    $("a", this).addClass("loading");                                        
        			$("a", this).after($('<div id="pid-'+pid+'" class="submenu"><a >Загрузка...</a></div>'));
                    //$("#pid-"+pid).hide();
        			$("#pid-"+pid).load("/ajax/catalogue-menu.php", { 'pid': pid }, onSubmenuLoaded);
        		}        		
            }
        },
        function () {
            if ( $(this).attr("rel") > 0 ) {
                $(this).removeClass("selected");
                var pid = $(this).attr("rel");
                $("#pid-"+pid).hide();
                //alert(pid);
            }
        }        
    );	
    
    /*$("cat_menu .submenu").hover (
        function () { $(this).show() },
        function () { $(this).hide() }
    );*/
	
	$("a.2cart").click(product2cart);

    $("a.view-product-by-art").click(
        function () {                        
            var win = window.open (
                $(this).attr("href"), 
                '',
                "scrollbars=1, resizable=1, titlebar=1,height=520, width=700, left="+(screen.width-750)+", top=20");
            
            return false;
        }
    );
	
	updateCart();	
	
	
}

var ajaxRes = new Array();

function onSubmenuLoaded(data) {	
	ajaxRes[$(this).attr("id").replace('pid-','')] = data;
    //$(this).show();
    $("#cat_menu .level0[rel="+$(this).attr("id").replace('pid-','')+"] a").removeClass("loading");
}

function product2cart( eventObject ) {
	var prod_id = $(this).attr("rel");	
	var count = $("#count_"+prod_id).attr("value");
	if ( !count )
		count = 1;
	
	
	var win = window.open('/cart/add/'+prod_id+'/'+count+'/',
		'add2cart',
		"scrollbars=1, resizable=1, titlebar=1, height=520, width=700, left="+(screen.width/2-750/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 == '' && document.forms[name].elements[i].className != 'nrq' ) {
				alert('Не все поля заполнены');
				
				document.forms[name].elements[i].focus();
				return;
			}
		
			i++;
		}
		document.forms[name].submit();		
	}
	catch ( ex ) {
		alert(ex);
	}
}

function clearForm( name ) {
	try {
		var n = document.forms[name].length;		
		
		var i = 0;
		while ( i < n ) {		
			document.forms[name].elements[i].value = '';				
			i++;
		}		
	}
	catch ( ex ) {
		alert(ex);
	}
}

function getFlashObj(movie){
	if (window.document[movie]) {
		return window.document[movie];
	}
	if (navigator.appName.indexOf("Microsoft Internet")==-1) {
		if (document.embeds && document.embeds[movie]) {
			return document.embeds[movie]; 
		}
	} 
	else {
		return document.getElementById(movie);
	}
}

function setDimensions(movie) {										
   var movieObj = getFlashObj(movie);							   
   var width = movieObj.TGetProperty("/", 8);
   var height= movieObj.TGetProperty("/", 9);
   movieObj.width = width;
   movieObj.height = height;
}