
// ==================================================================
//
// JAVASCRIPT ROMAINCOURTEMANCHE.COM
// Stephane Chanay-Savoyen
// http://www.chanaysavoyen.com
//
// ==================================================================

(function(){
	var
	// Before doing anything, be sure to have a debug
	debug=function(a,s)
	{
		try{ s.debug(a,s); }
		catch(e){ try{ fun.debug(a,s); }
			catch(e){ try{ console.log(a); }
				catch(e){}
			}
		}
	}
	,
	// add CSS as link or style
	addCSS=function(a)
	{	
		var n, d=document,
			s = a.s||a.src||a,
			t = a.t||a.target||
			d.getElementsByTagName("head")[0],
			e_msg = "function addCSS"+
			" could not append "+s+" to "+t;
		if( s.indexOf("{")<0 ){
			n = d.createElement("link"); 
			n.setAttribute("rel","stylesheet"); 
			n.setAttribute("href",s);
		}else{
			n = d.createElement("style");
			n.setAttribute("type","text/css");
			n.innerHTML=s;
		}
		if( typeof(getElt)!="undefined" &&
			typeof(t)=="string"){ t=getElt(t); }
		if(t){ try{ t.appendChild(n); }
			catch(e){ debug(e); debug(e_msg); }
		}else{ debug(e_msg); }
	}
	,
	projetInit=function()
	{	debug("function projetInit is running",this)
		
		if( $("body").hasClass("format_rc_2") ){
			
			scroll2img=function(a){
				var menu_w=249, 
					p=$("#content .container").parent(),
					e=$("#img-container > ul a").eq(a);
				
					$('html,body').animate({
						scrollLeft:e.offset().left-
							(p.offset().left+menu_w)
					}, "fast");
			}
		
			$("#img-container > ul a")
			.each(function(i){
				$(this).click(function(){
					scroll2img(i); return false;
				})
			});
		
			// Add arrow nav
			//	←	&larr;	←	&#8592;	left arrow	ISO10646
			//	→	&rarr;	→	&#8594;	right arrow	ISO10646
			//	↑	&uarr;	↑	&#8593;	up arrow	ISO10646
			//	↓	&darr;	↓	&#8595;	down arrow	ISO10646
		
			var s="<p class=\"nav\">"+
				"<a href=\"#\" onclick=\""+
				"$('html,body')"+
				".animate({scrollLeft:'0px'});"+
				"return false;"+
				"\">&lt;</a>"+
				"</p>";
			
			$("#img-container").prepend(s);
			$("#text-container").click(function(){
				$('html,body').animate({scrollLeft:'0px'});
			})
		}
		
		if( $("body").hasClass("format_slideshow") ){
		 
			$("#img-container .nav #prev").html("&lt;")
			$("#img-container .nav #next").html("&gt;")
			
		}
		
	}
	,
	menuSpecialInit=function()
	{	debug("function menuSpecialInit is running",this)
		
		$("#menu_section .section-title").each(function(){
			if( $(this).html().toLowerCase()=="special" )
			{ $(this).parent().remove(); }
		});
		
		var dloc=document.location, s_class="", 
			s_url = dloc.protocol+"//"+dloc.host+
				"index.php?/special/", 
			modrewrite=window.modrewrite||false;
		if( modrewrite==true )
		{ s_url=s_url.replace("index.php?",""); }
		if( dloc.href==s_url ||
			dloc.href==s_url+dloc.hash)
			{ s_class="active"; }
		
		var t=$("#menu_section ul:last"); // target
		if( $('a[href^="'+s_url+'"]',t).length==0 ){
			t.prepend('<li class="'+s_class+'"><a class="'+
				s_class+'" onclick="do_click();" href="'+
				s_url+'">Special</a></li>');
		}
		
		$('a[href^="'+s_url+'"]',t).click(function(){
			var dloc=document.location,
				url="http://romaincourtemanche.tumblr.com";
			dloc.href=url; 
			return false;
		})
	}
	,
	homepageInit=function(){}
	,
	// body ready
	ready=function()
	{	debug("function ready is running",this)
		
		setTimeout('move_up()', 1);
		
		// active menu
		$("#menu a").each(function(){
			var dloc=document.location;
			if( dloc.href==this.href ||
				dloc.href==this.href+dloc.hash ){
				$(this).addClass("active");
				var elt=$(this).parent().parent();
				if( elt ){ 
					$(elt).addClass("active")
					elt=$(".section-title",elt)
					.addClass("section-title-active");
				}
			}
		});
		
		// deal with menu_section expanding menu
		var elt=$("#menu_section > ul:first-child");
		$("li.section-title",elt).toggle(
			function(){ $("li:not(.section-title)",elt).show(); },
			function(){ $("li:not(.section-title)",elt).hide(); });
		$("li:not(.section-title)",elt).css({display:"none"});
		if( elt.hasClass("active") ){ $(".section-title",elt).click(); }
		
		// deal with menu special
		//menuSpecialInit();
		
		// init project
		projetInit();
		
		// init homepage
		homepageInit();
		
	}
	,
	// Lets run
	run=function(){
		
		// define globals
		window.path = '<%baseurl%>/files/gimgs/';
		
		// log
		var date = new Date(), js = 'unknown';
		try{js = $('script:last').attr('src');
			js = js.substr(js.lastIndexOf('/')+1);
		}catch(e){ debug(e); }
		var s = ''+
			'\nfunction running script: '+js+' '+
			'\nfrom: '+document.location.href+' '+
			'\nat '+date+' '+
			'\nwith browser: '+navigator.appName+
				' version: '+navigator.appVersion+
			'\n'+navigator.userAgent+
			'\n '; 
			debug(s);
		
		if( typeof jQuery=="function" ){
			
			addCSS("#expandingMenu li{ display:none; }"+
			"#expandingMenu li.section-title{ display:block; }");
			
			$('body').ready(function(){ready();});
			//$(window).bind('resize',resize);
			
		}
		
	}
	;
	// autorun
	run();
		
})();
