function addEvent( obj, type, fn )
{
	if (obj.addEventListener) {
		obj.addEventListener( type, fn, false );
	} else if (obj.attachEvent) {
		obj["e"+type+fn] = fn;
		obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
		obj.attachEvent( "on"+type, obj[type+fn] );
	}
}

function removeEvent( obj, type, fn ){
	if (obj.removeEventListener){
		obj.removeEventListener( type, fn, false );
	} else if (obj.detachEvent) {
		obj.detachEvent( "on"+type, obj[type+fn] );
		obj[type+fn] = null;
		obj["e"+type+fn] = null;
	}
}


//No more IE6 background flicker
try {
	document.execCommand('BackgroundImageCache', false, true);
} catch(e) {}

function forid(id) {
	return document.getElementById(id);
}

function StartMiniGall(){
	var theImages = document.getElementsByTagName("img");
	
	for (var i=0;i<theImages.length;i++) {

		var theImage = theImages[i];
		if(theImage.className == 'pict' && theImage.id) {
			
			var theVars = theImage.id.split(".");
			var theParent = theImage.parentNode;
			
				theImage.id = theVars[0] + theVars[2] + "-" + i + "img";
				
				var theIm = document.getElementById(theImage.id);
				
				var he = $(theIm).height();
				var we = $(theIm).width();
				var elDiv = $(theIm).parent();
				
				$(elDiv).css({ height: he, width: we });
				
			var theInputPrev = document.createElement("input");
				theInputPrev.type = "hidden";
				theInputPrev.id =  theImage.id.replace("img", "") + "prev";
				theInputPrev.value = theVars[3];

			var theInputNext = document.createElement("input");
				theInputNext.type = "hidden";
				theInputNext.id = theImage.id.replace("img", "") + "next";
				theInputNext.value = 1;

			var theNavUl = document.createElement("ul");
				theNavUl.className = "minigalcontrol";
				theNavUl.id = theImage.id.replace("img", "") + "control";

			var theNavUlLiPrev = document.createElement("li");

			var theNavUlLiPrevA = document.createElement("a");
				theNavUlLiPrevA.title = "Prev";
				theNavUlLiPrevA.action_act = "prev";
				theNavUlLiPrevA.id = "control" + theImage.id.replace("img", "") + "prev";
				theNavUlLiPrevA.className = "imglistprev";
				theNavUlLiPrevA.action_field = theVars[1];
				theNavUlLiPrevA.action_id = theImage.id.replace("img", "");
				theNavUlLiPrevA.action_dataname = theVars[0];
				theNavUlLiPrevA.onclick = ListMinigal;
				theNavUlLiPrevA.onmouseover = MinigalHover;
				theNavUlLiPrevA.onmouseout = MinigalOut;
				
				theNavUlLiPrev.appendChild(theNavUlLiPrevA);

			var theNavUlLiNext = document.createElement("li");
			var theNavUlLiNextA = document.createElement("a");
				theNavUlLiNextA.title = "Next";
				theNavUlLiNextA.action_act = "next";
				theNavUlLiNextA.id = "control" + theImage.id.replace("img", "") + "next";
				theNavUlLiNextA.className = "imglistnext";
				theNavUlLiNextA.action_act = "next";
				theNavUlLiNextA.action_field = theVars[1];
				theNavUlLiNextA.action_id = theImage.id.replace("img", "");
				theNavUlLiNextA.action_dataname = theVars[0];
				theNavUlLiNextA.onclick = ListMinigal;
				theNavUlLiNextA.onmouseover = MinigalHover;
				theNavUlLiNextA.onmouseout = MinigalOut;
				
				theNavUlLiNext.appendChild(theNavUlLiNextA);
				
				theNavUl.appendChild(theNavUlLiPrev);
				theNavUl.appendChild(theNavUlLiNext);

			var theClock = document.createElement("div");
				theClock.id = theImage.id.replace("img", "") + "clock";
				theClock.className = "clock";
				theClock.style.display = "none";

				theParent.insertBefore(theClock, theImage);
				theParent.insertBefore(theNavUl, theImage);
				theParent.appendChild(theInputPrev);
				theParent.appendChild(theInputNext);
		}
	}
}

