$(document).ready(function(){

/* GENERAL */

    $('#splashImage').cycle({
		fx: 'fade',
		speed:  3000,
		timeout: 15000 
	});

	//hide splashImage if window to small
	/*deactivated
	$(window).resize(function() {
		if($(window).height() < 775)
		{
			$("#splashImage").fadeOut(500);
			$("#splash").css("height", 95);
		}
		else
		{
			$("#splash").css("height", 410);
			$("#splashImage").fadeIn(500);
		}
	});
	*/
	
	//splashHide hover
	$("#splashHide").livequery(function()
		{
	        $(this).hover(function()
	        {
				$(this).find("img").attr("src", "interface/images/up_on.png");
            },
            function()
            {
				$(this).find("img").attr("src", "interface/images/up.png");
            }); 
    	},
    	function()
    	{
	        $(this).unbind('mouseover').unbind('mouseout');
	    }
	);

	//splashShow hover
	$("#splashShow").livequery(function()
		{
	        $(this).hover(function()
	        {
				$(this).find("img").attr("src", "interface/images/down_on.png");
            },
            function()
            {
				$(this).find("img").attr("src", "interface/images/down.png");
            }); 
    	},
    	function()
    	{
	        $(this).unbind('mouseover').unbind('mouseout');
	    }
	);
	
	//show/hide splashImage
    $("#splashHide, #splashShow").livequery('click', function(event)
    {
    	var clickedId = $(this).attr("id");
    	
		if(clickedId == "splashHide")
		{
			//write to session
			$.ajax({
				async: true,
				type: "GET",
				url: "hideShowSplash.php",
				data: "hideSplash=1",
				success: function(data){
					$("#splashImage").slideUp(500);
					$("#splash").css("height", 95);
					
					$("#splashHide").fadeOut(500);
					$("#splashShow").fadeIn(500);
				}
			});
		}
		else
		{
			//write to session
			$.ajax({
				async: true,
				type: "GET",
				url: "hideShowSplash.php",
				data: "hideSplash=0",
				success: function(data){
					$("#splash").css("height", 410);
					$("#splashImage").slideDown(500);
		
					$("#splashHide").fadeIn(500);
					$("#splashShow").fadeOut(500);
				}
			});
		}
		
		return false;
	});

	//bild vergroessern
	$(".zoom").click(function(event) {
		var url = $(this).attr('href');
		var offsetTop = $(this).find('input').attr('value');
		
		var data = '<img style="margin-top:'+offsetTop+'px;" src="'+url+'" />';
		
		$("#imageZoom").html(data).show('slow');
		
		return false;
	});
	
	$("#imageZoom").click(function() {
		$("#imageZoom").empty().hide('slow');
	});


/* GALLERY */

	//statistic
	$("aasdf").livequery('click', function(event)
    {
    	var clickedLink = $(this).attr("href");

		$.ajax({
			async: true,
			type: "POST",
			url: "/interface/php/statistic.php",
			data: "url="+clickedLink,
success: function(data) {
	alert(data);
}
		});
return false;
    });

	//up Image
	$("#upImage").livequery(function()
		{
	        $(this).hover(function()
	        {
				$(this).attr("src", "interface/images/up_on.jpg");
            },
            function()
            {
				$(this).attr("src", "interface/images/up.jpg");
            }); 
    	},
    	function()
    	{
	        $(this).unbind('mouseover').unbind('mouseout');
	    }
	);
	
	//hover over thumbnail-box
	$(".thumbnailBox").livequery(function()
		{
	        $(this).hover(function()
	        { 
				$(this).find(".imageOverlay").addClass("imageHover");
            },
            function()
            { 
				$(this).find(".imageOverlay").removeClass("imageHover");
            }); 
    	},
    	function()
    	{
	        $(this).unbind('mouseover').unbind('mouseout');
	    }
	);

	//hover over slide image
	$("#slideOverlayLeft, #slideOverlayRight").livequery(function()
		{
	        $(this).hover(function()
	        { 
				$(this).find('div.arrow').fadeIn('1500');
            },
            function()
            { 
				$(this).find('div.arrow').fadeOut('1500');
            }); 
    	},
    	function()
    	{
	        $(this).unbind('mouseover').unbind('mouseout');
	    }
	);
    
    //click on slide image loads nextImage
    $("#slideOverlayLeft, #slideOverlayRight").livequery('click', function(event)
    {
    	var clickedSide = $(this).attr("id");
    	
    	if(clickedSide == "slideOverlayRight")
    	{
    		var clickedLink = $("#nextImage").attr("href");
    	}
    	else
    	{
	    	var clickedLink = $("#prevImage").attr("href");
    	}
    	
    	if(clickedLink)
    	{
	    	var pathParts = clickedLink.split('&slide=');
			var realPath = pathParts[0].replace('?photos=', '');
			realPath = realPath.replace('?site=photos', '');
			if(pathParts[1])
			{
				var slide = pathParts[1];
			}
			else
			{
				var slide = "";
			}

			$.ajax({
				async: true,
				type: "POST",
				url: "loadSubmenu.php",
				data: "photos="+realPath+"&slide="+slide,
				success: function(data){
					$("#submenuBox").html(data);
				}
			});
	
			$.ajax({
				async: true,
				type: "POST",
				url: "loadContent.php",
				data: "photos="+realPath+"&slide="+slide,
				success: function(data){
					//hide content
					$("#content").css({ opacity: 0 });
					
					//load content
					$("#content").html(data);
					
					//hide image
					$("#slideImage").css({ opacity: 0 });
					
					//show content
					$("#content").fadeTo(0,1);
					
					//fade image in
					$("#slideImage").fadeTo(2000,1);


					//refresh socialImages
					if(pathParts[1])
					{
						var thumbPath = "fotos/"+realPath+"/thumbs/"+slide;
						var imageDesc = $('#slideDescription').html();
					}
					else
					{
						var thumbPath = "content/siteImage.jpg";
						var imageDesc = "Fotogalerie";
					}
			
					$('meta[property="og:image"]').attr("content", thumbPath);
					$('link[rel="image_src"]').attr("href", thumbPath);
		
					$('meta[property="og:title"]').attr("content", imageDesc);
				}
			});
			
			return false;
		}
		else
		{
			var upLink = $("#upLink").attr("href");
			if(upLink)
			{
				//show hidden elements
	    		$("#splash").slideDown('2000');
    			$("#menu").slideDown('1000');
    			$("#footer").show();

				var realPath = upLink.replace('?photos=', '');
				realPath = realPath.replace('?site=photos', '');
				$.ajax({
					async: true,
					type: "POST",
					url: "loadSubmenu.php",
					data: "photos="+realPath,
					success: function(data){
						$("#submenuBox").html(data);
					}
				});
		
				$.ajax({
					async: true,
					type: "POST",
					url: "loadContent.php",
					data: "photos="+realPath,
					success: function(data){
						$("#content").html(data);

						//refresh socialImages
						var thumbPath = "content/siteImage.jpg";
						$('meta[property="og:image"]').attr("content", thumbPath);
						$('link[rel="image_src"]').attr("href", thumbPath);
		
						var imageDesc = "Fotogalerie";
						$('meta[property="og:title"]').attr("content", imageDesc);
						
						//refresh footer share-link
						$.ajax({
							async: true,
							type: "POST",
							url: "loadFooterLink.php",
							data: "photos="+realPath,
							success: function(data){
								$("#facebookLink").attr("href", "http://www.facebook.com/sharer.php?u=http%3A%2F%2Fwww.simonschaller.ch%3Fphotos%3D"+data);
								$("#twitterLink").attr("href", "http://twitter.com/home?status="+imageDesc+"+-+http%3A%2F%2Fwww.simonschaller.ch%3Fphotos%3D"+data);
							}
						});
					}
				});
				
				return false;
			}
		}
	});
	
	//click on gallery-link
	$(".clickDir").livequery('click', function(event)
    {
    	//has class hideSplash? 
    	if($(this).hasClass('hideSplash'))
    	{
    		$("#splash").slideUp('2000');
    		$("#menu").slideUp('1000');
    		$("#footer").hide();
    	}
    	else
    	{
    		$("#splash").slideDown('2000');
    		$("#menu").slideDown('1000');
    		$("#footer").show();
    	}

    	if($(this).hasClass('fading'))
    	{
    		var fade = true;
    	}
    	
    	//get link like "?photos=PATH&slide=IMAGE"
    	var clickedLink = $(this).attr("href");
    	var pathParts = clickedLink.split('&slide=');
		var realPath = pathParts[0].replace('?photos=', '');
		realPath = realPath.replace('?site=photos', '');
		if(pathParts[1])
		{
			var slide = pathParts[1];
		}
		else
		{
			var slide = "";
		}

		$.ajax({
			async: true,
			type: "POST",
			url: "loadSubmenu.php",
			data: "photos="+realPath+"&slide="+slide,
			success: function(data){
				$("#submenuBox").html(data);
			}
		});

		$.ajax({
			async: true,
			type: "POST",
			url: "loadContent.php",
			data: "photos="+realPath+"&slide="+slide,
			success: function(data){
				if(fade)
				{
					//hide content
					$("#content").css({ opacity: 0 });
					
					//load content
					$("#content").html(data);
					
					//hide image
					$("#slideImage").css({ opacity: 0 });
					
					//show content
					$("#content").fadeTo(0,1);
					
					//fade image in
					$("#slideImage").fadeTo(2000,1);
				}
				else
				{
					$("#content").html(data);
				}

				//refresh socialImages
				if(pathParts[1])
				{
					var thumbPath = "fotos/"+realPath+"/thumbs/"+slide;
					var imageDesc = $('#slideDescription').html();
				}
				else
				{
					var thumbPath = "content/siteImage.jpg";
					var imageDesc = "Fotogalerie";

					//refresh footer share-link
					$.ajax({
						async: true,
						type: "POST",
						url: "loadFooterLink.php",
						data: "photos="+realPath,
						success: function(data){
							$("#facebookLink").attr("href", "http://www.facebook.com/sharer.php?u=http%3A%2F%2Fwww.simonschaller.ch%3Fphotos%3D"+data);
							$("#twitterLink").attr("href", "http://twitter.com/home?status="+imageDesc+"+-+http%3A%2F%2Fwww.simonschaller.ch%3Fphotos%3D"+data);
						}
					});
				}
				
				$('meta[property="og:image"]').attr("content", thumbPath);
				$('link[rel="image_src"]').attr("href", thumbPath);
				$('meta[property="og:title"]').attr("content", imageDesc);
			}
		});
		
		return false;
	});

	//overlay over movie, hide when clicked
	$(".clickToPlay").livequery('click', function(event)
		{
			$(".movieOverlay").slideDown('slow').empty();
		}
	);
	
	
/* ABOUT */


/* BLOG */


/* CONTACT */

    //Formular-Felder mit class='emptyInput' leeren bei fokus
    var orgValue = "";
    var newValue = "";
    
    $('.emptyInput').bind('focus', function()
    {
    	var thisName = $(this).attr("name");
        orgValue = $("[name="+thisName+"_hidden]").val();
        var actValue = $(this).val();
        
        if(actValue == orgValue)
        {
	        $(this).val('');
	    }
    });
    
    $('.emptyInput').bind('blur', function()
    {
        newValue = $(this).val();
        if (newValue == '')
        {
            $(this).val(orgValue);
        }
    });
	
});


function spamcheck_pure()
{
	var mensch_wert = $("#mensch").attr("alt");
	
	//unchecked -> checked
	if(mensch_wert == 'leer')
	{
		$("#contactForm").append('<input type="hidden" name="formPass" id="human" value="ja"/>');
		var tmp = $("#mensch").parent();
		$("#mensch").remove();
		tmp.append('<img src="interface/images/checkbox_on.jpg" width="10" height="10" id="mensch" alt="voll"/>');
	}
	//checked -> unchecked
	if(mensch_wert == 'voll')
	{
		$("#human").remove();
		var tmp = $("#mensch").parent();
		$("#mensch").remove();
		tmp.append('<img src="interface/images/checkbox.jpg" width="10" height="10" id="mensch" alt="leer"/>');
	}
}
