/***************************
* Random Image
****************************/
function RandomImage(timeout, timeout_rate){
	this.flip = function(){
		$.ajax({
				type: "GET",
				async: false,
				url: "/random_image",
				dataType: "xml",
				success: function(xml){
					var img = $(new Image());
					img.attr("src", $("src", xml).text());
					img.load(function(){
							$("#corner_img").attr("href", $("href", xml).text());
							$("#corner_img").css("background-image", "url("+$("src", xml).text()+")");
						}
					);
				}
			}
		);
		timeout *= timeout_rate;
		this.set_timeout();
	};
	this.set_timeout = function(){
		var thisObj = this;
		setTimeout(function(){thisObj.flip();}, timeout);
	};
	this.set_timeout(); /* init */
}

/***************************
* Print Functions
****************************/
function PrintHelper(html, options){
	var mini_window = window.open("", "", options);
	mini_window.document.write(html);
	mini_window.document.close();
	mini_window.print();
	mini_window.close();
}

function PrintLabel(html){
	PrintHelper(html, "toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=600,height=400,left=300,top=300");
}

function PrintPage(url){
	$.ajax({
			type: "GET",
			async: false,
			url: url,
			dataType: "html",
			success: function(html){
				PrintHelper(html, "toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width=800,height=600,left=200,top=200");
			}
		}
	);
}

/***************************
* Utility Functions
****************************/
function reload(){
	window.location.reload(true);
}

function Disable(id){
	$("#"+id).attr("disabled", true);
}

function GetValue(id){
	var value = $("#"+id).attr("value");
	if (value === undefined){
		value = "";
	}
	return value;
}

function SetValue(id, value){
	$("#"+id).attr("value", value);
}

function GetChecked(id){
	return $("#"+id).attr("checked");
}

function SubmitForm(id){
	$("#"+id).submit();
}

function EnterPressed(e){
	if(e.which){
		keyCode = e.which;
	}else if(e.keyCode){
		keyCode = e.keyCode;
	}
	if(13 == keyCode){
		return true;
	}
	return false;
}

function HideAnimate(id){
	$("#"+id).slideUp("fast");
}

function ShowAnimate(id){
	$("#"+id).slideDown("fast");
}

/***************************
* Setting vales in the WebBase
****************************/
function SetPageOp(id, op){
	Disable(id);
	$.ajax({
			type: "POST",
			async: true,
			url: "/null",
			data: op+'='+GetValue(id),
			complete: function(xmlhttp, type){reload();}
		}
	);
}

function DeleteItem(id){
	if (confirm('Do you want to delete item with ID: '+id+'?')){
		$.ajax({
				type: "POST",
				async: true,
				url: "/del_item",
				data: ITEM_ID_KEY+'='+id,
				complete: function(xmlhttp, type){reload();}
			}
		);
	}
}

function SetCart(id, quantity){
	$.ajax({
			type: "POST",
			async: false,
			url: "/set_cart",
			data: ITEM_ID_KEY+'='+id+'&amp;'+QUANTITY_KEY+'='+quantity
		}
	);
}

function SetOrderedItem(order_id, ordered_item_id, status){
	$.ajax({
			type: "POST",
			async: true,
			url: "/set_ordered_item",
			data: ORDER_ID_KEY+'='+order_id+'&amp;'+ORDERED_ITEM_ID_KEY+'='+ordered_item_id+'&amp;'+ITEM_STATUS_KEY+'='+status,
			complete: function(xmlhttp, type){reload();}
		}
	);
}

/***************************
* Init
****************************/
$(document).ready(function(){
		$(".hide_by_default_with_js").each(function(i, box){
				$(box).hide();
			}
		);
		$(".checkbox").each(function(i, box){
				$(box).attr("onclick")();
			}
		);
		$("#banner").each(function(){
				var image = new RandomImage(5000, 1.3);
				var train = new RandomTrain(3000, 1.7);
			}
		);
		$.preloadCssImages();
	}
);

