var _loadedHash = null;
var lockLink = false;
var allScriptsLoaded = false;
var lockAlert = false;
var alertMsg = 'درخواست شما با مشکل مواجه شده است.';
var scriptPath = 'templates/Gift/js/';
test=false;
$(document).ready(function(){
	var $body = $("#content");
	$("a:not([target='_blank'])").livequery('click',function(){
		var h = $(this).attr("href");
		if(exceptionLinks($(this)) == 1) return false;
		if(exceptionLinks($(this)) == 0) return ;
		if(exceptionLinks($(this)) == -1)return true;
		if(!lockLink && allScriptsLoaded){
			$.get(h, {'ajax_check':'true'} , function(response){
				response = response;
				$body.empty().append(response);
				/* if(!test) {
				$body.html(response);
				}else{
				} */
			});
			if (typeof check4update=="function")check4update(h);
			history(h);
		}
		return false;
	}).ajaxStart(function(e) {
		if(!lockLink && allScriptsLoaded){
			lockLink = true;
			$("#loading").css('display','');
		}
	}).ajaxStop(function() {
		if(lockLink){
			lockLink = false;
			$("#loading").css('display','none');
		}
	}).ajaxError(function(){
		if(!lockAlert){
		try{ myAlert(); }
		catch(e){ }
		}
	});
		
	$('form').livequery('submit',function(){
		var method = $(this)[0].method.toUpperCase();
		var action = $(this).attr('action');
		var name   = $(this).attr('name');
		var attributes = $(this).find('input').serialize();
		if ( $(this).find('textarea').serialize() != '' )
			attributes += '&' + $(this).find('textarea').serialize();
		attributes += '&ajax_check=true&' + $(this).find('select').serialize();
		if(method == '') method='POST';
		switch(method){
			case 'POST':
				$.post(action,attributes,function(data){
					$body.html('<center>' + data + '</center>');
				});
			break;
			
			case 'GET':
				$.get(action,attributes,function(data){
					$body.html('<center>' + data + '</center>');
				});
				break;
		}
		if ( typeof check4update == "function" ) check4update(action);
		history(action);
		return false;
	});
	
	$(".bookmarks > LI IMG").css('opacity',0.6).hover(function(){ $(this).css('opacity',1); },function(){ $(this).css('opacity',0.6);});


//////////////////////////////BOF GO TOP
	var checkScrollbarPosition = function(){
		if ( document.documentElement.scrollTop > $("#content").offset().top + $("#content").height() )//&& $.scrollTo) 
			$("#goTopObj").show();
		else if( $("#goTopObj").css('display') != 'none')
			$("#goTopObj").hide();
	};
	$("#goTopObj > SPAN").click(function(){
		try{$.scrollTo("#main",700)}catch(e){}
	});
//////////////////////////////EOF GO TOP


/////////////////////////////////////// BOF History Plugin
	var getHash = function(myHash){
		h = myHash;
		if ( h.length < 1) h = 'index';
		if ( h.indexOf(':') > 0 ){ 
			h = h.replace(/^#/,'').replace(':','.php?');
			while( h.indexOf('/') > 0 ) h = h.replace('/','&');
		}
		else { h = h.replace(/^#/,''); h += '.php'; }
		$.get(h,{'ajax_check':'true'},function(data){$body.html('<center>' + data + '</center>');});
		if($.browser.msie) _loadedHash = _historyIframe.contentWindow.document.location.hash;
		else _loadedHash = myHash;
		
	};
		
	if ($.browser.msie) {
		var _historyIframe = $('<iframe style="display: none"></iframe>').appendTo("BODY").get(0);
		var iframe = _historyIframe.contentWindow.document;
		iframe.open();
		iframe.close();
		setInterval(function(){
			checkScrollbarPosition();
			var h = _historyIframe.contentWindow.document.location.hash;
			if ( (_loadedHash != null) &&  ( _loadedHash != h) ) 	getHash(h);
			
		}, 500);
	} else if ($.browser.mozilla) {
		setInterval(function(){ 
			checkScrollbarPosition();
			if ( (_loadedHash != null) && ( _loadedHash != location.hash) )	getHash(location.hash);
		}, 500);
	}

	try{
		if(location.hash.length > 0 && !$.browser.msie)
			getHash(location.hash);
	}catch(e){ alert(e.name + ' ' + e.message); }

	

	var history = function(path){
		var qmp = (path.indexOf('?') > 0 ) ? path.indexOf('?') + 1 : path.length + 1;
		var lp = path.length;
		var fileNameP1 = path.indexOf('.php') + 1;
		var fileNameP2 = (qmp > 1) ? qmp-1 : path.length;
		var params = path.substring(qmp,lp);
		var newPath = '';
		
		var fileName = path.substring(  path.substring(0,fileNameP2).lastIndexOf('/')+1 , fileNameP2 ).replace('.php','');
		if (params.length > 0 ){
			var a = explodeArray(params,"&");
	
			for (var i=0, j=a.length; i<j; i++)	newPath += a[i] + '/';
			newPath = '#' + fileName + ':' + newPath;
		} else {
			newPath = '#' + fileName;
		}
		if ($.browser.msie) {
			var iframe = _historyIframe.contentWindow.document;
            iframe.open();
            iframe.close();
            iframe.location.hash = newPath;
		} 
		location.hash = newPath;
		_loadedHash = newPath;
	};
	
///////////////////////////////////////EOF History Plugin
	
		if(!$.browser.msie)
			$(".productListing-even , .productListing-odd , .product_table").hover(function(){
				if ( $(this).hasClass('product_table') ) $(this).addClass('product_tableBg');
				$(this).animate({opacity:.39},'fast',function(){
					$(this).animate({opacity:1},'slow');
				});
			},function(){
				if ( $(this).hasClass('product_tableBg') ) $(this).removeClass('product_tableBg');
			});

		$('input[@type^="text"]').addClass('textbox_gray , textbox').focus(function(){ $(this).removeClass('textbox_gray');});;
		$('input[@type^="password"]').addClass('textbox');
		$('textarea').addClass('textareabox');
		$('.noAvail').click(function(){
			if($.jGrowl)$.jGrowl('موجودی این محصول به پایان رسیده و دیگر به فروش نمی رسد. برای کسب اطلاعات بیشتر از کالاهای جدید به سایت <a href="http://www.giftcarpet.com" target="_blank">Gift Carpet</a> مراجعه کنید.', { life:30000, header:'کاربر گرامی!'} );
			return false;
		});
		disableSelection(document.body);
		$("#loading").css('display','none');
});
function clickIE4(){if (event.button==2){return false;}}
function clickNS4(e){if (document.layers||document.getElementById&&!document.all){if (e.which==2||e.which==3){return false;}}}
if (document.layers){document.captureEvents(Event.MOUSEDOWN);document.onmousedown=clickNS4;}else if (document.all&&!document.getElementById){document.onmousedown=clickIE4;}
document.oncontextmenu=new Function("return false");
function disableSelection(target){
if (typeof target.onselectstart!="undefined") //IE route
	target.onselectstart=function(){return false}
else if (typeof target.style.MozUserSelect!="undefined") //Firefox route
	target.style.MozUserSelect="none"
else //All other route (ie: Opera)
	target.onmousedown=function(){return false}
target.style.cursor = "default"
}
$(window).load(function(){
	$.getScript(base+scriptPath+'interface/iutil.js');
	$.getScript(base+scriptPath+'interface/easing.js',function(){
		allScriptsLoaded = true;
		$.getScript(base+scriptPath+'boxAndTable.js');
		$.getScript(base+scriptPath+'logo.js',function(){
			$('.kwicks').kwicks({max: 250,spacing:  0});
			$(".kwicks > LI").hover(function(){ $(this).find(".smallLetters").show("normal"); },function(){ $(this).find(".smallLetters").hide("noraml"); });
			//$.getScript(base+scriptPath+'headerEffect.js',function(){$(".lavaLampNoImage").lavaLamp({fx: "backout",speed: 700,click: function() {return false;}});});
			/* if($.browser.msie){
				$.getScript(base+scriptPath+'corner.js',function(){
					if($.corner){
					$(".box_title , #goTopObj").corner("top");
					$(".lavaLampNoImage").corner("bottom");
					}
				});
			}  */
			$.getScript(base+scriptPath+'jgrowl.js',function(){
				$.getScript(base+scriptPath+'scrollTo.js');
				allScriptsLoaded = true;
			});
		});
	});
});


function explodeArray(item,delimiter){
	tempArray=new Array(1);
	var Count=0;
	var tempString=new String(item);
	while (tempString.indexOf(delimiter)>0){
		tempArray[Count]=tempString.substr(0,tempString.indexOf(delimiter));
		tempString=tempString.substr(tempString.indexOf(delimiter)+1,tempString.length-tempString.indexOf(delimiter)+1);
		Count++;
	}
	tempArray[Count]=tempString;
		return tempArray;
}


