/*
 * www.pleasantbeef.com
 * 2009, August
 */


/************************ browser check / BEGIN ************************/

/* Cut browser version no. out of the given browser ID string.
 * Hand over browser's ID, a starting point (index) and a flag (char) signaling where to stop.
 * @ param:		BrowserID 				- browser's ID string (here: navigator.userAgent)
 * 				startIndex 				- index signaling where to start looking at the string
 * 				stopChar				- char indicating where to stop looking at the string
 * @ return:	browser version no.
 * @ example:	getBrowserVersion(navigator.userAgent, navigator.userAgent.indexOf("msie") + "msie ".length, "#")
 */
function getBrowserVersion(BrowserID, startIndex, stopChar) {
	
	return (BrowserID.substring(startIndex, BrowserID.indexOf(stopChar, startIndex)))
}


/* Deliver browser name and version.
 * All returning browser names are delivered lower case.
 * @ return: 	getBrowser().browser	- browser name
 * 				getBrowser().version	- browser version
 */
function getBrowser() {
	var checkBrowserAgent = navigator.userAgent.toLowerCase();
	var browserType = "";
	var browserVersion = "";
	
	// Detect Microsoft IE
	// e.g.: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)
	if (checkBrowserAgent.indexOf("msie") != -1) {
		browserType = "IE";
		browserVersion = getBrowserVersion(checkBrowserAgent, checkBrowserAgent.indexOf("msie") + "msie ".length, ".");
	}
	// Detect Google Chrome. Place Chrome before Safari as Chrome introduces itself as "chrome/x.x.x safari/x.x.x"
	// e.g.: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/530.1 (KHTML, like Gecko) Chrome/2.0.169.1 Safari/530.1
	else if(checkBrowserAgent.indexOf("chrome") != -1) {
		browserType = "Chrome"
		browserVersion = getBrowserVersion(checkBrowserAgent, checkBrowserAgent.indexOf("chrome") + "chrome/".length, ".");
	}
	// Detect Iron. See Chrome.
	// e.g.: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/530.1 (KHTML, like Gecko) Iron/2.0.168.0 Safari/530.1
	else if(checkBrowserAgent.indexOf("iron") != -1) {
		browserType = "Iron"
		browserVersion = getBrowserVersion(checkBrowserAgent, checkBrowserAgent.indexOf("iron") + "iron/".length, ".");
	}
	// Detect Safari
	// e.g.: Mozilla/5.0 (Windows; U; Windows NT 5.1; de-DE) AppleWebKit/525.28 (KHTML, like Gecko) Version/3.2.2 Safari/525.28.1
	else if (checkBrowserAgent.indexOf("safari") != -1) {
		browserType = "Safari";
		browserVersion = getBrowserVersion(checkBrowserAgent, checkBrowserAgent.indexOf("version") + "version/".length, ".");
	}
	// Detect Firefox
	// e.g.: Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.8.1.20) Gecko/20081217 Firefox/2.0.0.20
	else if (checkBrowserAgent.indexOf("firefox") != -1) {
		browserType = "Firefox";
		browserVersion = getBrowserVersion(checkBrowserAgent, checkBrowserAgent.indexOf("firefox") + "firefox/".length, ".");
	}
	// Detect K-Meleon
	else if (checkBrowserAgent.indexOf("k-meleon") != -1) {
		browserType = "kmeleon";
	}
	
	return ({
		browser: browserType.toLowerCase(), 
		version: browserVersion
	});
}

/************************ browser check / END ************************/


/************************ Execute immediately section / BEGIN ********/

// Check for global config object. If not available, initiate.
if (typeof(globalConfig) === "undefined") {
	var globalConfig = {};
}

// Get global config object from window.name. Make sure a value is handed over.
var getGlobalConfig = (window.name !== "") ? window.name : "";

// Check for older browser version.
// Show browser recommendation if either no skipping is signaled and browser is outdated.
if (((typeof(globalConfig.skipBrowserCheck) === "undefined") || (globalConfig.skipBrowserCheck !== "true")) && ((getBrowser().browser === "ie" && getBrowser().version < "7") || (getBrowser().browser === "firefox" && getBrowser().version < "2") || (getBrowser().browser === "safari" && getBrowser().version < "3"))) {
	window.location.href = window.location.protocol + "//" + window.location.host + "/browser/browser_recommendation.html";
}

// Show or hide icon according to login-state
if (typeof (globalConfig) !== "undefined" && typeof(globalConfig.header_secureIcon) !== "undefined")
	$("#header ul.menu li .secureIcon").css(globalConfig.header_secureIcon);

