$(document).ready(function(){
	
	/**
	 * Position of video-wrapper
	 */
	windowHeight = $(document).height();
	windowWidth = $(document).width();
	videoHeight = $("#video-wrapper").height() / 2;
	videoWidth = $("#video-wrapper").width();
		
	$("#video-wrapper")
		.css("position", "absolute")
		.css("top", (windowHeight/2)-(videoHeight/2)+"px")
		.css("left", (windowWidth/2)-(videoWidth/2)+"px");
		
	/**
	 * Close the video overlay
	 */
	if ($("#overlay").length) {
		$("#overlay .close a").click(function() {
			$(this).parents("#overlay").fadeOut("slow", function() {
				$("#overlay").remove();
			});
		});

		$("#nav a").click(function() {
			$("#overlay").fadeOut("slow", function() {
				$("#overlay").remove();
			});
		});
	}

	/**
	 * Play the video
	 */
	initFlowPlayer("player", ctx + "/docroot/lavino/videos/New_Lancia_Range_Luxury_Liberated_YouTube_1_.flv");

	$("#nav ul ul").css("display", "none");
	$("#nav ul ul").css("position", "absolute");

	//ie background
	if ($.browser.msie) {
		ie_background(bgImg);
	}
	
	if ($("#content").children().size() == 0) {
		$("#content").hide();
	}
	
	content_width();
	
	if (!$('.mgnlMainbar').length) {
		
		news_nav();
		
		//ajax
		$("#nav a").click(function() {
			
			//remove elements from slideshow
			$(".slideshow-image").remove();
			$("#slideshow-bg").remove();
			$(".sl-nav").remove();
			
			var link = $(this).parent().find("a");
			var classLink = $(this).parent().find("a:first");
			var listItem = link.parents("li:first");
			var attr = link.attr("href");
			
			$("#nav a").blur();
			
			$("#content").load(attr + "?ajax=" + Date.parse(new Date()), function() {
				
				if ($("#content").children().size() > 0) {
					$("#content").show();
				}
				
				//ie background
				if ($.browser.msie) {
					ie_background(bgImg);
				}
				
				news_nav();
				content_width();
				
				//show navigation
				navigation(classLink, link, listItem);
			});
			
			return false;
		});
	}
	else {
		$("#nav ul ul").css("display", "block");
	}
});

/**
 * Initialize flowplayer
 * @param id {String} element id of player
 * @param video {String} Name of video
 */
function initFlowPlayer(id, video) {
	// mp4 file isn't working properly in ff and chrome
	//if ($.browser.msie) {
	if (navigator.userAgent.toLowerCase().indexOf("safari") == -1) {
		$("#" + id).hide();
		flowplayer(
			id + "_flow",
			{ src: ctx + "/docroot/lavino/js/flowplayer/flowplayer-3.2.4.swf", wmode: 'transparent' }, 
			{
				clip : {
					autoPlay: true,
					autoBuffering: true
				},
				playlist: [
					{ url: video }
				],
				plugins: {
					controls: {
						url: ctx + "/docroot/lavino/js/flowplayer/flowplayer.controls-3.2.2.swf",
						playlist: false,
						backgroundColor: '#000000',
						tooltips: {
							buttons: true,
							fullscreen: 'Enter Fullscreen mode'
						}
					}
				}
			}
		);
	}
	else {
		$("#" + id + "_flow").hide();
		$("#" + id).VideoJS();
		$("#" + id)[0].player.play();
	}
}

/**
 * Internet Explorer background
 */
var ie_background = function(bgImg) {
	$("body").css("background", "none");
	$("#bg-img").remove();
	$('<img src="' + bgImg +'" alt="" id="bg-img"/>').insertBefore('#nav-wrapper').show();
}

/**
 * Navigation
 */
var navigation = function(classLink, link, listItem) {
	
	//navigation
	$('#nav a').removeClass('current');
	$('#nav .open').removeClass('current');
	
	classLink.addClass("current");
	
	if (link.parents().hasClass('level1')) {
		link.parents('.open').addClass("current");
	}
	else {
		$('#nav ul ul').hide();
	}
	
	if (listItem.children().size() > 1 ) {
		$('#nav ul ul').hide();
		link.parents('.open').addClass("current");
		listItem.children().show();
	}

}

/**
 * News ajax
 */
var news_nav = function() {
	$(".headline a").click(function() {
		
		var $this = $(this);
		var href = $this.attr("href");
		
		$("#content").load(href + "?ajax=" + Date.parse(new Date()), function() {
		
			if ($("#content").children().size() > 0) {
				$("#content").show();
			}
			
			//ie background
			if ($.browser.msie) {
				ie_background(bgImg);
			}
			
			content_width();
		
		});
		
		return false;
	});
}







/**
 * Formvalidation
 */
var formvalidate = function(obj) {
	
	var $this = $(obj);
	var valid = true;
	
	$this.find(".notempty").each(function() {
		if ($.trim($(this).val()) == "") {
			$(this).addClass("error");
			valid = false;
		}
		else {
			$(this).removeClass("error");
		}
	});
	
	$this.find('.email').each(function() {
		if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test($(this).val())) {
			$(this).removeClass('error');
		}
		else {
			$(this).addClass('error');
			valid = false;
		}
	});
	
	//send the form with ajax
	if (valid == true) {
		
		var testdrive = $("#testdrive").val();
		var catalog = $("#catalog").val();
		var name = $("#name").val();
		var surname = $("#surname").val();
		var tel = $("#tel").val();
		var email = $("#email").val();
		var street = $("#street").val();
		var zip = $("#zip").val();
		var place = $("#place").val();
		var country = $("#country").val();
		var message = $("#message").val();
		var check_send = $("#check-send").val();
		
		var data_string = 'testdrive=' + testdrive + '&catalog=' + catalog + '&name=' + name + '&surname=' + surname + '&tel=' + tel + '&email=' + email + '&street=' + street + '&zip=' + zip + '&place=' + place + '&country=' + country + '&message=' + message + '&form=' + check_send;
		
		$.ajax({
			type: "POST",
			url: ctx + "/kontakt.html?ajax=" + Date.parse( new Date() ),
			data: data_string,
			success: function() {
				$(".contactform form").hide();
				$(".contactform").html('<h3>' + success_title + '</h3> <p>' + success_text + '</p>');
			}
		});
	}
	return false;
}

var content_width = function() {
	//content width
	var win_width = $(window).width();
	var con_width = win_width - 335;
	
	$("#content").css("width", con_width+"px");
	
	$(window).resize(function() {
		var win_width = $(window).width();
		var con_width = win_width - 335;
		
		$("#content").css("width", con_width+"px");
	});
}

