/* javascript 
 * needs jquery
 * written by sunfish, 2009
 */
 

	/*
	 * for image preload and maybe dynamic link
	 * get the relpath to the root
	 * if the web is located in a subdir, you may add that to rootPath
	 */
 	jQuery.get_relRootPath = function()
	{		
		// get the pathname of the current file
		var pathName = window.location.pathname;
	  	var pathArray = pathName.split('/');
		// one array part more than slashes and do not count leading slash
		var relNum = (pathArray.length-2);
		var relRootPath = '';
		// get up to the root
		for (var i = 1; i <= relNum; i++) {
			relRootPath += '../';
		}
	    debug ='pathName: ' + pathName + ' relNum : ' + relNum  + ', relRootPath: '+relRootPath;
		// alert(debug);
		return relRootPath;
	}	
	/*
	 * preload the main images
	 * needs jQuery.preload
	 */	
	jQuery(function( $ ){	
		var rp = $.get_relRootPath();
		var urls = [
				  rp+'layout/img/arrow-left.gif', 
				  rp+'layout/img/arrow-right.gif', 
				  rp+'layout/img/background.gif', 
				  rp+'layout/img/ehrler-design.gif', 
				  rp+'layout/img/ehrler-design.jpg', 
				  rp+'mediendesign/img/1.jpg',				  
				  rp+'verkaufsraum/img/1.jpg',
				  rp+'fotoarbeiten/img/1.jpg',
				  rp+'fotoarbeiten/img/2.jpg'];
		
		// $.preload(urls,{enforceCache: 'true'});
		$.preload(urls);
		
	});	
	
	/*
	 * antispam mailto
	 * no link is needed, works onclick
	 */
	jQuery.mailto_link = function(a,b) {
		parent.location = 'mailto:'+a+'@'+b;
		return false;
	}


	/*
	 * fadein, fadeout containers from the same page
	 * after all images are loaded it works quite smooth.
	 * each container has its own id. eg. img-1 or text-1
	 *
	 * everyting is written in javascript, so the source
	 * code remains easy and clean
	 */

	/*
	 * global vars outside!!
	 * last_container_nr (back-link)
	 * last_page (back-link)
	 */
	var last_container_nr = 0;
	
jQuery.show_container = function(container) {
	
    // get type and number from the name of the container
	var container_array = container.split('-');
	var container_type = container_array[0];
	var current_nr = parseInt(container_array[1]);
		
    // if it is an image container
	if(container_type == 'img') {
		// hide all image- and infotext-containers
		// for IE this is to late, so it is written in the sourcecode at the and of the body
		$('div.img').hide();
		$('div.info').hide();
	} else if (container_type == 'text') {	
	    // hide all text container
		$('div.text').hide();
	}
	// nice but not needed: count only child div container was #imgcontainer > div
	// count all div.img- or div.text-containers
	var all_num = $('#content div.'+container_type).size();
	//get the number of the next and the last container
	var next_nr = parseInt(current_nr+1);	
	var last_nr = parseInt(current_nr-1);
	// at the end: loop to the first
	if(all_num < next_nr){
		next_nr = 1;
	}
	if(current_nr == 1){
		last_nr = all_num;
	}
	debug ='all_num: ' + all_num +', current_nr: '+current_nr+', container: '+container + ', next_nr: '+ next_nr+ ', last_nr: '+last_nr;
	// alert(debug);
	
	// first remove the old navi, otherwise many of arrow-navis are written
	$('#arrow-navi').remove();
	// then get new navi from  arrow_navigation function
	var arrownavi = $.arrow_navigation(current_nr, next_nr, last_nr, container_type);
	// write the mini navigation in the document
	$('#'+container).append(arrownavi);
	
	// if there is allready a shown container: fade out, fade in 
	if(last_container_nr > 0)
	{   
		if(container_type == 'img') {
			// show info-text
			$('#'+container+' div.info').show();
		}
		// fade out last element, fade in new element
		$('#'+container_type+'-'+last_container_nr+' div.'+container_type).fadeOut(1500, function() {
			$('#'+container+' div.'+container_type).fadeIn(1500);
		});
	}
	// if this is the first container, try to load the first picture as soon as possible
	else
	{
		if(container_type == 'img') {
		//preload first image
			$('#'+container+' div.info').show();	
			var firstimg = new Image();
			$(firstimg).attr('src','img/'+current_nr+'.jpg');
		}
		$('#'+container+' div.'+container_type).fadeIn(1500);
	}
	if(container_type == 'img') {
		// make the image clickable, to get the next picture
		$('#'+container +' div.img img').attr({ title: 'weiter' });	
		$('#'+container +' div.img img').click(function() 
		{			
			$.show_container('img-'+next_nr);		
		}); 
	}
	
	last_container_nr = current_nr;
}





