﻿var sidebar = {
	available: false,
	noshow: false,
	ajax: "/images/icons/ajax-loader-1-F.gif",
	arrowRight: "/images/icons/16/arrow_right_green.png",
	arrowLeft: "/images/icons/16/arrow_left_green.png",
	height: 0,
	width: 250,
	maxwidth: 500,
	left: 1008,
	top: 0,
	state: true,
	refreshSet: false,
	intervalID: 0,

	position: function() {
		if (this.width > this.maxwidth) { this.width = this.maxwidth; }
		$("#Sidebar").css("width", this.width).css("height", this.height);
	},

	show: function() {
		$("#Sidebar").show();
	},

	hide: function() {
		$("#Sidebar").hide()
	},

	showcontent: function() {
		$("#SideContent").show("slow");
	},

	hidecontent: function() {
		$("#SideContent").hide("slow");
		clearInterval(this.intervalID);
		this.intervalID = 0;
	},

	ajaxLoad: function() {
		$("#Ajax").show();
		$("#SideContent").load("/Sidebar.asp?Rnd=" + Math.random(), "", function() { sidebar.ajaxComplete(); });
	},

	ajaxStart: function() {
		$("#Ajax").show();
	},

	ajaxComplete: function() {
		$("#SideContent div h1")
			.css("cursor", "pointer")
			.click(function() {
			$(this).siblings("div").toggle();
		}
		);
		$("#Ajax").hide();
		//this.refresh();
	},

	ajaxError: function() {
		alert("Error!");
	},

	refresh: function() {
		if (this.intervalID == 0) { this.intervalID = window.setInterval("sidebar.ajaxLoad()", 300000); }
	},

	reSizeIt: function() {
		this.height = $(document).height();
		//sidebar.width = parseInt($(window).width()) - sidebar.left;
		//alert(parseInt($(window).width()) + ' < ' + (this.left + this.width));
		if (parseInt($(window).width()) < (parseInt(this.left + this.width) + 1)) {
			this.hide();
		} else {
			if (!this.available) {
				this.render();
			} else {
				this.position();
				this.show();
			}
		}
	},

	render: function() {
		//if (($.browser.msie) && (parseInt($.browser.version, 10) < 8)) { this.noshow = true; }
		if ((!this.available) && (!this.noshow)) {
			this.height = $(document).height();
			//alert($("body table").width());
			//this.width = parseInt($(window).width()) - (this.left + 1);
			$("body").append("<div id='Sidebar'><img id='Ajax' class='Right' src='" + this.ajax + "' alt='loading...' /><a class='Right' href='/SidebarConfig.asp'><img src='/images/icons/16/gear.png' border='0' alt='Configure Sidebar' /></a><a id='Arrow'><img id='ArrowImg' class='Left' src='" + this.arrowRight + "' alt='' border='0' /></a><br clear='all' /><div id='SideContent'></div></div>");
			this.position();

			$("#Arrow").click(function() {
				//alert($(this).children("img").attr("src") + ' :: ' + sidebar.arrowRight);
				if ($(this).children("img").attr("src") == sidebar.arrowRight) {
					$("#Sidebar").animate({ width: "20px", left: (this.left + this.width) - 22 }, 1500, "", sidebar.hidecontent());
					$(this).children("img").attr("src", sidebar.arrowLeft);
					sidebar.state = false;
				} else {
					$("#Sidebar").animate({ width: sidebar.width, left: sidebar.left }, 1500, "", sidebar.showcontent());
					$(this).children("img").attr("src", sidebar.arrowRight);
					sidebar.state = true;
				}
				return false;
			});

			if (!this.state) {
				$("#Arrow").click();
			}

			this.available = true;

			this.ajaxLoad();

			$("#AccountDiv").hide();
		}
	},

	validate: function() {
		//if (($.browser.msie) && (parseInt($.browser.version, 10) < 8)) { this.noshow = true; }
		if ((!this.available) && (!this.noshow)) {
			this.left = $("body table").width() + 1;
			//alert($(window).width() + '>=' + (this.left + this.width) + ' = ' + ($(window).width() >= this.left + this.width));
			if (parseInt($(window).width()) >= (this.left + this.width)) {
				this.render()
			}
		}
		//Setup Resize event to reset position of the bar
		$(window).wresize(function() { sidebar.reSizeIt(); });
	}
};