var isW3c = true;
if(typeof(isIE) === 'undefined') var isIE = false;

// ------[ IE6 Cache Control (remove flicker on mouseover) ]------------------------------------------------- //	
try {
  document.execCommand('BackgroundImageCache', false, true);
} catch(e) {}

window.UNITERRA = window.UNITERRA || {};
UNITERRA = {};

// ------[ Galleries ]------------------------------------------------- //
UNITERRA.Galleries = {
	Gallerie : [],
	
	Init : function() {					
		for(x=0; x<this.Gallerie.length; x++) {					
			$("#link-" + this.Gallerie[x].id + " a").click(function(event) {
				event.preventDefault();
				UNITERRA.Galleries.Show($(this));
			});			
		}							
		
		if(this.Gallerie.length > 0) {
			UNITERRA.Galleries.Show($("#link-" + this.Gallerie[0].id + " a"));
		}
	},
	
	HideAll : function() {
		for(x=0; x<this.Gallerie.length; x++) {	
			$("#link-" + this.Gallerie[x].id).removeClass('open');
			$("#" + this.Gallerie[x].id).removeClass('open');
		}
	},
	
	Show : function(obj) {		
		UNITERRA.Galleries.HideAll();
		id = obj.attr('rel');

		$("#link-" + id).addClass('open');
		$("#" + id).addClass('open');				
	}
}

// ------[ Blocks ]------------------------------------------------- //
UNITERRA.Blocks = {	
	Init : function() {					
		$("#blocks a.block-content-link").click(function(event) {
				event.preventDefault();
				UNITERRA.Blocks.Show($(this));
		});					

	},
	
	Show : function(obj) {		
		id = obj.attr('href').replace("#", "");
			
		if(obj.hasClass('open')) {
			obj.removeClass('open');
			obj.addClass('close');
			$("#" + id).removeClass('open');	
			$("#" + id).addClass('close');				
		
		} else {		
			obj.removeClass('close');
			obj.addClass('open');
			$("#" + id).removeClass('close');	
			$("#" + id).addClass('open');				
		}
	}
}

// ------[ AjaxLoad ]------------------------------------------------- //
UNITERRA.AjaxLoad = {
	Load : function(event, container, url, nextpage, pagecount) {						
		event.preventDefault();
		currentpage = $("#" + container).attr("current_page");
		if(!currentpage) {
			$("#" + container).attr("current_page", 1);
			currentpage = 1;
		}
		
		currentpage = parseInt(currentpage);
		nextpage = parseInt(nextpage);	
		
		var nextpage = currentpage+nextpage;
		if(nextpage > pagecount) nextpage = 1;
		else if(nextpage < 1) nextpage = pagecount;
		
		$("#" + container).attr("current_page", nextpage);
		
		var completeURL = url + nextpage + "&ajax=1"; 
		var html = '<center><img src="/themes/uniterra/ui/images/ajax-loader.gif" width="16" height="11" /></center>';
		$("#" + container + "-content").html(html);
		//$("#" + container + "-content").load(completeURL, {}, function(responseText, textStatus, XMLHttpRequest) {			
			
		$.ajax({
			type:	'POST',
			url:	completeURL,
			data:	"ajax=1",
			dataType:	'html',
			timeout:	5000,
			success: function(d,s){
					
						$("#" + container + "-content").fadeOut('slow',function(){
								$(this).html(d).fadeIn('slow');
							});
						
					},
			error: function(o,s,e){
						window.location = _Href;
					}
		});

		return false;										
	}	
}


// ------[ Map ]------------------------------------------------- //
UNITERRA.HomeHeader = {
	Header : false,
	URL : false,
	Page : 1,
	HeaderImages : [],
	index : 1,
	count: 1,
	timer: false,
	
	Init : function(url) {
		this.Header = $("#home-header");
		this.URL = url;	
		
		var div = document.createElement('div');
		div.setAttribute('id', 'home-header-tmp');
		div.setAttribute('style', 'display: block;');	
		
		$('#home-header').before(div);

		UNITERRA.HomeHeader.ResetTimer(10000);						
		UNITERRA.HomeHeader.SetHover();			
	},
	
	Update : function() {		
		html = $('#home-header').html();		
		$('#home-header-tmp').html(html);
		$('#home-header-tmp').css('display', 'block');
		Cufon.refresh();
		
		$('#home-header').css('display', 'none').load(this.URL + 'volontaire/?ajax=1',function() {
			Cufon.refresh();
			
			$('#home-header').fadeIn(2000);
			$('#home-header-tmp').fadeOut(2000);
						
			index = UNITERRA.HomeHeader.index + 1;
			if(index > UNITERRA.HomeHeader.HeaderImages.length-1) index = 0;
			
			UNITERRA.HomeHeader.index = index;
			
			UNITERRA.HomeHeader.SetHover();	
						
		});
		
		this.count++;
		
		if(this.count < 50) {	
			UNITERRA.HomeHeader.ResetTimer(10000);				
		}
	},
	
	ResetTimer : function(seconds) {
		clearTimeout(UNITERRA.HomeHeader.timer);
		UNITERRA.HomeHeader.timer = false;			
		UNITERRA.HomeHeader.timer = setTimeout("UNITERRA.HomeHeader.Update()", seconds);		
	},
	
	SetHover : function() {
		$('div.home-volontaire-open').mouseover(function() {
			UNITERRA.HomeHeader.ResetTimer(30000);			
		})
		$('div.home-volontaire-open').mouseout(function() {
			UNITERRA.HomeHeader.ResetTimer(10000);
		})		
		
		
	}
}

