// On page load
$(document).ready(function() {
	front_showModerator();
	front_loadNowPlaying();
});
//

function nav_showItemFields(id) {
	if(id.value == "1") {
		document.getElementById("nav_exturl").style.display = "none";

		document.getElementById("nav_page").style.display = "table-row";
		document.getElementById("nav_cleanurl").style.display = "table-row";
	}
	else if(id.value == "2") {
		document.getElementById("nav_exturl").style.display = "table-row";

		document.getElementById("nav_page").style.display = "none";
		document.getElementById("nav_cleanurl").style.display = "none";
	}
	
}

// Now playing
function front_loadNowPlaying() {
	$.ajax({url: "/v_zivo.php", async: true, success: function(data) {
		$("#header-playing-text").html(data);
	}});
}

var front_moderatorData = null;
var front_moderatorIndex = 0;

function front_showModerator() {
	$.ajax({url: "/index.php?action=getModeratorsData", async: true, success: function(data) {
		eval("front_moderatorData = "+ data);
		
		$("#header-moderator").fadeIn(1000);
		
		$("#header-moderator img").click(function() {
			window.location = front_moderatorData[0]["moderator-link"];
		});
		
		if(front_moderatorData.length > 1) {	
			var images_preload = new Array();
		
			for(var i = 0; i < front_moderatorData.length; i++) {
				images_preload[i] = front_moderatorData[i]["moderator-image"];
			}
		
			var cache = [];
		
			// preload
			for (var i = 0; i < images_preload.length; i++) {
			  var cacheImage = document.createElement('img');
			  cacheImage.src = arguments[i];
			  cache.push(cacheImage);
			}
					
			setTimeout("front_changeModerator()", 6000);
		}
	}});
}

function front_changeModerator() {	
	if(front_moderatorIndex == (front_moderatorData.length - 1)) {
		front_moderatorIndex = 0;
	} else {
		front_moderatorIndex++;
	}

	$("#header-moderator img").animate({left: '-=400'}, 400, function() {		
		$("#header-moderator img").attr("src", front_moderatorData[front_moderatorIndex]["moderator-image"]);
				
		$("#header-moderator img").click(function() {
			window.location = front_moderatorData[front_moderatorIndex]["moderator-link"];
		});
		
		$("#header-moderator img").css("left", "400px");				
		$("#header-moderator img").animate({left: '-=400'}, 500);
	});
	
	$("#header-moderator-text").animate({left: '-=400'}, 400, function() {				
		$("#header-moderator-text").html(front_moderatorData[front_moderatorIndex]["moderator-text"]);
		
		$("#header-moderator-text").css("left", "400px");
		$("#header-moderator-text").animate({left: '-=400'}, 500);
	});	
	
	setTimeout("front_changeModerator()", 6000);
}

// Ads
var ads = new Array(); 

function ads_startRotator(container, ads_class, time) {
	var children = $("#"+ container + "").children().size();	
	
	ads.push(container);	
	ads[container] = 0;
	
	if(children != 1) {
		ads_changeAd(container, ads_class, time);
	} else {
		$("#"+ container + "").children().show();
	}
}

function ads_changeAd(container, ads_class, time) {
	var first = false;
	var children = $("#"+ container + "").children().size();	
	var previous = ads[container] != 1 ? ads[container] - 1 : children;		
	
	if(ads[container] == 0) {
		first = true;
		ads[container] = 1;
	}
	
	if(first) {
		$("#"+ container + " ."+ ads_class + ":nth-child(1)").show();
	} else {
		var c = ads[container];
		
		$("#"+ container + " ."+ ads_class + ":nth-child("+ previous +")").fadeOut(500, function() {
			$("#"+ container + " ."+ ads_class + ":nth-child("+ c +")").fadeIn(500);
		});	
	}
	
	if(ads[container] == children) {
		ads[container] = 1;
	} else {
		ads[container]++;
	}
	
	setTimeout("ads_changeAd('"+ container +"', '"+ ads_class +"', "+ time +")", time);
}

// Weather
function weather_style() {
	$("#weather-inside .weather-city:nth-child(1) div > p").appendTo("#weather-module");
	$("#weather-inside .weather-city div > p").remove();
	
	$("#weather-module > p").css("margin-top", "5px").css("text-align", "center").css("margin-left", "0px");
	$("#weather-module > p a").html("vir: Pro-Vreme");
}

function weather_styleImages(site) {
	$("#weather-inside .weather-city img").each(function(index, image) {
		// Replace
		$(image).attr("src", $(image).attr("src").toString().replace(/(.*?)ikone\/(.*).gif/, "/sites/"+ site +"/images/weather/$2.png"));
	});
}

function weather_change(id, name) {	
	$("#weather-city-title").html(name);
	
	$("#weather-inside .weather-city").hide();
	$("#weather-inside #weather-city-"+ id +"").show();
	weather_style();
}

// User pages
var user_pages_index = 0; 

function user_pages_startImageRotator() {
	var children = $("#ekipa-clan-wrapper .ekipa-clan-img").size();	
	
	if(children != 1) {
		user_pages_changeImage(5500);
	} else {
		$("#ekipa-clan-wrapper .ekipa-clan-img").show();
	}
}

function user_pages_changeImage(time) {
	var first = false;
	var children = $("#ekipa-clan-wrapper .ekipa-clan-img").size();		
	var previous = user_pages_index != 1 ? user_pages_index - 1 : children;		
	
	if(user_pages_index == 0) {
		first = true;
		user_pages_index = 1;
	}
	
	if(first) {
		$("#ekipa-clan-wrapper .ekipa-clan-img:nth-child(1)").show();
	} else {
		var c = user_pages_index;
		
		$("#ekipa-clan-wrapper .ekipa-clan-img:nth-child("+ previous +")").fadeOut(300, function() {
			$("#ekipa-clan-wrapper .ekipa-clan-img:nth-child("+ c +")").fadeIn(300);
		});	
	}
	
	if(user_pages_index == children) {
		user_pages_index = 1;
	} else {
		user_pages_index++;
	}
	
	setTimeout("user_pages_changeImage("+ time + ")", time);
}

// Comments
function comments_load_all(id) {
	$.ajax({url: "/index.php?action=loadComments&id="+ id + "", async: true, success: function(data) {
		$("#comments-navigation").remove();
		$("#comments-list").append(data);
	}});
}