// browsers detections
var sUserAgent = navigator.userAgent;
var bIsOpera = sUserAgent.indexOf("Opera") > -1;
var bIsIE = sUserAgent.indexOf("compatible") > -1 && sUserAgent.indexOf("MSIE") > -1 && !bIsOpera;
// events handler
var EventUtil = new Object;
EventUtil.addEventHandler = function (oTarget, sEventType, fnHandler) {
	if (oTarget.addEventListener) {
		oTarget.addEventListener(sEventType, fnHandler, false);
	} else if (oTarget.attachEvent) {
		oTarget.attachEvent("on" + sEventType, fnHandler);
	} else {
		oTarget["on" + sEventType] = fnHandler;
	}
};

// open new windows
function aTarget(el, name)
{
	var wnd = window.open(typeof el == 'string' ? el : el.href, typeof name != 'undefined' ? name : '', 'menubar=yes,toolbar=yes,location=yes,directories=no,status=yes,scrollbars=yes,resizable=yes');
	if (!wnd) return false;
	wnd.focus();
	return true; 
}

function aTarget_upload_picture(el, name)
{
	var wnd = window.open(typeof el == 'string' ? el : el.href, typeof name != 'undefined' ? name : '', 'width=200,height=200,menubar=no,toolbar=no,location=no,directories=no,status=yes,scrollbars=yes,resizable=yes');
	if (!wnd) return false;
	wnd.focus();
	return true; 
}

function aTarget_screenshots(el, name)
{
	var wnd = window.open(typeof el == 'string' ? el : el.href, typeof name != 'undefined' ? name : '', 'width=675,height=600,menubar=no,toolbar=no,location=no,directories=no,status=yes,scrollbars=yes,resizable=yes');
	if (!wnd) return false;
	wnd.focus();
	return true; 
}

/******************************************************
author: DAroo, darooo7@interia.pl
do not remove this comment! (nie usuwać tego komentarza!)
******************************************************/
/*
Script sorts table by <th/> tag; table HAS TO contain tags <thead/> and <tbody/>; <th/> tags HAS TO contain <a/> tags

in the configuration you have to add following properties:
[ "id_of_the_TH_tag", ordered_number, "type_of_the_data_in_the_column" ]

available types of data:
	- "float" --> for all numbers
	- "date" --> for dates
	- null --> for text
*/

//configuration
var aTHs = [
//sites manager
["sort_id", 0, "float"], 
["sort_author", 1], 
["sort_created", 2, "date"], 
["sort_title", 3],
//my articles manager
["sort_created_2", 1, "date"],
["sort_title_2", 2]
];
//end of configuration

function convert(sValue, sDataType){
	switch(sDataType){
	case "float":
		return parseFloat(sValue);
	case "date":
		return new Date(Date.parse(sValue));
	default:
		sValue.toString();
		return sValue.toLowerCase();
	}
}

function generateCompareTRs(iCol, sDataType){

	return function compareTRs(oTR1, oTR2){
		var vValue1 = convert(oTR1.cells[iCol].firstChild.nodeValue || oTR1.cells[iCol].firstChild.firstChild.nodeValue, sDataType);
		var vValue2 = convert(oTR2.cells[iCol].firstChild.nodeValue || oTR2.cells[iCol].firstChild.firstChild.nodeValue, sDataType);
		if(vValue1 < vValue2){
			return -1;
		} else if(vValue1 > vValue2){
			return 1;
		} else {
			return 0;
		}
	};
}

function sortTable(oTable, iCol, sDataType){
	var oTbody = oTable.tBodies[0];
	var colDataRows = oTbody.rows;
	var aTRs = new Array;
	
	for(var i = 0; i < colDataRows.length; i++){
		aTRs[i] = colDataRows[i];
	}
	
	if(oTable.sortCol == iCol){
		aTRs.reverse();
	} else {
		aTRs.sort(generateCompareTRs(iCol, sDataType));
	}
	
	var oFragment = document.createDocumentFragment();
	for(var i = 0; i < aTRs.length; i++){
		oFragment.appendChild(aTRs[i]);
	}
	
	oTbody.appendChild(oFragment);
	oTable.sortCol = iCol;
}

EventUtil.addEventHandler(window, "load", loadTableSorting);

function loadTableSorting(){
	for(var i = 0; i < aTHs.length; i++){
		var oTH = document.getElementById(aTHs[i][0]);
		if(oTH){
			oTH.firstChild.onclick = function tableSorting(){
				var sTHid = this.parentNode.getAttribute("id");
				for(var j = 0; j < aTHs.length; j++){
					if(sTHid == aTHs[j][0]) break;
				}
				var oTable = this.parentNode.parentNode.parentNode.parentNode;
				sortTable(oTable, aTHs[j][1], aTHs[j][2]);
			}
		}
	}
}

/******************************************************
author: DAroo, darooo7@interia.pl
do not remove this comment! (nie usuwać tego komentarza!)
******************************************************/

// show alert when any of the form fields is empty
sAlert = "You didn't type all necessary informations"; // alert

EventUtil.addEventHandler(window, "load", pageLoaded);

function pageLoaded() {
	for(var i=0; document.forms[i]; i++){
		document.forms[i].onsubmit = function() {
			for(var j=0; this.elements[j]; j++){
				if(this.elements[j].value.length < 1){
					if(bIsIE){
						oEvent = window.event;
						oEvent.returnValue = false;
					} else {
						var oEvent = arguments[0];
						oEvent.preventDefault();
					}
					alert(sAlert);	
					break;
				}				
			}			
		}
	}	
};