﻿var ud = 'undefined';
var opera = Boolean(window["opera"]);
var ie = (navigator.appName.indexOf("Microsoft") != -1) && !opera;
var sfx = false;
var openedPopup = null;
var oPgs = false;

var ppStyles = {
	'faq':['741','bg_faq.png'],
	'map':['403','bg_map.png'],
	'formula':['741','bg_formula.png'],
	'state':['741','bg_state.png'],
	'accred':['741','bg_accred.png'],
	'tickets':['741','bg_tickets.png'],
	'contact':['344','bg_contact.png'],
	'site':['468','bg_map.png'],
	'partner':['344','bg_partner.png'],
	'judges':['404','bg_judges.png'],
	'articles':['741','bg_articles.png'],
	'mpartner':['344','bg_mpartner.png'],
	'transp':['510','bg_transp.png'],
	'take':['741','bg_take.png']
};

var ppContent = {};


xy = function (x,y) {
	this.x = x || 0;
	this.y = y || 0;
	return this;
}

function mju_play_track(num) {
	//if (num <= 0) return false;
	//var mc = ie ? window.mju : window.document.mju;
	var doc = document.getElementById("sfx_frame");
	alert(doc)
	//var mc = document.getElementById("sfx_frame");
	//mc.SetVariable("play_track",num);
}

function mju_play_file(chars) {
	if (!chars.length) return false;
	var mc = ie ? window.mju : window.document.mju;
	mc.SetVariable("play_file",chars);
}

function mju_do(cmd) {
	if (!cmd.length) return false;
	var e = document.getElementById("sfx_frame").contentWindow.document.getElementById("mju");
	//alert(e)
	//var mc = ie ? window.mju : window.document.mju;
	e.SetVariable("do_" + cmd, " ");
	//var x
}

function sfx_toggle() {
	if (!sfx) {
		document.getElementById("sfx_off").style.display = "none";
		document.getElementById("sfx_on").style.display = "";
		//el.className = "png";
		mju_do("play");
	} else {
		document.getElementById("sfx_on").style.display = "none";
		document.getElementById("sfx_off").style.display = "";
		//el.src = "img/sfx_on.png";
		mju_do("pause");
		//el.className = "png";
	}
	sfx = !sfx;
}

function popup(id, noFade) {
	if (openedPopup == id) return;
	var noFade = typeof(noFade) == ud ? false : true;
	var e = document.getElementById('popup');
	if (e && typeof(e) != ud) {
		if (typeof(ppContent[id]) == ud) {
			$(e).load('tpl/frag/pp_' + id + '.php', null, function(){
				ppContent[id] = $(e).html();
				popupIn(e, noFade, id);
			});
		} else {
			$(e).html(ppContent[id]);
			popupIn(e, noFade, id);
		}
	}
	return false;
}

function fadeOutMain(noFade) {
	var noFade = typeof(noFade) == ud ? false : true;
	if($.browser.msie2) {
		$('#opaco').height($(document).height()).toggleClass('hidden');
	}
	else {
		if (noFade) $('#opaco').height($(document).height()).toggleClass('hidden').fadeTo('fast', 0.7);
		else $('#opaco').height($(document).height()).toggleClass('hidden').fadeTo('normal', 0.7);
	}
}


function popupIn(e, noFade, id) {
	$(e).css('height', ppStyles[id][0] + 'px');
	$(e).css('background', 'transparent url(img/popup/' + ppStyles[id][1] + ') repeat 0px 0px');
	if (openedPopup == null) {
		if (!noFade) {
			$(e).fadeIn('normal', function(){openedPopup = id;});
			//fadeOutMain();
		} else {
			$(e).show();
			openedPopup = id;
			//fadeOutMain(true);
		}
	} else {
		openedPopup = id;
	}
}

function closePopup(noFade){
	var noFade = typeof(noFade) == ud ? false : true;
	if (!noFade) {
		$('#popup').fadeOut('normal');
		if(ie) {
			//$('#opaco').toggleClass('hidden').removeAttr('style');
		}
		else {
			//$('#opaco').fadeTo('normal', 0, function(){$(this).toggleClass('hidden').removeAttr('style')});
		}
	} else {
		$('#popup').hide();
		//$('#opaco').toggleClass('hidden').removeAttr('style');
	}
	openedPopup = null;
	return false;
}

function getScrollPosition () {
	var pos = new xy();
	if (window.scrollX && window.scrollY) {
		pos.x = window.scrollX;
		pos.y = window.scrollY;
	} else {
		var docBody = document.body;
		var pLeft  = (docBody.parentNode.scrollLeft) ? docBody.parentNode.scrollLeft : 0;
		var pTop = (docBody.parentNode.scrollTop) ? docBody.parentNode.scrollTop : 0;
		pos.x = Math.max(docBody.scrollLeft,pLeft );
		pos.y = Math.max(docBody.scrollTop,pTop);
	}
	return pos;
}

function updateProgressPosition(){
		oPgs.css("top", getScrollPosition().y);
		window.status = oPgs.css("top")
}

// jQuery plugin for firebug console logging:
// Examples:
//  $(root).find('li.source').log("sources to uncheck").removeAttr("checked");
//  $().log($(this));
jQuery.fn.log = function (msg) {
	console.log("%s: %o", msg, this);
	return this;
};

$(document).ready(function(){
	$('body').append('<div id="progress" style="display:none;z-index:999;top:0;left:0;width:100%;height:20px;background:transparent url(img/progress.gif) repeat 0px 0px;vertical-align:middle;text-align:center;font-size:10pt;line-height:10pt;font-weight:bold;color:#000"></div>');

	if(ie) { // IE SUX! everyone knows it
		// menu hover fix for IE
		$('#menu > DIV > DIV').hover(function(){$(this).addClass("over")}, function(){$(this).removeClass("over")});
		// manually position progress bar
	}

	oPgs = $("#progress");
	oPgs.css("position", ie ? "absolute" : "fixed");
	oPgs.ajaxStart(function(){
		if(ie){
			updateProgressPosition();
			$(window).scroll(function(){if(oPgs && oPgs.css("display") != "none")updateProgressPosition()});
		}
		oPgs.show();}
	).ajaxError(function(a,b,c){oPgs.hide()}).ajaxStop(function(){if(ie)$(window).scroll(null);oPgs.hide()})
	//$('#progress').ajaxStart(function(){$('#progress').fadeIn('slow')}).ajaxError(function(a,b,c){$('#progress').fadeOut('slow')}).ajaxStop(function(){$('#progress').fadeOut('slow')})

	if (!opera) {
		// get rid of dotted border around clicked links
		$('a').click(function(){this.blur()});
	}
	// automatically open #popup
	var hash = document.location.hash;
	if (hash && hash.length > 1) {
		var id = hash.substr(1, hash.length - 1);
		popup(id, true);
	}
});
