function nothing() { }
$(document).ready(function() {
	setInterval(checkHash, 200);
	$(".button").button();
	$(".dialog").dialog({modal:true, autoOpen: false, resizable: false, draggable: false});
	$("#searchbox").keypress(function(ev) { if (ev.keyCode == '13') { switchTask('/advancedSearch.php?q='+$("#searchbox").attr('value')); } });
	$("#searchbox2").keypress(function(ev) { if (ev.keyCode == '13') { switchTask('/advancedSearch.php?q='+$("#searchbox2").attr('value')); } });
	$("#searchbox2").focus(function() { if ($(this).hasClass('searchboxDefault')) { $(this).attr('value',''); $(this).removeClass('searchboxDefault'); } });
	$("#searchbox").focus(function() { collapseBar(); if ($(this).hasClass('searchboxDefault')) { $(this).attr('value',''); $(this).removeClass('searchboxDefault'); } });
	applyStyles();
	$("#loginForm").validate({
			rules: { username: { required: true }, password: { required: true } },
			onkeyup: false,
			submitHandler: function(form) {
	   	$("#loginForm").ajaxSubmit({
	   		beforeSubmit: function() { $(".loading-login").show(); $(".ui-state-error").hide(); },
	   		success: function(responseText, statusText, xhr, $form) {
	   		$(".loading-login").hide(); if (responseText == "success") { reboot(); } else { $(".msg").html(responseText); $(".ui-state-error").show(); }
	   		} });
			},
			messages: { username: { required: "" }, password: { required: "" } }
		}); 
});

function checkHash() {
	if (switcherBusy === true) return false;
	if (window.location.hash.substring(0,2) == '/#') window.location.hash = window.location.hash.substring(1);
	if (window.location.hash == '#stay') return false;
	if (window.location.hash.substring(1,2) == '/') {
		if (window.location.hash == activeTask) { return false; }
		if (window.location.hash.substring(1) != '/') { switchTask(window.location.hash.substring(1)); }
	}
	var state = window.location.hash.split('-');
	if (state.length > 1) {
		if (state[0] == "#task") {
			if (state[1] == activeTask || switcherBusy === true) { return false; }
			switchTask(state[1]);
		} else if (state[0] == "#tabs") {
			if (activeTab != state[1]) { switchTab(state[1]); }
		}
	}
}

var activeTask = 'splash'; var activeTaskId = 'splash'; var activeTab = 0; var taskCleanup = Array();
var switcherBusy = false; var barStatus = true; var previousInfo = Array();
function collapseBar() { if (barStatus === false) { return; } $("#bar-tabs").hide("fold", function() { switchTab(0); } ); barStatus=false; }
function activateBar() { if (barStatus === true) { return; } $("#bar-tabs").show("fold"); barStatus = true; }
function updateBar(task) { if (activeTask == 'splash' && task != 'splash') { $("#banner").hide("fold"); $("#pagestrip").show(); } if (task == 'splash') { $("#banner").show("fold"); $("#pagestrip").hide(); } }
function hideCurrent(callback) { $("#task-container-"+activeTaskId).fadeOut(250, callback); }
function switchTask(task) {
	if (task == activeTask || switcherBusy === true) { return; }
	if (task.substring(0,1) == '/') window.location.hash = '#'+task;
	else window.location.hash = '#task-'+task;
	switcherBusy=true; updateBar(task);
	previousInfo['task'] = activeTask;
	previousInfo['title'] = '';
	// Run cleanup code
	if (taskCleanup["task-"+activeTask]) taskCleanup["task-"+activeTask]();
	// Remove code added by JQuery elements - Keep datepicker div since it is not regenerated
	$("#ajaxContent").nextAll(":not(#ui-datepicker-div):not([aria-labelledby='ui-dialog-title-changeUser']):not(#footer)").remove();
	if ($("#task-container-"+task).length != 0) {
		taskId = task;
		hideCurrent(function() { $("#task-container-"+taskId).fadeIn(250, function() { switcherBusy=false; } ); });
	} else {
		if (task.substring(0,1) == '/') url = task;
		else url = '/ajax/getTask.php';
		taskId = Math.random(); taskId = taskId.toString().substring(2);
		$("#loading").show();
		hideCurrent(function() {} );
		$.ajax({type:'GET',url: url, data:{'ajax':true,'task':task, 'taskId':taskId}, success:function(data) {
		$("#loading").hide();
		data = '<div id="task-container-'+taskId+'" class="task">'+data+'</div>';
		$("#ajaxContent").append(data);
		applyStyles();
		$("#task-container-"+taskId).fadeIn(250, function() { switcherBusy=false; });
		if ($(".javaScriptInterpret")) {
			eval($(".javaScriptInterpret").html());
			$(".javaScriptInterpret").remove();
		}
		}, error: function() { reboot('#'+url); }});
	}
	if ($("#task-container-"+activeTaskId).length > 0 && (activeTask != activeTaskId)) {
		$("#task-container-"+activeTaskId).remove();
	}
	activeTask=task; activeTaskId=taskId;
}

