
var isSetAsIE6 = false;
var original_page = true;

var allowExtraVars = false;
var extraVar1 = '';
var extraVar2 = '';
var extraVar3 = '';

function pageLoad(hash,option) {
	// callback for handling ajax history/bookmarking if required
	if (hash.substring(0,1)=='/') {
		hash = hash.substring(1);
		if (option=='init') {
			var uri = parseUri(document.location.href);
			ajaxPage(uri.protocol+"://"+uri.host+uri.directory+hash,'',true);
		} else if (option=='check') {
			var uri = parseUri(document.location.href);
			ajaxPage(uri.protocol+"://"+uri.host+uri.directory+hash,'',true);
		}
	} else if (!original_page && document.location.href.indexOf('#') > -1 && hash == '') {
		var uri = parseUri(document.location.href);
		ajaxPage(uri.protocol+"://"+uri.host+uri.directory+uri.file+(uri.query!=''?'?'+uri.query:''),'',true);
	}
	original_page = false;
}

function showIndicator() {
	$('#indicator').defaultIndicator();
	$("#indicator").show();
}

function ajaxPage(href,reqtype,history,ni) {
	if (history==''||history==undefined) history=false;
	if (reqtype==''||reqtype==undefined) reqtype='default';
	if (ni==''||ni==undefined) ni=false;
	if (ni!=true) showIndicator();
	$.ajax({
	    url: href,
	    type: 'POST',
		data: {ajax:1,ajaxtype:reqtype},
	    dataType: 'xml',
	    timeout: 10000,
	    error: function(){
			document.location.href=href;
	    },
	    success: function(xml){
			if (processResult(xml)) {
				if (history) {
					var uri = parseUri(href);
					var uri2 = parseUri(document.location.href);
					if (uri.file!=uri2.file || uri.query!=uri2.query) {
						$.historyLoad('/'+uri.file+(uri.query!=''?'?'+uri.query:''));
					} else {
						original_page = true;
						$.historyLoad('');
					}
				}
		    	$("#indicator").hide(200);
			}
	    }
	});
}

function ajaxPageNI(href,reqtype,history) {
	if (history==''||history==undefined) history=false;
	if (reqtype==''||reqtype==undefined) reqtype='default';
	ajaxPage(href,reqtype,history,true);
}

function ajaxForm(oform,ni,wt,nhi) {
	if (wt==''||wt==undefined) {
		if (oform.id=='theLongForm') {
			// time out based on the form id
			wt=120000;
		} else if (document.forms[oform.id].timeout) {
			// use the specific time out setting
			wt=parseInt(document.forms[oform.id].timeout.value);
		} else {
			// default time out setting
			wt=10000;
		}
	}
	if (ni==''||ni==undefined) ni=false;
	if (nhi==''||nhi==undefined) nhi=false;
	if (ni!=true) showIndicator();
	$('#'+oform.id).ajaxSubmit({
	    url: oform.action,
	    type: 'POST',
	    dataType: 'xml',
	    timeout: wt,
	    error: function(){
			document.forms[oform.id].ajax.value='x';
			$('#'+oform.id).submit();

	    },
	    success: function(xml){
			if (processResult(xml)) {
		    	if (nhi!=true) $("#indicator").hide(200);
			}
	    }
	});
	return false
}

function ajaxFormNI(oform) {
	ajaxForm(oform,true);
	return false
}

function ajaxReq(href,reqtype,ni) {
	if (reqtype==''||reqtype==undefined) reqtype='default';
	if (ni==''||ni==undefined) ni=false;
	if (ni!=true) showIndicator();
	if (allowExtraVars) {
		var tempData = {ajax:1,ajaxtype:reqtype,exVar1:extraVar1,exVar2:extraVar2,exVar3:extraVar3};
		allowExtraVars = false;
	} else {
		var tempData = {ajax:1,ajaxtype:reqtype};
	}
	$.ajax({
	    url: href,
	    type: 'POST',
		data: tempData,
	    dataType: 'xml',
	    timeout: 10000,
	    error: function(){
			document.location.href=href;
	    },
	    success: function(xml){
			if (processResult(xml)) {
		    	$("#indicator").hide(200);
			}
	    }
	});
}

