function hook_links()
{
	$('a.push_link').each(function (i)
	{
		$(this).unbind();
		$(this).click(function(){
			//var hash = this.href;
			//hash = hash.replace(/^.*?/, '');
			//alert(hash);

			//$.history.load(hash);
			UpdateContent($(this).attr("href"), "");
			return false;
		});
	});

}
function ask_delete()
{	if(confirm($(this).attr("rel")))
	{        UpdateContent2($(this).attr("href"));	}
	return false;}
function hook_delete_links()
{	$('a.ask_delete').each(function (i)
	{
		$(this).unbind();
		$(this).click(ask_delete);
	});
}
function page_load(data)
{	if(data) {
			// restore ajax loaded state
			if($.browser.msie) {
				// jquery's $.load() function does't work when hash include special characters like åäö.
				data = encodeURIComponent(data);
			}
	}
	//alert(data);
	UpdateContent2(data);
	return false;}
function UpdateContent2(data)
{
	//if($(this).attr("href"))alert($(this).attr("href"));
	//else alert(data);
	if($(this).attr("href"))
	{		UpdateContent($(this).attr("href"), "");

	}
	else if(data != "") UpdateContent(data, "");
    return false;
}
function UpdateContent(aUrl, aData, element, aSuccessFunction, aErrorFunction)
{
    //alert(aUrl);
   //  alert("OK");
    $('#wait').show().centering(1,1);
    if(!element)element = "content";
	$.ajax(
    {
    	url :aUrl,
    	data : aData+"&confirmer",
    	success : function(data)
    	{
    		if(aSuccessFunction) aSuccessFunction(data, element);
    		else
    		{

    			$('#wait').hide();
    			$("#"+element).html(data);
               // initLytebox();
                change_span();

                if(aUrl == "index.php?quit&confirmer")
			    {
			    	UpdateContent("index.php", "confirmer", "content");
			    	UpdateCart();
			    }
                // обработка ссылок приводящих к переходу
			    hook_links();
			    // для ссылок, которые будут приводить к удалению.
			    hook_delete_links();
			    // обработать ссылки для картинок
			    initLytebox();
			    //http://stitch.kh.ua/index.php?section=products&action=full_description&id=45
                if(aUrl.indexOf("category")>-1 || aUrl.indexOf("full_description")>-1)window.scrollTo(0, 0 );
               //var spylog = {counter: 2029249, image: 37, next: spylog };
               //document.write(unescape("%3Cscript src=%22http" +(("https:" == document.location.protocol) ? "s" : "") +"://counter.spylog.com/cnt.js%22 defer=%22defer%22%3E%3C/script%3E"));
    		}

    	},
    	error :  function(data, status)
    	{
    		if(aErrorFunction)aErrorFunction(data, status);
            else alert(status+":"+data.responseText);
    	}
    }
    );

    return false;
}
function replace_preloader(element, preloader)
{
   $("#"+element).attr("src",preloader);
   $("#"+element).css("margin","0px 30px 5px 33px");
   $("#"+element).blur();
}


function add_to_cart(product_id)
{

   c = $("#f_"+product_id+" input[name='count']").val();
   replace_preloader("s_"+product_id, "/images/loader_small.gif");
   //alert(c);

//   id = form.elements["id"].value;
 // $("#cart").dialog({title:"Cart",close:function(event,ui){$(this).dialog('destroy')} });
   $.get("index.php", {section: "shop", action: "add_to_cart", id: product_id, count: c, confirmer:""},
        function(data){
     //           alert(data);
                $("#cart_text").html(data);
                replace_preloader("s_"+product_id, "images/buy-btn.gif");
                $("#s_"+product_id).css("margin","0px");
                //setTimeout(function(){$("#cart").dialog("close");},1500);
        });
     return false;
}
function UpdateCart()
{	$.get("index.php", {section: "shop", action: "update_cart", confirmer:""},
    function(data){
     //           alert(data);
                $("#cart_text").html(data);
                //replace_preloader("s_"+product_id, "images/buy-btn.gif");
                //$("#s_"+product_id).css("margin","0px");
                //setTimeout(function(){$("#cart").dialog("close");},1500);
    });
    return false;}

function UpdateMessages()
{
  // alert('rtuert');
 $.ajax({
	   type: "POST",
	   url: "check.php",
	   data: {action: "update_messages"},
	   success: function(msg){	   //	alert(decodeURIComponent(msg));
	     $("#messages_info").html(decodeURIComponent(msg));
	     hook_links();
	   }
	 });

/*

    $.get("index.php",{section: "update_messages", act:"1"},

   function(data){
   		$("#messages_info").html(decodeURIComponent(data));
       hook_links();
   });

   */
}
function ajaxUploadAvatar()
	{
        $('#wait').show().centering(1,1);
		$.ajaxFileUpload
		(
			{
				url:'album_file_upload.php?act=upload_avatar',
				secureuri:false,
				fileElementId:'fileToUpload',
				dataType: 'json',
				success: function (data, status)
				{

					$('#wait').hide();
					if(typeof(data.error) != 'undefined')
					{
						if(data.error != '')
						{
							alert(data.error);
						}else
						{
							//alert(data.msg);
							$("#avatar").html(decodeURIComponent(data.msg));
						}
					}
				},
				error: function (data, status, e)
				{
					alert(e);
				}
			}
		)
		return false;
	}


