 $(document).ready(function() {
            $('#cart_form').ajaxForm(function() {
                cart_reload();
            });
            
            $("a#card1").fancybox({
				'zoomOpacity'			: true,
				'overlayShow'			: false,
				'zoomSpeedIn'			: 500,
				'zoomSpeedOut'			: 500
			});
			$("a#card2").fancybox({
				'zoomOpacity'			: true,
				'overlayShow'			: false,
				'zoomSpeedIn'			: 500,
				'zoomSpeedOut'			: 500
			});
			$("a#card3").fancybox({
				'zoomOpacity'			: true,
				'overlayShow'			: false,
				'zoomSpeedIn'			: 500,
				'zoomSpeedOut'			: 500
			});
			$("a#card4").fancybox({
				'zoomOpacity'			: true,
				'overlayShow'			: false,
				'zoomSpeedIn'			: 500,
				'zoomSpeedOut'			: 500
			});
			$("a#card5").fancybox({
				'zoomOpacity'			: true,
				'overlayShow'			: false,
				'zoomSpeedIn'			: 500,
				'zoomSpeedOut'			: 500
			});
			$("a#card6").fancybox({
				'zoomOpacity'			: true,
				'overlayShow'			: false,
				'zoomSpeedIn'			: 500,
				'zoomSpeedOut'			: 500
			});
			
			$("a.page_image_a").fancybox({
				'zoomOpacity'			: true,
				'imageScale'			: true, 
				'overlayShow'			: true,
				'zoomSpeedIn'			: 500,
				'zoomSpeedOut'			: 500
			});
			
		$(".menu_new_title").click(function ()
		{
 			$("#menu_item_"+this.id).slideToggle(300); 
 			if(this.className == 'menu_new_title_active')
 			{
 				this.className = 'menu_new_title';
 				Delete_Cookie("menu_item_"+this.id);
 			}
 			else
 			{
 				this.className = 'menu_new_title_active';
 				Set_Cookie("menu_item_"+this.id, "1");
 			}
 			check_collapse_div();
		}
		);
		
		$(".menu_new_title_active").click(function ()
		{
 			$("#menu_item_"+this.id).slideToggle(300); 
 			if(this.className == 'menu_new_title_active')
 			{
 				this.className = 'menu_new_title';
 				Delete_Cookie("menu_item_"+this.id);
 			}
 			else
 			{
 				this.className = 'menu_new_title_active';
 				Set_Cookie("menu_item_"+this.id, "1");
 			}
 			check_collapse_div();
		}
		);
		
		$("#collapse_active a").click(function()
			{
				$(".menu_new_title").each(function()
				{
					$("#menu_item_"+this.id).slideToggle(300); 
					this.className = 'menu_new_title_active';
					Set_Cookie("menu_item_"+this.id, "1");
				}
				)
				check_collapse_div();
			}
		);
		
		$("#collapse a").click(function()
			{
				$(".menu_new_title_active").each(function()
					{
						$("#menu_item_"+this.id).slideToggle(300); 
						this.className = 'menu_new_title';
						Delete_Cookie("menu_item_"+this.id);
					}
				)
				check_collapse_div();
			}
		);
		check_collapse_div();
 });

function check_collapse_div()
{
	$("#collapse_active").show();
	$("#collapse").hide();
	$(".menu_new_title_active").each(
		function()
		{
			$("#collapse_active").hide();
			$("#collapse").show();
		}
	);
}

function delete_item(i)
{
	$.get(
		"/index.php?mod=cart&action=del_prod_from_cart_ajax&item_id="+i,
		[],
		cart_reload
		);
	
}

function return_item(i)
{
	$.get(
		"/index.php?mod=cart&action=return_prod_from_cart_ajax&item_id="+i,
		[],
		cart_reload
		);
}

function cart_reload()
{
	$.get(
		"/index.php?mod=cart&cart_ajax_load=1",
		[],
		dom_ready
		);
	reload_cart_data();
}

function dom_ready(data)
{
	$('#center').html(data);
	$('#cart_form').ajaxForm(function() {
                cart_reload();
            });
}

function reload_cart_data()
{
	$.getJSON('/index.php?mod=cart&action=load_cart_info_json', [], reload_cat_callback);
}

function reload_cat_callback(obj)
{
	$('#head_cart_count').html(obj['cartCounter']);
	$('#head_cart_sum').html(obj['sumprice']);
	if(parseInt(obj['cartCounter']) > 0)
	{
		$('#head_cart_img').attr('src', '/img/basketfull.jpg');
	}
	else
	{
		$('#head_cart_img').attr('src', '/img/basket.jpg');
	}
}