/*
jQuery.fn.fadeIn = function(speed, callback) { 
    return this.animate({opacity: 'show'}, speed, function() { 
        if (jQuery.browser.msie)  
            this.style.removeAttribute('filter');  
        if (jQuery.isFunction(callback)) 
            callback();  
    }); 
}; 
 
jQuery.fn.fadeOut = function(speed, callback) { 
    return this.animate({opacity: 'hide'}, speed, function() { 
        if (jQuery.browser.msie)  
            this.style.removeAttribute('filter');  
        if (jQuery.isFunction(callback)) 
            callback();  
    }); 
}; 
 
jQuery.fn.fadeTo = function(speed,to,callback) { 
    return this.animate({opacity: to}, speed, function() { 
        if (to == 1 && jQuery.browser.msie)  
            this.style.removeAttribute('filter');  
        if (jQuery.isFunction(callback)) 
            callback();  
    }); 
}; 
*/   

// ------[ Map ]------------------------------------------------- //
UNITERRA.Map = {
	Zones : [],
	Init : function() {						
		mapobj = document.getElementById('map');		
		for(x=0; x<UNITERRA.Map.Zones.length; x++) {
			zone = UNITERRA.Map.Zones[x];
			if(zone.y && zone.x) {
				var dot = document.createElement('a');
				//dot.setAttribute('style','background-image:url(/themes/uniterra/ui/images/dot.png); top: ' +  zone.y + 'px; left: ' +  zone.x + 'px;');
				dot.setAttribute('id', 'dot-' + zone.id);
				dot.setAttribute('name', zone.name);
				dot.setAttribute('rel', zone.id);
				dot.innerHTML = "<span></span>";
				mapobj.appendChild(dot);	
				
				$(dot).css("background-image", "url('/themes/uniterra/ui/images/dot.png')");
				$(dot).css("top", zone.y + "px");
				$(dot).css("left", zone.x + "px");
				$(dot).addClass("dot dotlink");
				
				$("#" + dot.id).click(function() {
					UNITERRA.Map.Show($(this));
				});
				
			} else if(zone.name == "canada") {
				// add canada hover (hardcoded)
				var dot = document.createElement('a');
				dot.setAttribute('id', 'dot-' + zone.id);	
				dot.setAttribute('rel', zone.id);
				dot.setAttribute('name', zone.name);
				//dot.setAttribute('class', 'dot-canada dotlink');
				dot.setAttribute('className', 'dot-canada');
				dot.innerHTML = "<span></span>";				
				mapobj.appendChild(dot);	
				
				
				$(dot).addClass("dot-canada dotlink");				
				
				$("#" + dot.id).click(function() {
					UNITERRA.Map.Show($(this));
				});						
			}
		}
	
		$("body").click(function(e) {
			if(!e.target) {
				target = e.srcElement;
			} else {
				target = e.target;
			}
			
			target = $(target).hasClass("dotlink");
			if(!target) {		
				UNITERRA.Map.HideAll();
			}
			
			//console.log(e.target);
		});												
	},
	
	Show : function(obj) {
		UNITERRA.Map.HidePanel();
		id = obj.attr('rel');
		
		if(obj.attr('name') == "canada") {
			$("#dot-" + id).addClass("selected-canada");		
		} else {
			$("#dot-" + id).addClass("selected");						
		}
		$("#panel-" + id).css("visibility", "inherit");
	},
	
	HidePanel : function() {
		for(x=0; x<UNITERRA.Map.Zones.length; x++) {
			zone = UNITERRA.Map.Zones[x];
			
			if(zone.name == "canada") {
				$("#dot-" + zone.id).removeClass("selected-canada");		
			} else {
				$("#dot-" + zone.id).removeClass("selected");						
			}
	
			$("#panel-" + zone.id).css("visibility", "hidden");
		}	
	},	
	
	HideAll : function() {
		for(x=0; x<UNITERRA.Map.Zones.length; x++) {
			zone = UNITERRA.Map.Zones[x];
			
			if(zone.name == "canada") {
				$("#dot-" + zone.id).removeClass("selected-canada");		
			} else {
				$("#dot-" + zone.id).removeClass("selected");						
			}
	
			$("#panel-" + zone.id).css("visibility", "hidden");
		}
		
		$("#map #enjeux ul li a").removeClass("selected");
		for(x=0; x<UNITERRA.Map.Zones.length; x++) {
			zone = UNITERRA.Map.Zones[x];			
			$("#dot-" + zone.id).removeClass("dot-hide");			
		}		
	},
	
	Filter : function(id, href) {		
		UNITERRA.Map.HideAll();
		$("#map #enjeux ul li a").removeClass("selected");
		$(href).addClass("selected");	
				
		for(x=0; x<UNITERRA.Map.Zones.length; x++) {
			zone = UNITERRA.Map.Zones[x];
			
			found = false;
			
			if($.inArray(id, zone.enjeux) != -1) {
				found = true; 
			}

			if(found) { 
				$("#dot-" + zone.id).removeClass("dot-hide");
			} else {
				$("#dot-" + zone.id).addClass("dot-hide");				
			}
		}
	}
}

