/************************************************************************************************************
Ajax poller
Copyright (C) 2006  DTHMLGoodies.com, Alf Magne Kalleland

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

Dhtmlgoodies.com., hereby disclaims all copyright interest in this script
written by Alf Magne Kalleland.

Alf Magne Kalleland, 2006
Owner of DHTMLgoodies.com
	
************************************************************************************************************/	

$(document).ready(function(){

// 	$(function() {
// 		$('a[rel=lightbox]').lightBox();
// 		$('a[rel=lightbox[gallery]]').lightBox();
// 	});

	$(function() {
		if($('div#raditem').length > 0){
			initAdresar();
		}
	});
;});

function initAdresar(){

	


}



function frontBanner(){
	var fseen = readCookie('fbp');
	if(!fseen) {

		if(document.getElementById("shb")){
			hideSelectBoxes();
			hideFlash();

			var fb = document.getElementById('fbanner');
			fb.style.display = 'block';

			var arrayPageSize = getPageSize();
			fb.style.width = arrayPageSize[0]+"px";
			fb.style.height = arrayPageSize[1]+"px";
			//alert(arrayPageSize[1]);

			var fO = fb.getElementsByTagName("object");
			for (i = 0; i < fO.length; i++) {
				fO[i].style.visibility = "visible";
			}
			var fE = fb.getElementsByTagName("embed");
			for (i = 0; i < fE.length; i++) {
				fE[i].style.visibility = "visible";
			}
			createCookie('fbp',1);
		}
	}
}

function hideFrontBanner(){
	showSelectBoxes();
	showFlash();
	document.getElementById('fbanner').style.display = 'none';
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function Get_Cookie( name ) {
	
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) &&
	( name != document.cookie.substring( 0, name.length ) ) )
	{
	return null;
	}
	if ( start == -1 ) return null;
	var end = document.cookie.indexOf( ";", len );
	if ( end == -1 ) end = document.cookie.length;
	return unescape( document.cookie.substring( len, end ) );
}


function eraseCookie(name) {
	createCookie(name,"",-1);
}

function showBanner(pos,ban){
	var seen = readCookie('bp' + pos);
	if(seen) seen = seen + '~' + ban;
	else seen = ban;
	createCookie('bp' + pos,seen,1);
}
function resetBanner(pos){
	eraseCookie('bp' + pos);
}




function getPageScroll(){

	var xScroll, yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
		xScroll = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
		xScroll = document.documentElement.scrollLeft;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
		xScroll = document.body.scrollLeft;	
	}

	arrayPageScroll = new Array(xScroll,yScroll) 
	return arrayPageScroll;
}




function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	
//	console.log(self.innerWidth);
//	console.log(document.documentElement.clientWidth);

	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

//	console.log("xScroll " + xScroll)
//	console.log("windowWidth " + windowWidth)

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
//	console.log("pageWidth " + pageWidth)

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}



















/* menu */

var ajaxMenuRequest = "/app/menu.php";
var ajaxMenu = new Array();

var timeout	= 500;
var closetimer	= 0;
var actmenuitem	= 0;
var initmenu = 1;

function initMenu(){
	initmenu = 1;
}

function menuOn(node){

	if(initmenu != 1) return false;

	mcancelclosetime();
	var id = node.id.replace(/[^0-9]/g, '');

	if(actmenuitem) {
		actmenuitem.parentNode.firstChild.className = actmenuitem.parentNode.firstChild.className.replace(/menuactive/g, '');
		actmenuitem.style.visibility = 'hidden';
	}
	
	if(!ajaxMenu[id]) {
		initmenu = 0;
		var params = "?id=" + id;
		ajaxMenu[id] = new sack();
		ajaxMenu[id].requestFile = ajaxMenuRequest + params;
		ajaxMenu[id].onCompletion = function() { createMenu(id) };
		ajaxMenu[id].runAJAX();
	}
	else {
		//actmenuitem = document.getElementById('ajaxmenudiv' + id);
		if(ajaxMenu[id].response.length > 2) {
			actmenuitem = document.getElementById('ajaxmenudiv' + id);
			actmenuitem.style.visibility = 'visible';
		}
	}
}

function menuOff(){
	//alert(actmenuitem)
	//actmenuitem.parentNode.firstChild.className = actmenuitem.parentNode.firstChild.className.replace(/menuactive/g, '');
	if(actmenuitem) {
		actmenuitem.parentNode.firstChild.className = actmenuitem.parentNode.firstChild.className.replace(/menuactive/g, '');
		actmenuitem.style.visibility = 'hidden';
	}
}

function createMenu(id){
	var actmenu = document.getElementById("ajaxmenu"+id);
	actmenu.className += ' menuactive';

	menuPart = document.createElement("div");
	menuPart.id = 'ajaxmenudiv' + id;
	menuPart.className = 'ajaxmenu';
	menuPart.onmouseover = function() { mcancelclosetime() };
	menuPart.onmouseout = function() { mclosetime() };
	
	actmenuitem = menuPart;

	if(ajaxMenu[id].response.length > 2) {
		menuPart.innerHTML = ajaxMenu[id].response;
		//actmenuitem = menuPart;
	}
	else menuPart.style.display = "none";

	actmenu.parentNode.appendChild(menuPart);
	initmenu = 1;
}

// go close timer
function mclosetime(){
	closetimer = window.setTimeout(menuOff, timeout);
}

// cancel close timer
function mcancelclosetime(){
	if(closetimer)	{
		window.clearTimeout(closetimer);
		closetimer = null;
	}
}

document.onclick = menuOff; 





var ajaxRequest = "/app/vote.php";
var poll;

function vote(node){
	var numId = node.id.replace(/[^0-9]/g, '');
	var parentDiv = node.parentNode.parentNode.parentNode.id;
	var params = "?" + numId + "/";
	poll = new sack();
	poll.requestFile = ajaxRequest + params;
	poll.onCompletion = function() { showVote(numId,parentDiv) };	// Specify function that will be executed after file has been found
	poll.runAJAX();	// Execute AJAX function
}

function showVote(id,parentDiv){
	document.getElementById(parentDiv).innerHTML = poll.response;
}

// ------------------------------------------------


function showSelectBoxes(){
	var selects = document.getElementsByTagName("select");
	for (i = 0; i != selects.length; i++) {
		selects[i].style.visibility = "visible";
	}
}

// ---------------------------------------------------

function hideSelectBoxes(){
	var selects = document.getElementsByTagName("select");
	for (i = 0; i != selects.length; i++) {
		selects[i].style.visibility = "hidden";
	}
}

// ---------------------------------------------------

function showFlash(){
	var flashObjects = document.getElementsByTagName("object");
	for (i = 0; i < flashObjects.length; i++) {
		flashObjects[i].style.visibility = "visible";
	}

	var flashEmbeds = document.getElementsByTagName("embed");
	for (i = 0; i < flashEmbeds.length; i++) {
		flashEmbeds[i].style.visibility = "visible";
	}
}

// ---------------------------------------------------

function hideFlash(){
	var flashObjects = document.getElementsByTagName("object");
	for (i = 0; i < flashObjects.length; i++) {
		flashObjects[i].style.visibility = "hidden";
	}

	var flashEmbeds = document.getElementsByTagName("embed");
	for (i = 0; i < flashEmbeds.length; i++) {
		flashEmbeds[i].style.visibility = "hidden";
	}

}