function ajaxReqNI(href,reqtype) {
	if (reqtype==''||reqtype==undefined) reqtype='default';
	ajaxReq(href,reqtype,true);
}

function ajaxProc(href,reqtype,ni) {
	if (reqtype==''||reqtype==undefined) reqtype='default';
	if (ni==''||ni==undefined) ni=false;
	if (ni!=true) showIndicator();
	$.ajax({
	    url: href,
	    type: 'POST',
		data: {ajax:1,ajaxtype:reqtype},
	    dataType: 'xml',
	    timeout: 10000,
	    error: function(){
	    },
	    success: function(xml){
			if (processResult(xml)) {
		    	$("#indicator").hide(200);
			}
	    }
	});
}

function processResult(xml) {
	var redirect = '';var aj_redirect = '';
	$(xml).find('item').each(function(){
		var elem_id = $(this).find('name').text();
		var item_opt = $(this).find('option').text();
		var item_text = $(this).find('data').text();
		if (elem_id=='#redirect') {
			redirect = item_text;
		} else if (elem_id=='#aj_redirect') {
			aj_redirect = item_text;
		} else if (elem_id.substring(0,4)=='#js_') {
			$.globalEval(item_text);
		} else {
			if (item_opt!='general') {
				eval('\$(elem_id).'+item_opt+'(item_text);');
			} else {
				$(elem_id).html(item_text);
				loadAutoClass(elem_id);
			}
		}
    });
	if (redirect) {
		document.location.href=redirect;
		return false;
	}else if (aj_redirect) {
		ajaxReq(aj_redirect);
		return false;
	} else {
		return true;
	}
}

function preProcess(history) {
	if($.browser.safari) {
		// not very reliable, so don't use it
	} else {
		if (history) $.historyInit(pageLoad);
	}
	loadAutoClass('');
	browserFix();
	preLoadImg();
}

function loadAutoClass(id) {
	$(id + (id!=''?' ':'') + 'a.ajax').bind('click', function() {
		ajaxPage(this.href,'',($.browser.safari?false:true));
		return false;
	});
	$(id + (id!=''?' ':'') + 'a.ajax2').bind('click', function() {
		allowExtraVars = false;
		ajaxReq(this.href);
		return false;
	});
	$(id + (id!=''?' ':'') + 'a.ajax3').bind('click', function() {
		allowExtraVars = true;
		ajaxReq(this.href);
		return false;
	});
	$(id + (id!=''?' ':'') + 'form.ajaxform').submit(function() {
		if (this.ajax.value == 'x') {
			// previous attempt failed lets not use ajax this time around
			this.ajax.value=0;
			return true;
		} else {
			this.ajax.value=1;
			return(ajaxForm(this))
		}
	});

	$(id + (id!=''?' ':'') + 'a.lightPop').bind('click', function() {
		openLightbox(this);
		return false;
	});

	if (id=='') {
		$("#basketTab").hover(function() {
			showMiniBasket();
		});
		$("#basketTab").click(function() {
			showMiniBasket();
			return false;
		});
	}
}

function browserFix() {
}

function goTo(href) {
	document.location.href=href;
	return false;
}

function sendForm(mvar,oform,ret,mval) {
	if (document.forms[oform].iaction) {
		document.forms[oform].iaction.name='iaction';
		document.forms[oform].iaction.value='';
		if (mval!=undefined) document.forms[oform].iaction.value=mval;
		if (mvar!='') document.forms[oform].iaction.name=mvar;
	}
	$('#'+oform).submit();
	return (ret!='' && ret!=undefined ? ret : false);
}