function ListMinigal(){
	var theField = this.action_field;
	var theId = this.action_id;
	var theAction = this.action_act;
	var theDataname = this.action_dataname;
	var img = forid(theId + 'img');
	var theFlag = '0';
	var theClock = forid(theId + "clock");
	
	var req = new JSHttpRequest();
		req.onreadystatechange = function(){
			if (req.readyState == 4) {
				forid(theId + "prev").value = req.responseJS.row["prev"];
				forid(theId + "next").value = req.responseJS.row["next"];
				
				$(img).fadeOut("normal", function() {
					theFlag = '1';
					$(img).attr("src", req.responseJS.row["src"]);
				});
				
				$(img).load( function() {
					if(theFlag=='1') {
						var he = $(img).height() + 12;
						var we = $(img).width() + 12;
						var elDiv = $(img).parent();
						
						elDiv.animate( { height: he, width: we }, "slow", function() {
							//$(theClock).hide("slow");
							$(img).fadeIn("normal");
							theFlag = '0';
							return false;
						});
					}
				});
			}
		}
		
		//$(theClock).show("slow");
		
		req.caching = true;
		req.open("GET", "/inc/listminigal.php", true);
		req.send({ field: theField, id: theId.replace(theDataname, "").split("-")[0], position: forid(theId + theAction).value, dataname: theDataname });
	
	//Google stat
	//urchinTracker('/gallery/' + theField + '/' + theId + '/' + theAction + '/' + theDataname);
}

function MinigalHover(){
	var Who = this.action_act;
	var WhoId = this.action_id;
	
	forid("control" + WhoId + Who).className = "imglist" + Who + "hover";
}

function MinigalOut(){
	var Who = this.action_act;
	var WhoId = this.action_id;
	
	forid("control" + WhoId + Who).className = forid("control" + WhoId + Who).className.replace("hover", "");
}

function openInNewWindow() {
	// Change "_blank" to something like "newWindow" to load all links in the same new window
    var newWindow = window.open(this.getAttribute('href'), '_blank');
    newWindow.focus();
    return false;
}

/*
Add the openInNewWindow function to the onclick event of links with a class name of "new-window"
*/
function getNewWindowLinks() {
	// Check that the browser is DOM compliant
	if (document.getElementById && document.createElement && document.appendChild) {
		// Change this to the text you want to use to alert the user that a new window will be opened
		var strNewWindowAlert = " (opens in a new window)";
		// Find all links
		var links = document.getElementsByTagName('a');
		var objWarningText;
		var strWarningText;
		var link;
		for (var i = 0; i < links.length; i++) {
			link = links[i];
			// Find all links with a class name of "non-html"
			if (/\bnewblank\b/.exec(link.className)) {
				// Create an em element containing the new window warning text and insert it after the link text
				//objWarningText = document.createElement("em");
				//strWarningText = document.createTextNode(strNewWindowAlert);
				//objWarningText.appendChild(strWarningText);
				//link.appendChild(objWarningText);
				link.onclick = openInNewWindow;
			}
		}
		objWarningText = null;
	}
}

//From Yandex
document.onkeydown = NavigateThrough;
function NavigateThrough (event) {
	if (!document.getElementById) return;

	if (window.event) event = window.event;

	if (event.ctrlKey){
		var link = null;
		var href = null;
		switch (event.keyCode ? event.keyCode : event.which ? event.which : null){
			case 0x25:
				link = document.getElementById ('nextlink');
				break;
			case 0x27:
				link = document.getElementById ('prevlink');
				break;
			case 0x26:
				link = document.getElementById ('uplink');
				break;
			case 0x28:
				link = document.getElementById ('downlink');
				break;
			case 0x24:
				href = '/';
				break;
		}

		if (link && link.href) document.location = link.href;
		if (href) document.location = href;
	}			
}

function PollActivate(){
		var fields = document.getElementsByTagName('input');
		for (var i=0; i<fields.length; i++){
			var field = fields[i];
			if((field.type == "radio" && field.className == "pollfield") ||	(field.type == "checkbox" && field.className == "pollbotton")){
				field.onclick = PollGetResult;
			}
		}
}

function PollGetResult(){
		var polldata = "";
		
		var fields = document.getElementsByTagName('input');
		var pollurl = forid('polllocation');
		var pollid = forid("id_poll");
		
		for (var i=0; i<fields.length; i++){
			var field = fields[i];
			if((field.type == "radio" && field.className == "pollfield") ||	(field.type == "checkbox" && field.className == "pollbotton")){
				if(field.checked){
					polldata += field.value + "|";
				}
			}
		}
		
		var req = new JSHttpRequest();
		req.onreadystatechange = function(){
		  if (req.readyState == 4){
					forid("polldata").innerHTML = req.responseJS.row;
			}
		}
		
		forid("polldata").innerHTML = "<p class=\"clock\">&nbsp;</p>";
				
		req.caching = true;
		req.open('GET', '/inc/poll.php', true);
		req.send({ data: polldata, url: pollurl.value, id: pollid.value });
}

