/*
 * general functions for navigation, styles and back-end
 * 
 * (c) 2010, Philipp Anger
 *  
 */


// variables with global scope
var lang = (getGETParameter('lang') == 'en' ? 'EN' : 'DE');
var mod = (getGETParameter('mod') == '' ? mod = 0 : getGETParameter('mod'));
var page = getCurrentPage();
var windowWidth = $(window).width();
var windowHeight = $(window).height();


// returns value of get parameter
function getGETParameter(name){
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	
	var regexS = "[\\?&]"+name+"=([^&#]*)";
	var regex = new RegExp( regexS );
	var results = regex.exec(window.location.href);

	if( results == null ){
		return '';
	}else{
		return results[1];
	}
}

// returns the filename of the active php file
function getCurrentPage(){
	var path = window.location.pathname;
	return (path.match(/backend/)) ? "backend.php" : "index.php";
}


// checks which flash version is installed
function checkFlashVersion(){
	if(!FlashDetect.installed || !FlashDetect.versionAtLeast(8)){
		$('#banner').remove();
		if(mod == 0){
			$('<span></span>').attr('id','sliderimage').css('background-image','url(source/images/machines.jpg)').text('ImageSlider').appendTo($('#head-slider'));
		}else if(mod == 3){
			$('<span></span>').attr('id','sliderimage').css('background-image','url(source/images/shop.jpg)').text('ImageSlider').appendTo($('#head-slider'));
		}
	}
}


// returns a string with style definitions of text edits
function htmlToText(string){
	var string,match,color,fontsize,fontweight,fontstyle,temp,index;
	result = "";
	temp = string.replace(/<\/span>/g, "<>");
	temp = temp.replace(/<br\/>/g, "\n");
	
	while(temp.match(/<span class=".{11}">/)){
		match = temp.match(/<span class=".{11}">/);
		color = match[0].charAt(13);
		fontsize = match[0].charAt(15)+match[0].charAt(16)+match[0].charAt(17);
		fontweight = match[0].charAt(19);
		fontstyle = match[0].charAt(21);
		textdecoration = match[0].charAt(23);
				
		index = temp.indexOf('<span class="'+color+' '+fontsize+' '+fontweight+' '+fontstyle+' '+textdecoration+'">');
		result += temp.substr(0,index)+"<"+color+fontsize+fontweight+fontstyle+textdecoration+">";
		temp = temp.substr(index+26);
	}
	result += temp;
		
	return result;
}


// returns a string with html tags and css classes of text edits
function textToHtml(string){
	var string,match,color,fontsize,fontweight,fontstyle,textdecoration,temp,index;
	result = "";
	temp = string.replace(/<>/g, "</span>");
	temp = temp.replace(/\n/g, "<br/>");
	
	while(temp.match(/\<\w{7}\>/)){
		match = temp.match(/\<\w{7}\>/);
		color = match[0].charAt(1);
		fontsize = match[0].charAt(2)+match[0].charAt(3)+match[0].charAt(4);
		fontweight = match[0].charAt(5);
		fontstyle = match[0].charAt(6);
		textdecoration = match[0].charAt(7);
		
		index = temp.indexOf("<"+color+fontsize+fontweight+fontstyle+textdecoration+">");
		result += temp.substr(0,index)+'<span class="'+color+' '+fontsize+' '+fontweight+' '+fontstyle+' '+textdecoration+'">';
		temp = temp.substr(index+9);
	}
	result += temp;
			
	return result;
}


$(document).ready(function() {
	($('#sliderimage').size() != 1) ? checkFlashVersion(): "";
	(mod > 8 && mod < 13) ? createSortimentButtons() : "";
	(mod == 13) ? initContactForm() : "";
	initContentStyle();
	initNaviSlider();
	initListener();
});