function add2cart(link, name)
{
	if(confirm('Вы действительно хотите добавить товар в корзину?'))
	{
		ShowWait.show_box();
		$.get(
			link,
			[],
			reload_cart_data_box
			);
	}
}

function reload_cart_data_box()
{
	reload_cart_data();
}

ClShowWait = function() {   
    this.get_win_width = function() {
        var myWidth = 0;
        if( typeof( window.innerWidth ) == 'number' ) //Non-IE
            myWidth = window.innerWidth;
        else if( document.documentElement && document.documentElement.clientWidth ) //IE 6+ in 'standards compliant mode'
            myWidth = document.documentElement.clientWidth;
        else if( document.body && document.body.clientWidth) //IE 4 compatible
            myWidth = document.body.clientWidth;
        return myWidth;
    }
	
    this.get_win_height = function() {
        var myHeight = 0;
        if( typeof( window.innerHeight ) == 'number' ) //Non-IE
            myHeight = window.innerHeight;
        else if( document.documentElement && document.documentElement.clientHeight ) //IE 6+ in 'standards compliant mode'
            myHeight = document.documentElement.clientHeight;
        else if( document.body && document.body.clientHeight) //IE 4 compatible
            myHeight = document.body.clientHeight;
        return myHeight;
    }

    this.get_scrol = function() {
        var yPos = 0;
        if (self.pageYOffset) {
            yPos = self.pageYOffset;
        } else if (document.documentElement && document.documentElement.scrollTop){
            yPos = document.documentElement.scrollTop;
        } else if (document.body) {
            yPos = document.body.scrollTop;
        }
        return yPos;
    }

    this.show_box = function() {
        var l = this.get_win_width()/2;
        var t = this.get_win_height()/2 - 50 + this.get_scrol();
        document.getElementById('addok').style.top  = t+'px';
        document.getElementById('addok').style.left = l+'px';
        document.getElementById('addok').style.display='block';	
        setTimeout( "ShowWait.hide_box()", 1000);		
    }
	
    this.hide_box = function() {
        document.getElementById('addok').style.display='none';		
    }
}	
var ShowWait =  new ClShowWait();

function Set_Cookie( name, value, expires, path, domain, secure )
{

var today = new Date();
today.setTime( today.getTime() );
if ( expires )
{
	expires = expires * 1000 * 60 * 60 * 24;
}
var expires_date = new Date( today.getTime() + (expires) );

document.cookie = name + "=" +escape( value ) +
( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
";path=/" +
( ( domain ) ? ";domain=" + domain : "" ) +
( ( secure ) ? ";secure" : "" );
}

function Get_Cookie( check_name ) {
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f

	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );


		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}

function Delete_Cookie( name, path, domain ) {
if ( Get_Cookie( name ) ) document.cookie = name + "=" +
";path=/" +
( ( domain ) ? ";domain=" + domain : "" ) +
";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

function delete_compare_item(item, from_prod)
{
	cookie_all = Get_Cookie('compare');
	items = cookie_all.split("|");
	for(i in items)
	{
		if(items[i] == item) items.splice(i, 1);
	}
	Set_Cookie('compare', items.join("|"));
	if(from_prod == 1)
	{
		$("#compare_unctive").show();
		$("#compare_active").hide();
	}
	else if(from_prod == 2)
	{
		$("#compare_deact_"+item).hide();
		$("#compare_act_"+item).show();
		$(".compare_prod_count").html(items.length);
	}
	else
	{
		create_compare_link();
	}
}

function add_compare_items(item, from_prod)
{
	cookie_all = Get_Cookie('compare');
	add = true;
	if(cookie_all != null && cookie_all != '')
	{
		items = cookie_all.split("|");
		for(i in items)
		{
			if(items[i] == item) add = false
		}
	}
	else
	{
		items = new Array();
	}
	if(add)
	{
		items[items.length] = item;
		Set_Cookie('compare', items.join("|"));
		if(from_prod == 1)
		{
			$("#compare_unctive").hide();
			$("#compare_active").show();
			$("#compare_prod_count").html(items.length);
		}
		else if (from_prod == 2)
		{
			$("#compare_deact_"+item).show();
			$("#compare_act_"+item).hide();
			$(".compare_prod_count").html(items.length);
		}
	}
	else if(from_prod == 2)
	{
		delete_compare_item(item, 2);
	}
}

function create_compare_link()
{
	cookie_all = Get_Cookie('compare');
	items = cookie_all.split("|");
	link = items.join("/");
	//alert(link);
	document.location.href='/compare/'+link;
}