
$.cookie('__tcoo', 1);
//--
if ($.cookie('__tcoo') && !$.cookie('sid'))
{
	window.location = '/';
}

function str_replace(search, replace, subject) {
    return subject.split(search).join(replace);
}

function historyCall(hash, sl)
{
	if(hash) 
	{
		if (!sl) 
		{
			//var i = parseInt(hash.replace('p_', ''));
			//Navigate.geth(Navigate.historyStock[i], Navigate.historyIdStock[i], false);
		}
		
		return false;

	} else 
	{
		Navigate.geth('x-galery.subcat.aspx?categoryid=20&stay=20', 'left-tape_body', false);
	}

};


var Navigate = {

	url					: null,
	html					: null,
	id						: null,
	historyStock		: [],
	historyIdStock	: [],
	historyStockI	: 0,

	locked				: [],

	cache				: [],

	categoryId		: 20,
	stayId				: 20,
	itemId				: 0,
    isSendReproductions: false,
    imgSrc: '',
	imgUrl: '',
    geth : function(url, id, vshistory)
	{

		
        var ukey = url.match(/^([^?]+)\?/);

		/*if (Navigate.locked[ukey])
		{
			return false;
		}
                   */

		if (Navigate.url && Navigate.url.match(/\/html\//) /* && $.browser.msie */)
		{
//			window.location.href = url;
		}

		Navigate.locked[ukey]= true;

		if (vshistory && jQuery.historyCallback)
		{
			Navigate.historyAdd(url, id);
		}

		if(url.indexOf("x-galery.subcat.aspx")==0 || url.indexOf("x-entity.galery.grid.aspx")==0)
		{
			
            var reg = new RegExp("stay=(\\d+)", "i");
			var reg1 = new RegExp("categoryID=(\\d+)", "i");

			var reg2 = new RegExp("categoryID=64", "i");
			if(reg2.test(url))
			{
				Tape.obHide = true;
			}
			else
			{
				Tape.obHide = false;
			}
		}

		this.url		= url;
		this.html	= null;
		this.id		= id;

		Navigate.loadHtml(url);

	},

	historyAdd : function(url, id)
	{

		Navigate.historyStock[Navigate.historyStockI]	= url;
		Navigate.historyIdStock[Navigate.historyStockI]	= id;
        
		//$.historyLoad('p_' + Navigate.historyStockI, true);

		Navigate.historyStockI++;

	},

	loadHtml : function(url)
	{
		var url = url;
		var ukey = url.match(/^([^?]+)\?/);
        
		$.ajax({
			url: url,
			cache: false,
			dataType: "text",
			success: function(html){
//debugger;
				var name = $('#page_menu_click_name').val();
                var tmp_url = document.location.href + ':::' + name;
                var tmpp = tmp_url.split(':::');
                var isLiteral = document.location.href.indexOf('#');
                if (isLiteral == -1) {
                    tmpp[0] = tmpp[0] + '#';
                }
                
                document.location.href = tmpp[0] + ':::' + name;
                Navigate.html = html;
				Navigate._refresh(Navigate.html);
				Navigate.locked[ukey] = false;
                if (Navigate.isSendReproductions) {
                    Navigate.isSendReproductions = false;
                    $('#sfForm').append(Navigate.imgSrc);
                    $('#block_img_45132').html('<img src="'+Navigate.imgUrl+'" width="300" alt="img" />');
                }
               // document.location.href = document.location.href + '-' + name;
			}
		});

	},

	_refresh : function(html)
	{
		var reg = new RegExp("AddToCartForm_[\\w]+_[\\w]+_Validator", "gi");
		html = html.replace(reg, '');

		var reg = new RegExp('id="AddToCart_', "gi");
		html = html.replace(reg, 'class="AddToCart" id="AddToCart_');

		var reg = new RegExp('id="AddToWishList_', "gi");
		html = html.replace(reg, ' class="AddToWishList" id="AddToWishList_');

		var reg = /AddToCartForm_\d+/g;
		var fid = '';
		if (result = reg.exec(html))
		{
			fid = result[0].replace('AddToCartForm_', '');
		}

		var reg = new RegExp('action="addtocart.aspx.returnurl', "gi");
		html = html.replace(reg, 'action="http://trevimedia.net/shop/iframe.aspx?id='+fid+'&returnurl');

		var ds = (this.id == 'content_box' ? 'block' : 'none');

		$('#content_box').css({display: ds});
		$("#"+this.id).html(html);

		html = html.replace(/\&amp\;/gi, '&');
		
		if (this.id == 'content_box')
		{
			$("#content").jScrollPane();
		} else {
            $('#content_box').css('display', 'block');
        }
        
        
        
		// -- embed script eval
		try
		{
			eval(/\<div\ class\=\"embed-script\"\>([^<]+)\<\/div\>/.exec(html)[1]);
		}
		catch(e)
		{}
 	},

	postOrder : function ()
	{
		var productId = $('.ProductID').val();

		var url = '/order.php?productId=' + productId + '&imgId=' + Img.imgId;

		$.get(url, function (data) {
			
			if (data != 0)
			{
				if (data == 'ok')
				{
					$('#img-order-form').submit();
				}
				else
				{
					alert('Unfortunately, there is an error has occured! The page will reload now, so, after that, please, repeat you order.');
//					window.location = '/default.aspx?categoryid=' + Navigate.categoryId + '&subcategoryid=' + Navigate.stayId + '&id=' + Navigate.itemId;
					window.location = '/';
				}
			}
			else
			{
				alert('This image already purchased');
			}

		});

	}

};