jQuery.arrow_navigation = function(current_nr, next_nr, last_nr, type){	
	// go to the next image container or to the next page
	var go_next = 'onclick="$.show_container(\''+type+'-'+next_nr+'\');"';	
	// go to the last image container or to the last page
	var go_back = 'onclick="$.show_container(\''+type+'-'+last_nr+'\');"';
	// alert(go_back);
	
	//get rel path for images
	var relRootPath = $.get_relRootPath();
	var imgPath = relRootPath+'layout/img/';
	
	var output = '<div id="arrow-navi"><img src="'+imgPath+'arrow-left.gif" title="zurück" '+go_back+'/><img src="'+imgPath+'arrow-right.gif" title="weiter" '+go_next+' /></div>';
	return output;
}



/* jquery INIT */
 $(document).ready(function(){  
     // hide content - to slow in IE, ther is a content flash before...
	 // so the page content is hidden from javascript in the document	 
	$('#content').hide();	
	var relRootPath = $.get_relRootPath();	
	// get a (very short) stylesheet for javascript version, reload problems with IE
	$("link[rel=stylesheet]").after('<link rel="stylesheet" type="text/css" href="'+relRootPath+'layout/style-jscript.css" />');
	/* alternative does not work in IE
	jQuery(document.createElement('link'))
	.attr({type: 'text/css', href: relRootPath+'layout/style-jscript.css', rel: 'stylesheet'})
	.appendTo('head');
	*/
	
	// get javascript background - new stylesheet does not work with internet explorer
	//$('body').css('background-image','url('+relRootPath+'layout/img/background.gif)');
	$('body').addClass('jscript');
							
	// nic but no more needed: javascript back link
	$('#backlink').click(function () { 	
      history.back();
	  return false;
    });		
	
    //  pages with imagecontainer
	if($('div.img').length > 0) {
		$('#content').show();
		// wrap the imagecontainer for context css
		// $('#imgcontainer').wrap('<div id="jscript"></div>');
		// show logo part		
		if(location.hash == '#logo') {
			$.show_container('img-10');			
		} else {
		// show only the first image
			$.show_container('img-1');
		}
	//  simple pages
	} else if ($('div.text').length > 0) {
		$('#content').show();
		// wrap the imagecontainer for context css
		// $('#content').wrap('<div id="jscript"></div>');
		// show only the first image
		$.show_container('text-1');
	} else {
	//  simple pages
    	$('#content').fadeIn(1500);
	}
	
		// email adress antispam - needs jquery
		// eg. write in html name<span> |at| </span>domain.org
	$('#content p:contains("|at|"),td:contains("|at|")').each(function(){
		// alert($(this).html());
		var filter = /([a-zA-Z0-9_\.\-]+)(<span> \|at\| <\/span>)([a-zA-Z0-9_\-\.]+\.[a-zA-Z]{2,3})/ig;
		var emaillink = "<a onclick=\"$.mailto_link('$1','$3')\" class=\"email\">$1 (at) $3</a>";
		$(this).html($(this).html().replace(filter, emaillink));	
	});
	
	
		// open external links with target _blank	
	$("a[href^='http']").attr('target','_blank');
	

});