/* 

	Sitemap Styler v0.1
	written by Alen Grakalic, provided by Css Globe (cssglobe.com)
	visit http://cssglobe.com/lab/sitemap_styler/
	
*/

this.sitemapstyler = function(){
	var sitemap = document.getElementById("sitemap")
	if(sitemap){
		
		this.listItem = function(li){
			if(li.getElementsByTagName("ul").length > 0){
				var ul = li.getElementsByTagName("ul")[0];
					ul.style.display = "none";
				
				var span = document.createElement("span");
				
				span.className = "collapsed";
				span.onclick = function(){
					ul.style.display = (ul.style.display == "none") ? "block" : "none";
					this.className = (ul.style.display == "none") ? "collapsed" : "expanded";
				};
				
				li.appendChild(span);
			};
		};
		
		var items = sitemap.getElementsByTagName("li");
		for(var i=0;i<items.length;i++){
			listItem(items[i]);
		};
		
	};	
};

/*
$(document).ready(function(){
	$("<div>")
	.appendTo("#container")
	.attr("id", "gotop")
	.click(function(){
		$.scrollTo("#allcontent", "slow");
	})
	.hover(
		function () {
			$(this).css("backgroundPosition", "center -22px");
		},
		function () {
			$(this).css("backgroundPosition", "center 0");
		}
	);
});
*/

$(document).ready(function() {
	$("#content .body-navigation li").hover(
		function () {
			$(this).addClass("hover");
		},
		function () {
			$(this).removeClass("hover");
		}
	);
	
	$(".webform .field").hover(
		function () {
			$(this).addClass("hover");
		},
		function () {
			$(this).removeClass("hover");
		}
	);
	
	/*
	$("*").click(function(){
		var cl = $(this).attr("class");
		var id = $(this).attr("id");
		
		
		$("#alert").remove();
		
		$("<div>")
		.attr("id", "alert")
		.prependTo("#header")
		.text("class: " + cl + "; id:" + id)
		.css({background:"#FFF", padding:"10px", position:"absolute"})
		.hide()
		.show("slow");
		
		
		return false;
	})
	*/
	
});

//forms
var Forms = new Object({
	confirmfeedbackform: function() {
		var queryString = $('#webform').formSerialize();
		var rand = Math.floor(Math.random()*(10000-1+1))+1;
		var container;
		
		$(".fieldsubmit").addClass("sloading");
		
		var req = new JSHttpRequest();
			
		req.onreadystatechange = function(){
			if (req.readyState == 4) {
				if(req.responseJS.row == "correct"){
					document.getElementById("webform").submit();
				} else {
					var field = document.getElementById(req.responseJS.field);
					var parent = $(field).parent();
					
					$(".fieldsubmit").removeClass("sloading");
						
					$("<div>")
					.addClass("inputalert")
					.css("display", "none")
					.appendTo(parent)
					.fadeIn("normal")
					.html(req.responseJS.row);
						
					$(field).addClass("confirm");
						
					$(".inputalert")
					.click(function(){
					   $(this).fadeOut("fast", function(){
							$(this).remove();
						});
					   return false;
					});
					
					$('input')
					.focus(function(){
						$('input').removeClass("confirm");
						
						$(".inputalert").fadeOut("fast", function(){
							$(this).remove();
						});
						return false;
					});
				}
			}
		}
					
		req.caching = true;
		req.open('GET', '/m/s_confirmfeedbackform.php', true);
		req.send({ query: queryString, r: rand });
		
		return false;
	}
});

//png fix
$(document).ready(function() {
	$.ifixpng("/images/system/pixel.gif");
	
	$("span.title").ifixpng();
	$(".footerinformation").ifixpng();
	$("div#logo").ifixpng();
	$("div#extra").ifixpng();
	$("div.body-navigation").ifixpng();
	$("div.news").ifixpng();
	$("div.notes").ifixpng();
});
	
addEvent(window, 'load', getNewWindowLinks);
addEvent(window, 'load', StartMiniGall);
addEvent(window, 'load', PollActivate);
addEvent(window, 'load', sitemapstyler);
//addEvent(window, 'load', enableTooltips);