function callLogout() {	$.post('/ajax/doLogout.php', { }, function (data) { window.location = '/'; });}
function callLogin() { if (activeTask == 'splash') switchTask('loginForm_'); else switchTask('splash'); }
function reboot(link) { if (link && link != '') { window.location = '#'+link; } location.reload();}
function changeUser() { $('#changeUser').dialog('open');return false;}
function applyStyles() { 
$(document).ready(function() {
	$(".tableRow").children("div").addClass("tableColumn").children("div").addClass("ui-widget ui-state-default ui-button-text-only nobg").children("div").addClass("ui-button-text");
	$(".tableHead").children("div").children("div").removeClass("nobg");
	$(".buttonset").buttonset(); $(".button").button();
	if ($(".javaScriptInterpret")) { eval($(".javaScriptInterpret").html()); $(".javaScriptInterpret").remove(); }
	$("a.local").click(function() { switchTask(this.href.substring((document.domain.length+7))); return false; });
	if (getCookie("keyboard") == "off") { $("#keyboard_nowOff").show(); }
	else { $("#keyboard_nowOn").show(); VKI_buildKeyboardInputs(); }
	previousInfo['title'] = $("#pagetitle").html();
	if (previousInfo['title']) if (previousInfo['title'].length > 33) previousInfo['title'] = previousInfo['title'].substring(0,30) + '...';
	var title = $($($(".task-main")[0]).find(".title")[0]).text();
	if (title) if (title.length > 33) title = title.substring(0,30) + '...';
	$("#pagetitle").html(title);
	if (previousInfo['title'] != '') {
		$("#pageprevious").html(" <a title=\""+previousInfo['title']+"\" href=\"javascript:switchTask('"+previousInfo['task']+"')\">&lt;</a> ");
		$("#pageprevious").show();
	} else {
		$("#pageprevious").hide();
	}
	$(".tooltip").tipTip({maxWidth: "auto", edgeOffset: 10, delay: 100});
});
}

function setCookie(c_name,value,expiredays) { var exdate=new Date(); exdate.setDate(exdate.getDate()+expiredays); document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate.toUTCString()); }
function getCookie(c_name) { if (document.cookie.length>0) { c_start=document.cookie.indexOf(c_name + "=");
if (c_start!=-1) { c_start=c_start + c_name.length+1; c_end=document.cookie.indexOf(";",c_start);
if (c_end==-1) { c_end=document.cookie.length; }
return unescape(document.cookie.substring(c_start,c_end));
} } return ""; }

function setKeyboard(s) {
if (s=="on") { setCookie("keyboard","on","600"); VKI_buildKeyboardInputs(); $("#keyboard_nowOff").hide(); $("#keyboard_nowOn").show(); window.location.hash = ''; }
else { setCookie("keyboard","off","600"); $("#keyboard_nowOn").hide(); $("#keyboard_nowOff").show(); window.location.hash = ''; }
}

function deleteArticle(aid) { $.ajax({url:'/ajax/deleteArticle.php', type:'POST',data:{id:aid}, success: function(resp) {	alert(resp); }, error: function() { $(".formError").html("An unknown error occurred. Please try again."); } }); }

function addToCart(itemId) {
	$(".addToCart-"+itemId).hide(); $(".removeFromCart-"+itemId).show();
	$.ajax({type:'POST',url: '/ajax/doCart.php', data:{action:'add',id:itemId}, success:function(data) { if (data != 'done') { if (data == 'login') { window.location = '#task-loginForm_/article/'+itemId+'/article.html'; } $(".addToCart-"+itemId).show(); $(".removeFromCart-"+itemId).hide(); } }, error: function() { $(".addToCart-"+itemId).show(); $(".removeFromCart-"+itemId).hide(); }});
}

function removeFromCart(itemId) {
	$(".addToCart-"+itemId).show(); $(".removeFromCart-"+itemId).hide();
	$.ajax({type:'POST',url: '/ajax/doCart.php', data:{action:'remove',id:itemId}, success:function(data) { if (data != 'done') { $(".addToCart-"+itemId).hide(); $(".removeFromCart-"+itemId).show();} }, error: function() { $(".addToCart-"+itemId).hide(); $(".removeFromCart-"+itemId).show(); }});
}