// ------[ SubMap ]------------------------------------------------- //
UNITERRA.SubMap = {
	Zones : [],
	mapimg : false,
	subcarteimg : false,
	
	Init : function() {				
		map = document.getElementById('submap');
		UNITERRA.SubMap.subcarteimg = document.getElementById('subcarte');	
		UNITERRA.SubMap.mapimg = UNITERRA.SubMap.subcarteimg.src;
		
		for(x=0; x<UNITERRA.SubMap.Zones.length; x++) {
			zone = UNITERRA.SubMap.Zones[x];
			if(zone.y && zone.x) {
				var dot = document.createElement('a');
				dot.setAttribute('style','background-image:url(/themes/uniterra/ui/images/subdot.png); top: ' +  zone.y + 'px; left: ' +  zone.x + 'px;');
				dot.setAttribute('id', 'dot-' + zone.id);
				dot.setAttribute('name', zone.name);
				dot.setAttribute('class', 'subdot');
				dot.setAttribute('img', zone.img);				
				//dot.setAttribute('title', 'En savoir plus');
				dot.setAttribute('rel', zone.id);
				dot.innerHTML = "<span></span>";
				map.appendChild(dot);	
				
				$("#" + dot.id).click(function() {
					UNITERRA.SubMap.Show($(this));
				});				
			} 
		}	
		
		$("body").click(function(e) {
			if(!e.target) {
				target = e.srcElement;
			} else {
				target = e.target;
			}
			
			target = $(target).hasClass("subdot");
			if(!target) {		
				UNITERRA.SubMap.HideAll();
			}
			
			//console.log(e.target);
		});											
	},
	
	Show : function(obj) {
		UNITERRA.SubMap.HideAll();
		id = obj.attr('rel');
		img = obj.attr('img');
				
		$("#dot-" + id).addClass("subselected");								
		$("#panel-" + id).css("visibility", "inherit");
		$("#subcarte").attr("src", img);
	},
	
	HideAll : function() {
		for(x=0; x<UNITERRA.SubMap.Zones.length; x++) {
			zone = UNITERRA.SubMap.Zones[x];

			$("#dot-" + zone.id).removeClass("subselected");						
			$("#panel-" + zone.id).css("visibility", "hidden");
			$("#subcarte").attr("src", UNITERRA.SubMap.mapimg);
		}
	}	
}

// ------[ Form ]------------------------------------------------- //
/*
UNITERRA.Navigation = {
	Init : function() {				
		$('#navigation > ul > li').mouseover(function() {								
			$(this).addClass('hover');								
		});		
		$('#navigation > ul > li').mouseout(function() {					
			$(this).removeClass('hover');								
		});	
									
	}	
}
$(document).ready(function() {
	UNITERRA.Navigation.Init();
});	
*/

// ------[ Form ]------------------------------------------------- //
UNITERRA.Form = {
	Init : function() {				
		$('input.text').click(function() {					
			if($(this).val() == $(this).attr("label")) 
				$(this).val('');									
		});		
		
		$('input.text').blur(function() { 
			if($(this).val() == '') 
				$(this).val($(this).attr("label"));			
		});					
	}	
}
$(document).ready(function() {
	UNITERRA.Form.Init();
});	




