var ROOT_URL = 'http://www.kudos.ro/';
var RES_URL = ROOT_URL + "resources/";
var AJAX_URL = 'ajax/';
var RES_OK = 'ok';

var bubble = 1;

function initPage() {
	
	var context = document.body.id;
	var template = document.body.className;

	var main = document.getElementById('main');
	//openMovieSwf("bg.swf", "obj", "visual", "1000", "800");
	//main.style.height = '0';
	
	$(document).ready(function() {
	  		$("a[rel=contact]").fancybox({
	  			 'zoomOpacity': true,
	  			 'zoomSpeedIn'			: 600,
				 'zoomSpeedOut'			: 500,
				 'easingIn'				: 'easeInExpo',
				 'easingOut'			: 'easeInBack',
				 'overlayShow'          : true,
				 'centerOnScroll'       : true

			}); 
	 });
	/*
	if (document.getElementById('nsf_email')) document.getElementById('nsf_email').onclick = function() {
		this.value = "";		
		this.style.color = "#ffffff";
		this.style.backgroundColor = "transparent";
		this.style.borderColor = "#ffffff";
	}
	
	
	var promo = document.getElementById('promo');
	var lis = promo.getElementsByTagName('li');
	var lis_no = lis.length;
	if (lis_no > 0) {	
		for (var i = 0; i < lis_no; i++) {
			var as = lis[i].getElementsByTagName('a');			
			if (as.length == 1) {
				as[0].onmouseover = function() {				
					var ps = this.parentNode.parentNode.getElementsByTagName('p');
					for (var j = 0; j < ps.length; j++) ps[j].style.backgroundColor = "#C30D6F";
				} 			 
				as[0].onmouseout = function() {				
					var ps = this.parentNode.parentNode.getElementsByTagName('p');
					for (var j = 0; j < ps.length; j++) ps[j].style.backgroundColor = "#4EEBF5";
				} 
			}
		}
	}

			*/
	
	Cufon.replace('.graphic');
	Cufon.replace('#navigation li a', {hover: true});
	Cufon.replace('#navigation li h1');
	
	if (context.indexOf('Index') != '-1') {	
	
		Cufon.replace('#promo li h3', {hover: true});
		
		$(document).ready(function() {
	  		$("a[rel=events]").fancybox({
	  			 'zoomOpacity': true,
	  			 'zoomSpeedIn'			: 600,
				 'zoomSpeedOut'			: 500,
				 'easingIn'				: 'easeInExpo',
				 'easingOut'			: 'easeInBack',
				 'overlayShow'          : true,
				 'centerOnScroll'       : true

			}); 
	    });
		
		var multimedia = document.getElementById('multimedia');
		var lis = multimedia.getElementsByTagName('li');
		var lis_no = lis.length;
		
		if (lis_no > 0) {		
		
			for (var i = 0; i < lis_no; i++) {				
			
				lis[i].onmouseover = function() {
					this.className += " hovered";
					var ps = this.getElementsByTagName('p');
					var as = this.getElementsByTagName('a');					
					for (var j = 0; j < ps.length; j++) Cufon.replace(ps[j], {color: '#4EEBF5'})					
					for (var l = 0; l < as.length; l++) {
						if (as[l].className.indexOf('master') != '-1') Cufon.replace(as[l], {color: '#4EEBF5'});
					}
				}	
				lis[i].onmouseout = function() {
					this.className = this.className.replace(" hovered", "");					
					var ps = this.getElementsByTagName('p');
					var as = this.getElementsByTagName('a');					
					for (var j = 0; j < ps.length; j++) Cufon.replace(ps[j], {color: '#000'});									
					for (var l = 0; l < as.length; l++) {
						if (as[l].className.indexOf('master') != '-1') Cufon.replace(as[l], {color: '#000000'});
					}					
				}
				lis[i].onclick = function() { redirect(ROOT_URL + "multimedia/"); }
				
			}
		}
	}
	
	if (template.indexOf('multimedia') != '-1') {
		
		if (template.indexOf('loggedin0') != -1) {
			main.style.visibility = 'visible';
			parseLinks();
			return;
		}
		
		var multimedia = document.getElementById('multimedia');
		var lis = multimedia.getElementsByTagName('li');
		var lis_no = lis.length;
		
		if (lis_no > 0) {		
		
			for (var i = 0; i < lis_no; i++) {
				if (lis[i].parentNode.parentNode.id == 'multimedia' && lis[i].className.indexOf('selected') == -1) {
				
					lis[i].onmouseover = function() {
						this.className += " hovered";
						var ps = this.getElementsByTagName('p');
						var as = this.getElementsByTagName('a');					
						for (var j = 0; j < ps.length; j++) {
							if (ps[j].className.indexOf('date') != '-1') Cufon.replace(ps[j], {color: '#4EEBF5'})					
						}
						for (var l = 0; l < as.length; l++) {
							if (as[l].className.indexOf('master') != '-1') Cufon.replace(as[l], {color: '#4EEBF5'});
						}
					}	
					lis[i].onmouseout = function() {
						this.className = this.className.replace(" hovered", "");					
						var ps = this.getElementsByTagName('p');
						var as = this.getElementsByTagName('a');					
						for (var j = 0; j < ps.length; j++) {
							if (ps[j].className.indexOf('date') != '-1') Cufon.replace(ps[j], {color: '#000'})					
						}
						for (var l = 0; l < as.length; l++) {
							if (as[l].className.indexOf('master') != '-1') Cufon.replace(as[l], {color: '#000000'});
						}					
					}
					lis[i].onclick = function() {
						
						if (this.className.indexOf(' selected') != '-1') {
							if (window.bubble == '0') {window.bubble = 1; return};
							this.className = this.className.replace(" selected", "");
							this.style.backgroundColor = '#ff7900';
							var ps = this.getElementsByTagName('p');
							for (var j = 0; j < ps.length; j++) {
								if (ps[j].parentNode.parentNode.parentNode.id == 'multimedia') {
									ps[j].style.backgroundPosition = "0 -31px";
									Cufon.replace(ps[j], {color: '#000'});
									ps[j].style.color = "#000";
								}
							}
							var as = this.getElementsByTagName('a');
							for (var l = 0; l < as.length; l++) {
								if (as[l].className.indexOf('master') != '-1') Cufon.replace(as[l], {color: '#000000'});
							}
						}
						else {
							this.className += " selected";							
							this.style.backgroundColor = "#000000";
							this.onmouseover = function() {};
							this.onmouseout = function() {};
							var sub_lis = this.getElementsByTagName('li');
							for (var a = 0; a < sub_lis.length; a++) {
								sub_lis[a].onmouseover = function() {								
									if (this.className.indexOf('audio') != -1) {
										var sub_lis_ps = this.getElementsByTagName('p');
										for (var b = 0; b < sub_lis_ps.length; b++) {
											if (sub_lis_ps[b].className.indexOf('title') != -1) sub_lis_ps[b].style.backgroundPosition = '0 -18px';										    
										}
									}
									else if (this.className.indexOf('video') != -1) {
										var sub_lis_ps = this.getElementsByTagName('p');
										for (var b = 0; b < sub_lis_ps.length; b++) {
											if (sub_lis_ps[b].className.indexOf('title') != -1) sub_lis_ps[b].style.backgroundPosition = '0 -14px';										    
										}
									}
									this.className += " hovered";
								}
								sub_lis[a].onmouseout = function() {
									var sub_lis_ps = this.getElementsByTagName('p');
									for (var b = 0; b < sub_lis_ps.length; b++) {
										if (sub_lis_ps[b].className.indexOf('title') != -1) sub_lis_ps[b].style.backgroundPosition = '0 0';									    
									}
									this.className = this.className.replace(" hovered", "");
								}
								sub_lis[a].onclick = function() {
								
									window.bubble = 0;
									var m_id = this.id.replace("audio", "").replace("video", "");										
/*									
									new Ajax.Request(ROOT_URL + AJAX_URL, {
										method: "post",
										parameters: {action:"downloadMultimedia", id:m_id},
										onComplete:function(transport) {
										alert('da');return;
											redirect(transport.responseText);
										}
									});
*/									
                                $.post(ROOT_URL + AJAX_URL, {
    								action: 'downloadMultimedia',
    								id: m_id
									}, function(res) {									
    									redirect(res);
								});
								
								}

							
							}
						}
						
					}
				}
			}
			
		}
		
	}
	
	if (context.indexOf('Events') != -1) {
		
		$(document).ready(function() {
	  		$("a[rel=gallery]").fancybox({
	  			 'zoomOpacity': true,
	  			 'zoomSpeedIn'			: 600,
				 'zoomSpeedOut'			: 500,
				 'easingIn'				: 'easeInExpo',
				 'easingOut'			: 'easeInBack',
				 'overlayShow'          : true,
				 'centerOnScroll'       : true

			}); 
	 	});
		
	}
	
	if (1==2 && context.indexOf('Photos') != -1) {
		
		for (var a = 1; a <= 8; a++) {
		
		$(document).ready(function() {
	  		$("a[rel=gallery" + a + "v1]").fancybox({
	  			 'zoomOpacity': true,
	  			 'zoomSpeedIn'			: 600,
				 'zoomSpeedOut'			: 500,
				 'easingIn'				: 'easeInExpo',
				 'easingOut'			: 'easeInBack',
				 'overlayShow'          : true,
				 'centerOnScroll'       : true

			}); 
	 	});
		
		$(document).ready(function() {
	  		$("a[rel=gallery" + a + "v0]").fancybox({
	  			 'zoomOpacity': true,
	  			 'zoomSpeedIn'			: 600,
				 'zoomSpeedOut'			: 500,
				 'easingIn'				: 'easeInExpo',
				 'easingOut'			: 'easeInBack',
				 'overlayShow'          : true,
				 'centerOnScroll'       : true

			}); 
	 	});
		
		}
		
	}
	
	main.style.visibility = 'visible';

	parseLinks();

	
}

function redirect(url) {
	document.location.href = url;
}

function timedRedirect(url, secs) {
	window.setTimeout("redirect('" + url + "')", secs * 1000);
}

function openMovieSwf(movie, swfid, container, width, height) {
	
	var s1 = new SWFObject(RES_URL + "swf/" + movie, swfid, width, height, "9");
	s1.addVariable('wmode', 'transparent');
	s1.write(container);	
	
}

function parseLinks() {

	var as = document.getElementsByTagName('a');
	var as_no = as.length;
	if (as_no == 0) return false;
	
	for (var i = 0; i < as_no; i++) {
		if (as[i].rel == 'external') as[i].target = "_blank";
	}
	
}