// Execute font replacement
Cufon.replace("div.contentArea h1", {
	fontFamily: 'Lucida Grande'
});
Cufon.replace("div.news h3, ul.subMenu li a, div#footer a.backToTop, div#footer li", {
	fontFamily: 'Lucida Grande Bold'
});

// Execute hyphenation
if (!(getBrowser().browser === "firefox" && getBrowser().version === "2") && typeof(Hyphenator) !== "undefined") {
	$(".hyphenate").css({
		visibility: "hidden"
	});
	Hyphenator.config({
		remoteloading: false,
		enablecache: true,
		onhyphenationdonecallback: function() {
			// Callback when hyphenation is done.
			// Justify all related elements and make them visible again.
			$(".hyphenate").css({
				"text-align": "justify",
				visibility: "visible"
			});
		},
		onerrorhandler: function(error) {}
	});
	Hyphenator.run();
}

/************************ Execute immediately section / END **********/



/************************ Add functionality / BEGIN ******************/

var generateHTML = {
	
	MailObj: {},

	inlineCSS: function() {
		var styles = "html, body {margin: 0px; padding: 0px; font-size: 12px; font-family: Calibri, Trebuchet MS, Verdana, Tahoma, Arial;}" +
		".wrapper {padding: 10px 15px 20px;}" +
		"p {margin: 0px; padding: 7px 0px 0px;}" +
		"h2.title {margin-top: 0px; font-size: 15px; text-transform: uppercase; border-bottom: 1px solid #CDCECD;}" +
		"address {font-style: normal; margin-bottom: 50px;}" +
		"address span.name {text-transform: capitalize;}";
		
		return styles;
	},
	
	prepareMail: function(callback) {
		var HTML_MailHeader = 
		"<html>" +
		"<head>" +
		"<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />" +
		"<style>" + generateHTML.inlineCSS() + "</style>" +
		"</head>" +
		"<body>";
		
		var HTML_MailFooter = 
		"</body>" +
		"</html>";
		
		var HTML_MailMessage =
		"<div class='wrapper'><h2 class='title'>Kontaktformular Website</h2>" +
		"	<address>" +
		"		<span class='name'>" + generateHTML.MailObj.Salutation + "&nbsp;" + generateHTML.MailObj.Name + "</span><br />" + 
				generateHTML.MailObj.eMail + 
		"	</address>" +
		"	<p>" + generateHTML.MailObj.Message + "</p>";
		"</div>";

		var HTML_Mail = HTML_MailHeader + HTML_MailMessage + HTML_MailFooter;
		if ($('div#contact form#contactForm').length != 0) {
			$('div#contact form#contactForm').append('<input type="hidden" name="mailBody" id="mailBody" value="' + HTML_Mail + '" />');
		}
		
		if (typeof(callback) === "function") {
			callback();
		}
	}
}

/************************ Add functionality / END ********************/


/************************ Document ready / BEGIN *********************/

$(document).ready(function() {
	
	// Set focus to input field.
	try {
		$("div#login form input.inputText:first").focus();
	}
	catch (error) {}


	
/* *** ADD EVENT HANDLING. *** */
 
	$("div#contact form#contactForm").bind("submit", function (event) {
		event.preventDefault();
		// Fehlerpruefung
		generateHTML.MailObj.Salutation = $("div#contact form#contactForm input#salutation").val();
		generateHTML.MailObj.Name = $("div#contact form#contactForm input#name").val();
		generateHTML.MailObj.eMail = $("div#contact form#contactForm input#mail").val();
		generateHTML.MailObj.Message = $("div#contact form#contactForm textarea#message").val();
		
		generateHTML.prepareMail(function() {
			$("div#contact form#contactForm").unbind("submit");
			$("div#contact form#contactForm").submit();
		});
	});

	$("div.collection_single_head ul li a").click(function() {
		var mainImage = $(this).attr("href"); //Find Image Name
		jQuery("<img>").attr("src", mainImage);
		$("div.single_img img").stop().animate({
			opacity: 0
		}, 'slow', null, function() {
			$("div.single_img img").attr({src: mainImage});
		});
		
		$("div.single_img a").attr({href: mainImage});

		$("div.single_img img").fadeTo("slow", 1);

		$(this).parent().css({opacity: 0.35}); 
		$(this).parent().siblings().css({opacity: 1}); 
		
		return false;
	});	

	$('a > img').parent().css('border', '0');
});

/************************ Document ready / END ***********************/