var is_login = true;
var resizeFunctions = [];

if(!Array.indexOf){
	    Array.prototype.indexOf = function(obj){
	        for(var i=0; i<this.length; i++){
	            if(this[i]==obj){
	                return i;
	            }
	        }
	        return -1;
	    }
	}



var currentPage = [0];
var resetTimer;
var topicsTimer;
var myDataTable;

$(document).ready(function() {
	navOut();
});
function navOver(a) {
	clearTimeout(resetTimer);
	var navIndexes = (a == null) ? [] : a.id.split("navLink").join("").split("_");
	for (x=0; x<=6; x++) {
		try {
		if($('#navItem'+x)) {
			imageBaseName = $('#navImage'+x).attr('src').split(".gif").join("").split("_on").join("");

			$('#navItem'+x).removeClass();
			if (navIndexes[0] == x) {
				$('#navItem'+x).addClass('nav0ItemOn');
				$('#navImage'+x).attr('src', imageBaseName + "_on.gif");
			} else {
				$('#navItem'+x).addClass('nav0ItemOff');
				$('#navImage'+x).attr('src', imageBaseName + ".gif");
			}
		}
		} catch(error) {}
	}
	
}

function navOut() {
	resetTimer = setTimeout("navReset()",200);
}

function navReset() {
	navOver(document.getElementById("navLink" + currentPage.join("_")));
}

function over(a, on) {
	var linkTxt = new RegExp("Link");
	var img = $("#"+a.id.replace(linkText, "Img"));
	var imageBaseName = img.attr('src').split(".gif").join("").split("_on").join("");
	if (on)
		imageBaseName += "_on.gif";
	else
		imageBaseName += ".gif";
	img.attr('src', imageBaseName);
}

function add_item(item_number) {
	postData = {'item_number':item_number};
	$.ajax({
		type:'POST',
	    url:"/test.pl",
		data:postData,
		success:alert_complete
		//dataType:'json',
	});
}

function alert_complete(res) {
	alert(res);
}

function getProperty(name, selector) {
	if (!selector) 
		selector = '#colright';
	return parseInt($(selector).css(name))
}

$(document).ready(function() {
	fixColumnHeight();
});
$(window).load(function() {
	fixColumnHeight();
});

function fixColumnHeight() {

	if ($('#footerText').position() && $('#footer').position()) {
		var footerSize = $('#footerText').position().top + parseInt($('#footerText').height()) + parseInt($('#footerText').css('padding-top')) + getProperty('padding-bottom', '#footerText') - $('#footer').position().top;
		var start = $('#colright').position().top;
		if (start > 220) {
			start = 220;
		}
		var size = $.viewportHeight() - start - footerSize - getProperty('padding-top') - getProperty('padding-bottom');
		var width = $.viewportWidth() - getProperty('padding-left') - getProperty('padding-right') - getProperty('width', '#colleft');
		if (is_login) {
			width -= $('#news').css('width');
		}
		var totalHeight = 0;
		$('#colright').children().each(function() {
			var child = $(this);
			if (child.attr('id') != 'content' && child.css('display') != 'none') {
				totalHeight += child.outerHeight(true);
			} else if (child.attr('id') != 'content') {
				totalHeight += parseInt(child.css('margin-bottom')) + parseInt(child.css('margin-top'));
			}
		});
		$('#colleft').css({'height':px(size)});
		$('#colright').css({'height':px(size)});
		$('#news').css({'height':px(size)});
		$('#colright').css({'width':px(width - 20)});
		//$('#content').jScrollPaneRemove();
		$('#content').css({'height':px(size - totalHeight)});
		if ($('#dynamicdata')) {
			if (myDataTable != undefined) {
				myDataTable.set('height', ($('#content').height() - 20) + "px");
			}
		}
	
		//$('#content').css({'width':px(width - 20)});
		//$('#content').jScrollPane();
	}
	if (parseInt($('#nl-leftnav').height()) > parseInt($('#colleft').height())) {
		$('#colleft').css({'overflow-y':'scroll'});
	} else {
		$('#colleft').css({'overflow-y':'hidden'});
	}
}
	
	
function getAllowedWidth() {
	var width = $.viewportWidth() - getProperty('padding-left') - getProperty('padding-right') - getProperty('width', '#colleft') - 65;
	if ($('#news').length != 0) {
		width -= $('#news').css('width');
	}
	return width;
}

function getAllowedHeight(exclude) {
	var footerSize = $('#footerText').position().top + parseInt($('#footerText').height()) + parseInt($('#footerText').css('padding-top')) + getProperty('padding-bottom', '#footerText') - $('#footer').position().top;
	var start = $('#colright').position().top;
	var size = $.viewportHeight() - start - footerSize - getProperty('padding-top') - getProperty('padding-bottom') - 20;
	var totalHeight = 0;
	if (exclude != undefined) {
		$('#colright').children().each(function() {
			var child = $(this);
			if (child.attr('id') != 'content' && child.css('display') != 'none' && child != exclude) {
				totalHeight += child.outerHeight(true);
			} else if (child.attr('id') != 'content') {
				totalHeight += parseInt(child.css('margin-bottom')) + parseInt(child.css('margin-top'));
			}
		});
		size -= totalHeight;
	}
	return size;
}


function px(size) {
	return size + "px";
}


$(window).resize(function(){
	fixColumnHeight();
	if (resizeFunctions.length) {
		for (i = 0; i < resizeFunctions.length; i++) {
			resizeFunctions[i]();
		}
	}
});


$.viewportHeight = function() {
     return self.innerHeight ||
        jQuery.boxModel && document.documentElement.clientHeight ||
        document.body.clientHeight;
};

$.viewportWidth = function() {
     return self.innerWidth ||
        jQuery.boxModel && document.documentElement.clientWidth ||
        document.body.clientWidth;
};


	var messages = new Array();
	function adminMessage(message, message_class, clear, repeat, selector) {
		if (clear == true) {
			messages = new Array();
		}
		if (repeat == null || repeat == false) {
			if (messages.indexOf(message) != -1) {
				messages.splice(messages.indexOf(message), 1);
			}
		}
		messages.unshift(message);
		if (!selector)
			selector = '#admin_message';
		var m_selector = selector + " #message";
		$(selector).removeClass();
		$(selector).addClass(message_class);
		$(selector).click(function() {
							$(this).hide();
							fixColumnHeight();
						});
		var newmessage = '';
		for (var i = 0; i < messages.length; i++)
		{
			if (i == 0)
				newmessage += '<li class="b">'+messages[i]+'</li>'; 			
			else 
				newmessage += '<li>'+messages[i]+'</li>';
		}
		$(m_selector).html(newmessage);

		if ($(selector).css('display') == "none") {
			$(selector).css({'display':'block'});
		}
		fixColumnHeight();
	}

$(function() {

    if($.browser.msie){
    $("#dt_input_ship")

        .focus(function(){
            $(this)
                .data("origWidth", $(this).css("width"))
                .css("width", "auto");
        })

        .blur(function(){
            $(this).css("width", $(this).data("origWidth"));
        });
    }

});

