/**
 * Starts all javascript which is needed after dom has been built.
 */
$(document).ready(function() {

	if ($.browser.msie) {
  		if($.browser.version.substr(0,1) < 8) { initIE() };
	}
	
	if($(".showcaseList").is("ul")) { initHover($(".showcaseList .logo a")) }; // Init showcase effect
	if($("#projectTeaser").is("div")) { initHover($("#projectTeaser li:not('.btn') a")) }; // Init project reference effect
	if($(".downloadList").is("ul")) { initHover($(".downloadList .image a")) }; // Init download effect
	
	initFacebookILikeButton();
	initLogin();
});

// Layout corrections
function initIE() {
	
	$(".listing:first-child").css({"border-top":"2px solid #B7B8B9", "margin":"0px 0 10px 0", "position":"relative", "top":"32px"});
	//$(".panel:first-child").css({"border-top":"2px solid #B7B8B9", "margin":"30px 0 35px 0", "position":"relative", "top":"32px"});
	$(".panel:first-child").css({"padding":"62px 0 0 0"});
	}

/**
 * Nivo Slider
 * Do some corrections on the slider to center navigation and display the hover image.
 */
function initSlideDeck() {
		var calcLeft = $(".controlNav").width()/2 - ($(".controlNav a:first").width() * $(".controlNav a").length)/2;  
		$(".controlNav a:first").css("margin-left",calcLeft);
	}

/**
 * Init the hover effect on image elements.
 * @param {Object} object jQuery object inside the recpecitvely needed image series. 
 */	
function initHover(object) {
	
	var teaserImageLink = object;
		
		teaserImageLink.prepend('<div class="effect">&nbsp;</div>');
		teaserImageLink.parents("li").find(".effect").hide();
		
		teaserImageLink.bind("mouseenter",function() {
			showEffect($(this));
		}).bind("mouseleave",function() {
			hideEffect($(this));
		}).bind("click", function() {
			callURL($(this));
		});
		
		teaserImageLink.live("touchend", function() {
			var element = $(this);
    		var link = element.attr('href');
		    window.location = link;

		});
		
		function hideEffect(object) {
			if($.support.opacity) {
				object.parents("li").find(".effect").stop(true, true).fadeOut(800);
			} else {
				object.parents("li").find(".effect").hide();
			}
		}
		
		function showEffect(object) {
			if($.support.opacity) {
				object.parents("li").find(".effect").stop(true, true).fadeIn(800);
			} else {
				object.parents("li").find(".effect").show();
			}
		}
		
		function callURL(object) {
			object.find("a").click();
		}
}

function initReferenceHover(object) {
		var teaserImageLink = $(".nivoTeaser .slidedeck");
		var moved = 0;
		
		teaserImageLink.prepend('<div class="effect">&nbsp;</div>');
		teaserImageLink.parents("div").find(".effect").hide();
		
		teaserImageLink.bind("mouseenter",function() {
			showEffect($(this));
		}).bind("mouseleave",function() {
			hideEffect($(this));
		}).bind("click", function() {
			callURL($(this));
		});
		
		//teaserImageLink.live("touch", function() {
			//window.location = "fullscreen.php";
		//});
		
			//Assign handlers to the simple direction handlers.
			var swipeOptions=
			{
				swipeStatus:swipeStatus
			}
			
			$(function()
			{			
				//Enable swiping...
				$(".nivoTeaser").swipe( swipeOptions );
			});
		
			//Swipe handlers.
			//The only arg passed is the original touch event object			
				
			function swipeStatus(event, phase, direction, distance)
			{
				
				if (phase=="move")
				{
					str="You have moved " + distance;
				}
				
				moved = distance;
			}
			
			teaserImageLink.live("touchend", function() {
				if(moved == 0) {
					window.location = "fullscreen.php";
				}
			});
		
		function hideEffect(object) {
			if($.support.opacity) {
				object.parents("div").find(".effect").stop(true, true).fadeOut(800);
			} else {
				object.parents("div").find(".effect").hide();
			}
		}
		
		function showEffect(object) {
			if($.support.opacity) {
				object.parents("div").find(".effect").stop(true, true).fadeIn(800);
			} else {
				object.parents("div").find(".effect").show();
			}
		}
		
		function callURL(object) {
			window.location = "fullscreen.php";
		}
	}
	
function initFacebookILikeButton() {
    
	var path = window.location.pathname;

    //$('#mainColumn h1').before('<div id="fb-root"></div>');
	//$('#mainColumn h1 cufon:last').after('<span class="fb"><fb:like href="http://www.aclewe.de/dev/aclewe/home.php" layout="button_count" show_faces="false" width="200"></fb:like></span>')
	//$('#mainColumn .header a').after('<span class="fb"><fb:like href="http://www.aclewe.de/dev/aclewe/home.php" layout="button_count" show_faces="false" width="100"></fb:like></span>')
	window.fbAsyncInit = function() {
	   FB.init({appId: '114164328661510', status: true, cookie: true, xfbml: true});
    };
	
	(function() {
        var e = document.createElement('script'); e.async = true;
        e.src = document.location.protocol + '//connect.facebook.net/de_DE/all.js';
        //document.getElementById('fb-root').appendChild(e);
	}());
}

function initLogin() {
	
	$('input').keypress(function(e){
		if(e.which == 13){
			$('form').submit();
		}
	});
	
	$('.delete').mousedown(function() {
		resetForm();
	});
	
	$('button.submit').click(function() {
		$('form').submit();
	});

	
	$('form').submit(function() {
		var username = $('#userName').val();
		var password = $('#userPassword').val();
		var directory = location.host + "/kundenzugang/" + username;
		
		/*
		if(username == false) { $('#userName').addClass('missing'); } else { $('#userName').addClass('missing'); }
		if(password == false) { $('#userPassword').addClass('missing'); } else { $('#userPassword').addClass('missing'); }
		*/
		/*
		if(username != '' && password != '') {
			
		}
		*/
		window.location.href = "http://" + username + ":" + password + "@" + directory + "/";
		//setTimeout( "window.location.href = http://" + username + ":" + password + "@" + directory + "/", 0 );
		//alert("http://" + username + ":" + password + "@" + directory);
		return false;
	});
	
	function resetForm() {
		$('#userName').val('');
		$('#userPassword').val('');
	}
	
}