function MM_preloadImages() {
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function getStamp() {
	var jar=new Date();
	var flashc=jar.getTime();
	return flashc;
}

function preLoadImg() {
}

var miniBasketOpt = 0;
function showMiniBasket() {
	if (miniBasketOpt != 1) {
		miniBasketOpt = 1;
		if (!isSetAsIE6) {
			$("#basketTab").addClass("selected");
		}
		$("#miniBasket").slideDown({
			duration: 500,
			easing: "easeOutCirc"
		});
		$("body").click(function() {
			hideMiniBasket();
		});
		if ($.browser.msie && $.browser.version == "6.0") {
			$("#basketTab").css({ zoom: "0" });
		}
	}
	return false;
}

function hideMiniBasket() {
	if (miniBasketOpt != 0) {
		$("body").unbind("click");
		miniBasketOpt = 0;
		if($.browser.msie && $.browser.version == "6.0"){
			$("#basketTab").css({zoom: "0"});
		}
		if (!isSetAsIE6) {
			$("#basketTab").removeClass("selected");
		}
		$("#miniBasket").slideUp({
			duration: 500,
			easing: "easeOutCirc"
		});
	}
	//return false;
}

function hideMiniBasketQuick() {
	if (miniBasketOpt != 0) {
		$("body").unbind("click");
		miniBasketOpt = 0;
		if($.browser.msie && $.browser.version == "6.0"){
			$("#basketTab").css({zoom: "0"});
		}
		$("#miniBasket").hide();
	}
	//return false;
}

function videoPlayer(id,cb1,video,width,height,img,autoStart,autoRepeat,skin) {
	if (autoStart==''||autoStart==undefined) autoStart=false;
	if (autoRepeat==''||autoRepeat==undefined) autoRepeat=false;
	if (skin==''||skin==undefined) skin='';
	var version = deconcept.SWFObjectUtil.getPlayerVersion();
	if (version["major"] < 1) {
		$('#'+id).innerHTML('You need flash installed to play this video.');
	} else {
		var s1 = new SWFObject('/flash/flvplayer2.swf?cb1='+cb1, 'showplayer1', width, height, '1', '#000000');
		s1.addParam('allowfullscreen','true');
		s1.addVariable('file',video);
		s1.addVariable('image',img);
		s1.addVariable('backcolor','0x000000');
		s1.addVariable('frontcolor','0xFFFFFF');
		s1.addVariable('lightcolor','0xFFFFFF');
		s1.addVariable('autostart',(autoStart ? 'true' : 'false'));
		if (skin != '') {
			s1.addVariable('skin',skin);
		}
		if (autoRepeat) {
			s1.addVariable('repeat','single');
		}
		s1.write(id);
	}
}

var player = null;
function playerReady(thePlayer) {
	player = document.getElementById(thePlayer.id);
	addListeners();
}

function addListeners() {
	if (player) {
		player.addModelListener("STATE", "stateListener");
	} else {
		setTimeout("addListeners()",100);
	}
}

function stateListener(obj) { //IDLE, BUFFERING, PLAYING, PAUSED, COMPLETED
	currentState = obj.newstate;
	previousState = obj.oldstate;
	var tmp = document.getElementById("stat");
	if (tmp) {
		tmp.innerHTML = "current state: " + currentState +
		"<br>previous state: " + previousState;
	}
	if ((currentState == "COMPLETED")&&((previousState == "PLAYING" || previousState == "IDLE"))) {
		if (typeof window.onPlayerFinish == 'function') {
			onPlayerFinish(obj);
		}
	}
}

function omni(mType,mCode) {
	document.cookie="posttrk="+escape(mType)+";path=/";
	document.cookie="postalt="+escape(mCode)+";path=/";
	return true;
}

function checkSearch(e) {
	if (e.value=='SEARCH') {
		e.value = '';
	} else {
		e.select()
	}
}

function checkSearch2(e) {
	document.forms['siteSearch']['searchTerms'].onclick();
}

