//MAXWEL * Scripts
//V1.0

//Contact Form
function mail(name,domain) {
  thisLocation = "mailto:" + name + "@" + domain;
  document.location = thisLocation;
}
//Contact Form Mail Validator
function checkForValidMail(idname) {
	var iAt=document.getElementById(idname).value.indexOf("@");
	var iDot=document.getElementById(idname).value.lastIndexOf(".");
	if (iAt>-1 && iDot>-1 && iAt<iDot)
		return true;
	return false;
}
//Contact Form Input Length Validator
function checkForInput(idname) {
	if (document.getElementById(idname).value.length>0)
		return true;
	return false;
}
//Contact Form Input Length Validator
function checkForInputLength(idname, minlength) {
	if (document.getElementById(idname).value.length>=1)
		return true;
	return false;
}
//Contact Form Function English
function submitWithCheckEn() {
	var checkParams=new Array("username", "usermessage", "usermail");
	for (var i=0; i<checkParams.length; i++) {
		if (document.getElementById(checkParams[i]).value.length==0) {
			alert("Form incomplete.");
			return;
		}
	}
	if (checkForValidMail("usermail")) {
		document.forms[0].submit();
		return;
	}
	alert("Email incorrect.");
}
//Contact Form Function Deutsch
function submitWithCheckDe() {
	var checkParams=new Array("username", "usermessage", "usermail");
	for (var i=0; i<checkParams.length; i++) {
		if (document.getElementById(checkParams[i]).value.length==0) {
			alert("Formular unvollständig.");
			return;
		}
	}
	if (checkForValidMail("usermail")) {
		document.forms[0].submit();
		return;
	}
	alert("Email inkorrekt.");
}

//Login Form Function
function submitLogin()
{
  document.login.submit();
}

//iPhone Detection
function isiPhone(){
	return (
		(navigator.platform.indexOf("iPhone") != -1) ||
		(navigator.platform.indexOf("iPod") != -1)
	);
}

//iOS Detection
function isiOS(){
	return (
		(navigator.platform.indexOf("iPhone") != -1) ||
		(navigator.platform.indexOf("iPod") != -1) ||
		(navigator.platform.indexOf("iPad") != -1)
	);
}

//Fancybox
//If Not iPhone
if(!isiPhone()){
	$(document).ready(function() {
		$("a.gallery").fancybox({
			'overlayShow'	: false,
			'transitionIn'	: 'elastic',
			'transitionOut'	: 'elastic',
			'padding' : 0,
			'hideOnContentClick' : true,
			'titleShow' : false,
			'showNavArrows': false,
			'showCloseButton' : false
		});
	});
}

/*Supersized
jQuery(function($){
	$.supersized({
		start_slide				:	0,		//Start slide (0 is random) //Requires multiple background images
		vertical_center         :   1,		//Vertically center background
		horizontal_center       :   1,		//Horizontally center background
		min_width		        :   100,	//Min width allowed (in pixels)
		min_height		        :   100,	//Min height allowed (in pixels)
		fit_portrait         	:   0,		//Portrait images will not exceed browser height
		fit_landscape			:   0,		//Landscape images will not exceed browser width
		image_protect			:	1,		//Disables image dragging and right click with Javascript
		slides					:   [ 		//Background image
									  		{ image : 'media/image/maxwel.jpg' },
									  		{ image : 'media/image/maxwel.jpg' },
									  		{ image : 'media/image/maxwel.jpg' }
									]					
	});
});*/

//iScroll
//If iOS
if(isiOS()){
	var myScroll;
	function myScrollRefresh() {
		myScroll = new iScroll('content', {zoom:true, hScrollbar:false, bounceLock:true});
	}
	document.addEventListener('touchmove', function (e) { e.preventDefault(); }, false);
	//document.addEventListener('DOMContentLoaded', loaded, false);
	//window.onload = function() { scrollContent.refresh(); } 
}

//Center Function
jQuery.fn.center = function () {    
	if($(window).height() > this.height()){
		this.css("position","absolute");
		this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px");
		this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
	} else {
		this.css("position","absolute");
		this.css("top", "0px");
		this.css("left", "0px");
	};
	return this;
	
}

//Cufon
function replaceFonts() {
	Cufon.replace('.admin', { hover: 'true', fontFamily: 'Times' });
	Cufon.replace('.headline', { hover: 'true', fontFamily: fontHeadline });
	Cufon.replace('.menu, .menu-active', { hover: 'true', fontFamily: fontMenu });	
	Cufon.replace('#tweets, .text, .text-active, .mini', { hover: 'true', fontFamily: fontText });
	Cufon.refresh();
}

//Document Ready
$(document).ready( function (){
	//Tweet
	$(function(){
	  $("#tweets").tweet({
		avatar_size: 32,
		count: 20,
		username: "peterschmidler",
		template: function(i){return i["text"]}
	  });
	});
	//Loading Center
	$('#loading').center();
	//Loading Show
	$("#loading").fadeTo(0, 1);
	$("#gradient").fadeTo(0, 0);
	//$("#supersized").fadeTo(0, 0);
	//$("#supersized-loader").fadeTo(0, 0);
	//If Not Tweets
	if(!$("#tweets").length){
		//Cufon Replace
		replaceFonts();
		//Loading Fadeout
		$("#loading").fadeTo(500, 0);
		//Content Center
		$('#content').center();
		//Content Fadein
		$("#content").delay(500).fadeTo(500, 1);
		//Gradient Fadein
		$("#gradient").delay(0).fadeTo(500, 1);
		//Supersized Fadein
		//$("#supersized").delay(500).fadeTo(500, 1);
		//iScroll
		//If iOS
		if(isiOS()){
			$("#content").css("height", "100%");
			myScrollRefresh();
		}
	};
})

//Resize Window
$(window).resize(function(){
    $('#content').center();
	$("#tweets").css("padding", "0");
	$("#tweets").css("margin", "0");
});

//Admin
$(function() {
	$("#changeFonts").click(function(e) {
		e.preventDefault(); // if desired...
		$LAB
    		.script("js/cufon/fonts/Helvetica Condensed.js")
			.script("js/cufon/fonts/Magda.js")
			.script("js/cufon/fonts/Courier.js")
    		.wait(function(){
				    $('a.headline').each(function() {
				        $(this).html($(this).text());
				        var clone = $(this).clone();
				        $(this).replaceWith(clone);
				    });
				    $('a.menu, a.menu-active').each(function() {
				        $(this).html($(this).text());
				        var clone = $(this).clone();
				        $(this).replaceWith(clone);
				    });
				    $('a.#tweets, a.text, a.text-active, a.mini').each(function() {
				        $(this).html($(this).text());
				        var clone = $(this).clone();
				        $(this).replaceWith(clone);
				    });
					fontHeadline="Courier";
					fontMenu="Courier";
					fontText="Courier";
					replaceFonts();
					$("#gradient").css("filter", "progid:DXImageTransform.Microsoft.gradient(startColorstr=\"#2F2F2F\", endColorstr='#000')");
					$("#gradient").css("background", "-webkit-gradient(linear, left top, left bottom, from(#2F2F2F), to(#000))");
					$("#gradient").css("background", "-moz-linear-gradient(top,  #2F2F2F, #000)");
					$('#content').center();
     		});
   });
});
