// JavaScript Document
var global_windowContainer = '';
var global_images = Array();
var global_imagesAllPreloaded = false;
var global_imagesDisplayer = 0;
var global_activeDisplayer = 'gallery-display';
function initialize() {
	global_windowContainer = document.getElementById('menu-container-sample').innerHTML;
	createMenu('Start Menu', '');
	createWindow('logo', false, 'A project by', '<table border="0" cellspacing="0" cellpadding="5"><tr><td><a href="http://www.kidsinaction.gr" target="_blank"><img src="images/logo_kids_in_action.png" width="120" height="120" /></a></td></tr><tr><td>&copy; 2011 Kids in Action</td></tr><tr><td><div style="float:left"><a href="http://www.facebook.com/creativity.gr" target="_blank"><img border="0" src="si/facebook.png"/></a></div><div style="float:left"><a href="http://twitter.com/#!/Creativity_Fest" target="_blank"><img src="si/twitter.png" border="0" /></a></div><div style="float:left"><a href="http://www.youtube.com/user/NGOKidsInAction" target="_blank"><img border="0" src="si/youtube.png"/></a></div></td></tr></table>');
	galleryImagePreloader();
	changeBackgroundImage();
	setTimeout("displayFirstPageEntries();", 500);
}
function displayFirstPageEntries() {
	global_fp_entriesCounter = 0;
	while (global_fp_entriesCounter < global_fp_entries.length) {
		dfpe_functionCall = createMenu('' + global_fp_entries[global_fp_entriesCounter][1] + '', global_fp_entries[global_fp_entriesCounter][0]);
		dfpe_functionCall = viewContent(global_fp_entries[global_fp_entriesCounter][0]);
		global_fp_entriesCounter++;
	}
	
	return 0;
}
function viewContent(vc_contentId) {
	new Ajax.Request('pw.create-content.php?id=' + vc_contentId + '&language=' + global_language, {
		method: 'get',
		onSuccess: function(vc_xml) {
			vc_title = vc_xml.responseXML.documentElement.getElementsByTagName('title')[0].childNodes[0].nodeValue;
			vc_content = vc_xml.responseXML.documentElement.getElementsByTagName('content')[0].childNodes[0].nodeValue;
			if (vc_content != '' && vc_content != '*' && vc_content != '<p>*</p>') {
				vc_contentHTML = '';
				vc_contentHTML += '<div id="texter_' + vc_contentId + '" class="contentClass">';
				vc_contentHTML += vc_content;
				vc_files = vc_xml.responseXML.documentElement.getElementsByTagName('files')[0];
				vc_filesCounter = 0;
				if (vc_files.childNodes.length > 0) {
					vc_contentHTML += '<div class="allRelatedFiles">';
					vc_contentHTML += '<div class="relatedFiles">&nbsp;Related Files</div>'
					vc_imagesFound = false;
					while (vc_filesCounter < vc_files.childNodes.length) {
						vc_file = vc_files.childNodes[vc_filesCounter];
						vc_filePath = vc_file.getAttribute('path');
						vc_fileExtension = vc_filePath.split('.')[(vc_filePath.split('.').length - 1)];
						vc_fileDate = vc_file.getAttribute('date');
						vc_fileDescription = vc_file.childNodes[0].nodeValue;
						if (vc_fileExtension != 'jpg' && vc_fileExtension != 'png' && vc_fileExtension != 'gif') {
							//if(vc_fileExtension == 'mov'){
							//}else{
							vc_contentHTML += '<div onclick="document.location.href=\'admin/files/' + vc_filePath + '\'" class="filesContainer">';
							vc_fileType = vc_filePath.split('.')[vc_filePath.split('.').length - 1];
							vc_contentHTML += '<div class="fileType" title="File type: ' + vc_fileType + '" alt="File type: ' + vc_fileType + '">' + vc_filePath + '</div>';
							//vc_contentHTML += '<div class="fileDate">'+vc_fileDate+'</div>';
							vc_contentHTML += '<div class="fileDescription" title="File type: ' + vc_fileType + '" alt="File type: ' + vc_fileType + '">' + vc_fileDescription + '</div>';
							vc_contentHTML += '</div>';
							//}
						}
						else {
							if (vc_imagesFound == true) {
								vc_contentHTML += '<div style="display:none;"><a class="lightview" title="' + vc_fileDescription + '" rel="gallery[creativity]" href="admin/files/' + vc_filePath + '" >image</a></div>'
							}
							else {
								vc_imagesFound = true;
								vc_contentHTML += '<div class="filesContainer" style="cursor:default !important;">';
								vc_contentHTML += '<div class="fileType"><a class="lightview" title="' + vc_fileDescription + '" rel="gallery[creativity]" href="admin/files/' + vc_filePath + '">Photo Gallery</a></div>';
								vc_contentHTML += '</div>';
							}
						}
						vc_filesCounter++;
					}
					vc_contentHTML += '</div>';
				}
				vc_contentHTML += '</div>';
				vc_functionCall = createWindow('content_' + vc_contentId, true, vc_title, vc_contentHTML);
				Lightview.updateViews();
				CSBfleXcroll('texter_' + vc_contentId);
			}
		},
		onFailure: function() {
			alert('Something went wrong...');
		}
	});
}
function showGallery(sg_path) {
	Lightview.show({
		href: sg_path,
		rel: 'gallery[creativity]'
	});
}
function createMenu(cm_title, cm_parent) {
	new Ajax.Request('pw.create-menu.php?parent=' + cm_parent + '&language=' + global_language, {
		method: 'get',
		onSuccess: function(cm_xml) {
			if (cm_xml.responseXML.documentElement.childNodes.length > 0) {
				cm_canClose = true;
				if (cm_parent == '') {
					cm_parent = 'NULL';
					cm_canClose = false;
				}
				cm_html = '';
				cm_menus = cm_xml.responseXML.documentElement;
				cm_menusCounter = 0;
				while (cm_menusCounter < cm_menus.childNodes.length) {
					cm_menuId = cm_menus.childNodes[cm_menusCounter].getAttribute('id');
					cm_menuTitle = cm_menus.childNodes[cm_menusCounter].childNodes[0].nodeValue;
					cm_html += '<div onclick="createMenu(\'' + cm_menuTitle + '\',\'' + cm_menuId + '\');viewContent(' + cm_menuId + ');" onmouseover="menuHover(this,true);" onmouseout="menuHover(this,false);" id="window_button_' + cm_menuId + '" class="menuClass">' + cm_menuTitle + '</div>';
					cm_menusCounter++;
				}
				createWindow('menu_' + cm_parent, cm_canClose, cm_title, cm_html);
			}
		},
		onFailure: function() {
			alert('Something went wrong...');
		}
	});
}
function menuHover(mh_element, mh_mode) {
	if (mh_mode == true) {
		//$(mh_element.id).style.backgroundColor = '#3781b1';
		$(mh_element.id).style.color = '#990000';
	}
	else {
		//$(mh_element.id).style.backgroundColor = '#ffffff';
		$(mh_element.id).style.color = '#05436b';
	}
}
function galleryImagePreloader() {
	if (global_imagesAllPreloaded == false) {
		if (global_images.length > 0) {
			global_imagesCounter = 0;
			global_imagesAllPreloaded = true;
			while (global_imagesCounter < global_images.length) {
				if (global_images[global_imagesCounter][1] == false) {
					gip_image = new Image();
					gip_image.src = global_images[global_imagesCounter][0];
					global_images[global_imagesCounter][1] = true;
					global_imagesAllPreloaded = false;
				}
				global_imagesCounter++;
			}
		}
		setTimeout('galleryImagePreloader();', 100);
	}
	else {
		
		//alert('All images were preloaded!');
		return 0;
	}
}
function changeBackgroundImage() {
	if (global_images.length > 0) {
		if (global_images[global_imagesDisplayer][1] == true) {
			if (global_activeDisplayer == 'gallery-display') {
				document.getElementById('gallery-display').style.backgroundImage = 'url(' + global_images[global_imagesDisplayer][0] + ')';
				global_activeDisplayer = 'gallery-display-sec';
				$('gallery-display-sec').fade({
					duration: 5.0
				});
				$('gallery-display').appear({
					duration: 5.0
				});
			}
			else {
				document.getElementById('gallery-display-sec').style.backgroundImage = 'url(' + global_images[global_imagesDisplayer][0] + ')';
				global_activeDisplayer = 'gallery-display';
				$('gallery-display-sec').appear({
					duration: 5.0
				});
				$('gallery-display').fade({
					duration: 5.0
				});
			}
			global_imagesDisplayer++;
			if (global_imagesDisplayer > global_images.length - 1) {
				global_imagesDisplayer = 0;
			}
			setTimeout('changeBackgroundImage()', 20000);
		}
	}
}
function createWindow(cw_id, cw_canClose, cw_title, cw_innerHTML) {
	if (!(document.getElementById('window_' + cw_id))) {
		/* Remove open windows Hack */
		var cw_divs = document.getElementsByTagName('div');
		var cw_divsCounter = 0;
		while (cw_divsCounter < cw_divs.length) {
			if (cw_divs[cw_divsCounter].id.split('content_').length == 2) {
				if (cw_id.split('content_').length == 2) {
					document.getElementById('content-container').removeChild(cw_divs[cw_divsCounter]);
				}
			}
			cw_divsCounter++;
		}
		/* Remove open windows Hack End */
		cw_windowContainer = document.createElement('div');
		cw_windowContainerId = 'window_' + cw_id;
		cw_windowContainer.id = cw_windowContainerId;
		cw_windowContainer.style.position = 'absolute';
		cw_windowContainer.style.top = '40px';
		cw_windowContainer.style.left = '40px';
		cw_windowContainer.style.zIndex = 100;
		cw_windowContainer.onmouseout = function() {
			checkLocation(this);
		};
		cw_windowContainer.onclick = function() {
			setFocus(this);
		}
		cw_windowContainer.onmousedown = function() {
			setFocus(this);
		}
		cw_windowContainer.setAttribute('iswindow', 'true');
		cw_newInnerHTML = global_windowContainer;
		/* change drag element */
		cw_newInnerHTML = cw_newInnerHTML.replace(/menu-container-sample/g, cw_windowContainerId);
		cw_windowContainer.innerHTML = cw_newInnerHTML;
		document.getElementById('content-container').appendChild(cw_windowContainer);
		/* change title */
		document.getElementById('windowTitle_' + cw_windowContainerId).innerHTML = cw_title;
		/*change content */
		document.getElementById('windowContents_' + cw_windowContainerId).innerHTML = cw_innerHTML;
		if (cw_canClose != true) {
			document.getElementById('closeHandler_' + cw_windowContainerId).style.display = 'none';
		}
		/* menu window settings */
		if (cw_windowContainerId.split('menu_').length >= 2) {
			cw_checkId = cw_windowContainerId.replace(/menu/g, 'button');
			if ($(cw_checkId)) {
				cw_newTop = $(cw_checkId).offsetTop;
				cw_newLeft = $(cw_checkId).offsetLeft + $(cw_checkId).offsetWidth;
				$(cw_windowContainerId).style.top = cw_newTop;
				$(cw_windowContainerId).style.left = cw_newLeft;
			}
		}
		/* content window settings */
		if (cw_windowContainerId.split('content_').length >= 2) {
			//alert('content');
			cw_checkId = cw_windowContainerId.replace(/content/g, 'menu');
			if ($(cw_checkId)) {
				$(cw_checkId).style.top = $('window_menu_NULL').offsetTop + 'px';
				$(cw_checkId).style.left = $('window_menu_NULL').offsetLeft + $('window_menu_NULL').offsetWidth - 10 + 'px';
				cw_newTop = $(cw_checkId).offsetTop;
				cw_newLeft = $(cw_checkId).offsetLeft + $(cw_checkId).offsetWidth;
				$(cw_windowContainerId).style.top = cw_newTop + 'px';
				$(cw_windowContainerId).style.left = cw_newLeft - 10 + 'px';
			}
			else {
				cw_checkId = cw_windowContainerId.replace(/content/g, 'button');
				if ($(cw_checkId)) {
					//window
					cw_newTop = $(cw_checkId).offsetTop;
					cw_newLeft = $(cw_checkId).offsetLeft + $(cw_checkId).offsetWidth;
					$(cw_windowContainerId).style.top = $('window_menu_NULL').offsetTop + 'px';
					$(cw_windowContainerId).style.left = $('window_menu_NULL').offsetLeft + $('window_menu_NULL').offsetWidth - 10 + 'px';
				}
				else {
					$(cw_windowContainerId).style.top = $('window_menu_NULL').offsetTop + 'px';
					$(cw_windowContainerId).style.left = $('window_menu_NULL').offsetLeft + $('window_menu_NULL').offsetWidth - 10 + 'px';
				}
			}
			cw_texterId = 'texter_' + cw_id.split('_')[cw_id.split('_').length - 1];
			//if($(cw_texterId).offsetWidth > 700){
			//alert('Testing javascript');
			$(cw_texterId).style.width = '710px';
			$(cw_texterId).style.height = '504px';
			//}
			/*
			 if($(cw_texterId).offsetHeight > 500){
			 $(cw_texterId).style.height = '500px';
			 }
			 */
		}
		/* main menu cannot be draggable */
		if (cw_windowContainerId == 'window_menu_NULL') {
			$('windowHandler_window_menu_NULL').onmousedown = function() {
			};
			$(cw_windowContainerId).onclick = function() {
			};
			$(cw_windowContainerId).onmousedown = function() {
			};
			$('windowHandler_window_menu_NULL').style.cursor = 'default';
		}
		if (cw_id == 'logo') {
			cw_totalWidth = $('content-container').offsetWidth;
			cw_totalHeight = $('content-container').offsetHeight;
			$(cw_windowContainerId).style.left = cw_totalWidth - 30 - $(cw_windowContainerId).offsetWidth + 'px';
			$(cw_windowContainerId).style.top = cw_totalHeight - 30 - $(cw_windowContainerId).offsetHeight + 'px';
			$('windowHandler_window_logo').onmousedown = function() {
			};
			$(cw_windowContainerId).onclick = function() {
			};
			$(cw_windowContainerId).onmousedown = function() {
			};
			$('windowHandler_window_logo').style.cursor = 'default';
		}
		setFocus($(cw_windowContainerId));
	}
	
	return 0;
}
function setFocus(sf_element) {
	sf_allDivs = document.getElementsByTagName('div');
	sf_allDivsCounter = 0;
	sf_maxIndex = 0;
	while (sf_allDivsCounter < sf_allDivs.length) {
		if (sf_allDivs[sf_allDivsCounter].id.split('window_').length >= 2) {
			if (sf_allDivs[sf_allDivsCounter].style.zIndex > sf_maxIndex) {
				sf_maxIndex = sf_allDivs[sf_allDivsCounter].style.zIndex;
			}
		}
		sf_allDivsCounter++;
	}
	sf_element.style.zIndex = ++sf_maxIndex;
	
	return 0;
}
function closeWindow(cw_id) {
	if (document.getElementById(cw_id)) {
		document.getElementById('content-container').removeChild(document.getElementById(cw_id));
	}
	
	return 0;
}
function resizeContentContainer() {
	cgib_height = document.getElementById('screen-tester').offsetHeight;
	cgib_width = document.getElementById('screen-tester').offsetWidth;
	document.getElementById('content-container').style.width = cgib_width + 'px';
	document.getElementById('content-container').style.height = cgib_height + 'px';
	
	return 0;
}
function checkLocation(cl_element) {
	cl_maxTop = document.getElementById('content-container').offsetHeight - 10;
	if (cl_element.offsetTop < 0) {
		cl_element.style.top = '0px';
	}
	if (cl_element.offsetTop > cl_maxTop) {
		cl_element.style.top = (cl_maxTop - 30) + 'px';
	}
}
/* window events */
window.onload = function() {
	resizeContentContainer();
	initialize();
};
window.onresize = function() {
	resizeContentContainer();
};
/* drag functions */
//*****************************************************************************
// Do not remove this notice.
//
// Copyright 2001 by Mike Hall.
// See http://www.brainjar.com for terms of use.
//*****************************************************************************
// Determine browser and version.
function Browser() {
	var ua, s, i;
	this.isIE = false;
	this.isNS = false;
	this.version = null;
	ua = navigator.userAgent;
	s = "MSIE";
	if ((i = ua.indexOf(s)) >= 0) {
		this.isIE = true;
		this.version = parseFloat(ua.substr(i + s.length));
		
		return;
	}
	s = "Netscape6/";
	if ((i = ua.indexOf(s)) >= 0) {
		this.isNS = true;
		this.version = parseFloat(ua.substr(i + s.length));
		
		return;
	}
	// Treat any other "Gecko" browser as NS 6.1.
	s = "Gecko";
	if ((i = ua.indexOf(s)) >= 0) {
		this.isNS = true;
		this.version = 6.1;
		
		return;
	}
}
var browser = new Browser();
// Global object to hold drag information.
var dragObj = new Object();
dragObj.zIndex = 0;
function dragStart(event, id) {
	var el;
	var x, y;
	// If an element id was given, find it. Otherwise use the element being
	// clicked on.
	if (id) 
		dragObj.elNode = document.getElementById(id);
	else {
		if (browser.isIE) 
			dragObj.elNode = window.event.srcElement;
		if (browser.isNS) 
			dragObj.elNode = event.target;
		// If this is a text node, use its parent element.
		if (dragObj.elNode.nodeType == 3) 
			dragObj.elNode = dragObj.elNode.parentNode;
	}
	// Get cursor position with respect to the page.
	if (browser.isIE) {
		x = window.event.clientX + document.documentElement.scrollLeft +
		document.body.scrollLeft;
		y = window.event.clientY + document.documentElement.scrollTop +
		document.body.scrollTop;
	}
	if (browser.isNS) {
		x = event.clientX + window.scrollX;
		y = event.clientY + window.scrollY;
	}
	// Save starting positions of cursor and element.
	dragObj.cursorStartX = x;
	dragObj.cursorStartY = y;
	dragObj.elStartLeft = parseInt(dragObj.elNode.style.left, 10);
	dragObj.elStartTop = parseInt(dragObj.elNode.style.top, 10);
	if (isNaN(dragObj.elStartLeft)) 
		dragObj.elStartLeft = 0;
	if (isNaN(dragObj.elStartTop)) 
		dragObj.elStartTop = 0;
	// Update element's z-index.
	//dragObj.elNode.style.zIndex = ++dragObj.zIndex;
	// Capture mousemove and mouseup events on the page.
	if (browser.isIE) {
		document.attachEvent("onmousemove", dragGo);
		document.attachEvent("onmouseup", dragStop);
		window.event.cancelBubble = true;
		window.event.returnValue = false;
	}
	if (browser.isNS) {
		document.addEventListener("mousemove", dragGo, true);
		document.addEventListener("mouseup", dragStop, true);
		event.preventDefault();
	}
}
function dragGo(event) {
	var x, y;
	// Get cursor position with respect to the page.
	if (browser.isIE) {
		x = window.event.clientX + document.documentElement.scrollLeft +
		document.body.scrollLeft;
		y = window.event.clientY + document.documentElement.scrollTop +
		document.body.scrollTop;
	}
	if (browser.isNS) {
		x = event.clientX + window.scrollX;
		y = event.clientY + window.scrollY;
	}
	// Move drag element by the same amount the cursor has moved.
	dragObj.elNode.style.left = (dragObj.elStartLeft + x - dragObj.cursorStartX) + "px";
	dragObj.elNode.style.top = (dragObj.elStartTop + y - dragObj.cursorStartY) + "px";
	if (browser.isIE) {
		window.event.cancelBubble = true;
		window.event.returnValue = false;
	}
	if (browser.isNS) 
		event.preventDefault();
}
function dragStop(event) {
	// Stop capturing mousemove and mouseup events.
	if (browser.isIE) {
		document.detachEvent("onmousemove", dragGo);
		document.detachEvent("onmouseup", dragStop);
	}
	if (browser.isNS) {
		document.removeEventListener("mousemove", dragGo, true);
		document.removeEventListener("mouseup", dragStop, true);
	}
}

