// JavaScript Document

function el(ObjID) { return document.getElementById(ObjID); }

var themeTimeout;

function changeHeader(Source) {
	jQuery("#page-header").append('<div id="load-header" style="position:absolute; display:none; height:318px; width:960px; z-index:300;"><div style="width:56px; height:21px; display:block; margin-left:auto; margin-right:auto; margin-top:150px;"><img src="/Resources/Images/ajax-loader.gif" height="21" width="56" /></div></div>');
	var img = new Image();
	jQuery(img).load(function() {
		jQuery("#load-header").remove();
		jQuery("#header-background").animate({opacity:0},600,function() {
			jQuery(this).remove();
			jQuery("#page-header-image").html('<img src="/Resources/Upload/Headers/theme-' + Source + '.jpg" alt="Latina.ro" style="display:none" id="header-background" />');
			
			var pos = jQuery("#theme-" + Source).position();
			jQuery("#selected-theme").animate({left:pos.left + 'px'},600);
			
			jQuery("#header-background").css("opacity",0).css("display","block").animate({opacity:1},600,function() {
				activeTheme = Source;
					
				var nextTheme = arrayFindPos(themes,activeTheme) + 1;
				
				if(typeof(themes[nextTheme]) == 'undefined')
					nextTheme = themes[0];
				else
					nextTheme = themes[nextTheme];
				
				clearTimeout(themeTimeout);
				themeTimeout = setTimeout("changeHeader(" + nextTheme + ")",10000);
				jQuery("#theme-0").html("&times;").attr("href","javascript:stopAutoChangeHeader()");
				
			});
		});
	
	}).attr("src","/Resources/Upload/Headers/theme-"+ Source+".jpg");
	jQuery("#load-header").css("display","block");
}

function arrayFindPos(array,value) {
	for(var i = 0; i < array.length; i++) {
		if(array[i] == value)
			return i;
	}
}

function startAutoChangeHeader() {
	var nextTheme = arrayFindPos(themes,activeTheme) + 1;
				
	if(typeof(themes[nextTheme]) == 'undefined')
		nextTheme = themes[0];
	else
		nextTheme = themes[nextTheme];
		
	themeTimeout = setTimeout("changeHeader(" + nextTheme + ")",10000);
	jQuery("#theme-0").html("&times;").attr("href","javascript:stopAutoChangeHeader()");
	jQuery.post("/Resources/Php/setHeader.php?p=delete");
}

function stopAutoChangeHeader() {
	clearTimeout(themeTimeout);
	jQuery("#theme-0").html("&gt;").attr("href","javascript:changeHeader(" + activeTheme + ")");
	jQuery.post("/Resources/Php/setHeader.php?p=create", {headerBackground:activeTheme});
}

function activateMap(mapID) {
	jQuery("#" + mapID).find("area").each(function() {
		var explode = jQuery(this).attr("alt").split("-");
		var regionPath = explode[0];
		var regionName = explode[1];
		jQuery(this).mouseenter(function() {
			
			jQuery("#" + mapID + "-region-title").html(regionName).css("opacity",0).css("display","block").animate({opacity:1},300,function() {
				jQuery().mousemove(function(e) {
					jQuery("#" + mapID + "-region-title").css("left",parseInt(e.pageX + 20) + "px").css("top",e.pageY + 'px');	
				});
			});
			
			jQuery("#" + mapID + "-map").attr("src","/Resources/Images/" + regionPath + ".gif");	
			
		}).mouseleave(function() {
			jQuery("#" + mapID + "-region-title").css("display","none");
			jQuery("#" + mapID + "-map").attr("src","/Resources/Images/hartaprincipal.gif");	
		});										   
	});
}

function imagesLoad(Class) {
	if(jQuery(Class)) {
		var img = new Image()
		jQuery(Class).each(function() {
			var div = jQuery(this);
			jQuery(this).find('img').each(function() {
				jQuery(div).append("<img src=\"/Resources/Images/small-white-loader.gif\" id=\"loader-" + jQuery(div).attr("id") + "\" />");
				var photo = jQuery(this);
				jQuery(img).load(function() {
					
					jQuery("#loader-" + jQuery(div).attr("id")).animate({opacity:0},50,function() {
						jQuery(photo).css("display","block").animate({opacity:1},50);											 
					});
				}).attr("src",jQuery(photo).attr('src'));
				jQuery(photo).css("opacity",0).css("display","none");
			});
		});	
	}	
}

jQuery(document).ready(function() {
	jQuery(this).pngFix();
	
	jQuery("#page-menu").animate({marginTop:"288px"},500);
	
	var themeLen = parseInt(25 + 25 * themes.length);
	jQuery("#theme").css("width",themeLen + 'px').animate({left:parseInt(960 - themeLen) + 'px'},1000,function(){
		var pos = jQuery("#theme-" + activeTheme).position();
		jQuery("#selected-theme").animate({left:pos.left + 'px'},600);
	});
	
	if(el('Romania'))
		activateMap("Romania");
	
	/*imagesLoad(".gallery-photo-bg");*/
	//loadImages();
});
