/*
 * © De Media Winkel 2011
 * I'm not a webdeveloper, so don't judge me on my code.
 * Especially you, Danny
*/

//debug
// function debugText(txt){
	// $('#debug').text(txt);
	// }

//eerste dingen
var presentdiv = "maintext";
var bgnr = 0;
var chapter = 0;
//vars portfolio
var aantalitems;
var topmarge=0;
var portfolioy=450;
var portfoliox;
var portfolioopen=false;
var activeproject;
var fotonr = 0;// actieve fotonr in portfolio
var fotoaantal; // aantal foto's
var fotoscroll = 915; //aantal pixels dat de foto scrollt
var vimeo_url = "x";
var scroll = 0;


function imageresize() {
	var widthtotal = $(document).width();
	var heighttotal = $(window).height();
	var lijnwidth = widthtotal-30 + "px";
	var boxleft = Math.round(widthtotal/2)+"px";
	$('#header_line').css('width', lijnwidth);
	$('#mainprev').css('left', boxleft);
	$('#maintext').css('top', Math.round(heighttotal/2-100)+"px");
	//debugText("width="+widthtotal+" height="+heighttotal);
	}
	
function caption(tekst){
	$('#fototext').html(tekst+" / &copy; 2011 DMW"); 
	};
// change background
function bgimg(file){
 	bgnr++; //increment
	$.backstretch("images/bg/"+file+".jpg", {speed: 300, bgnr: bgnr})
	}	

// beweging divs
function movePage(image,part,tekst,nr){
	if (presentdiv != part){
		var centre = $(window).width()/2;
		if(nr>chapter){
			chapto = -centre-$('#'+part+'').width();
			chapfrom = centre;
		} else {
			chapto = centre;
			chapfrom = -centre;
		}
		chapter=nr;
		$(".content").fadeOut(200);
		$('#'+presentdiv).animate({left:chapto}, 500, 'easeInSine',function() {
			// work laden
			if(part == "work"){
					laadxml();	
					$('#'+presentdiv).hide();
					$("#portfoliowrap").show();
					presentdiv = part;
					bgimg(image);
					caption(tekst);
			} else {
				// de rest laden
				if(presentdiv == "work"){
					sluitportfolio (200);
					$("#portfoliowrap").fadeOut('slow',function() {
						$("#portfoliowrap").hide() // portfolio weg
						.empty();
					});	
				}
				$('#'+presentdiv).hide();
				$('#'+part).show()
				.css("left", chapfrom+"px")
				.animate({left:-$('#'+part+'').width()}, 500, 'easeOutSine',function() {
					if(part == "contact") {
						$('#'+part+'content')
							.css("left", 5+"px")
							.fadeIn();
						}
					presentdiv = part;
					bgimg(image);
					caption(tekst);
	  			});
  			}
		});
	}
}

$(document).ready(function(){ 
	$('#head').delay(800).slideDown('easeOut');
	$('#maintext').delay(1200).fadeIn();
	
	imageresize();

	$(window).bind("resize", function(){
	 	imageresize();
	 	gridopbouw();
	 	}); 
	 	 
	// scroll
	 $(window).bind( "scroll", function(e) {
		scroll = $(window).scrollTop();
		var opacity=(100-scroll)/100;
		$('#head').css('opacity', opacity)
	});
	//basis vars
	bgimg("tron");
	caption("Data visualisation of random numbers");
	
	//navigatie boven
	$('#menu').each(function(){
		var $links = $('a', this);
		$links.click(function(){
			var $link = $(this);
			link = this;
			$links.removeClass('selected');
			$link.addClass('selected');
			})
	});
	// menu items los
	$('#menu_item_info').click(function(){
		movePage('paris4','info','la grande arche, paris','1');
		_trackEvent(menu, info);
	});
	$('#menu_item_works').click(function(){
		movePage('berlin1','work','Karl Marx Alee, Germany','2');
		_trackEvent(menu, work);
	});
	$('#menu_item_contact').click(function(){
		movePage('paris1','contact','Pont Neuf, paris','4');
		_trackEvent(menu, contact);
	});
	$('#menu_item_login').click(function(){
		movePage('paris2','login','Pont Neuf, paris','5');
		_trackEvent(menu, login);
	});
	// foto caption onder
	$('#fototextbox').hover(
	 	function (){
			$('#fototext').stop().animate({ bottom:'0px' },"slow")
		}, 
		function () {
			$('#fototext').stop().animate({ bottom:'-24px' },"slow")
		});
	// van portfolio
	$('#close').click(function(){
		sluitportfolio(1000);
	});
	//lower 3rd
	$('#omschrijving').hover(
	 	function (){
			$('#omschrijvingbody').stop().animate({ bottom:'0px' },"slow")
		}, 
		function () {
			$('#omschrijvingbody').stop().animate({ bottom:'-50px' },"slow")
		});
	//pijlen functie
	$('.pijlen').hover(
	 	function (){
	 		$(this).children().fadeIn();
		}, 
		function () {
			$(this).children().fadeOut();
		});
	$('#volgendefoto').click(function (){
 		next_image();
	}); 
	$('#vorigefoto').click(function (){
 		previous_image();
	}); 
	
	//login
	$("#login_form").submit(function(){
		$("#logincontent").children("#infotext").text('Validating....');
		$.post("ajax_login.php",{ 
			user_name:$('#username').val(),
			password:$('#password').val(),
			rand:Math.random() } ,
			function(data){
	  			if(data=='yes'){
		  				$("#logincontent").children("#infotext").text('Logging in.....').fadeIn(); 
			 			$.get("secure.php", function(data){
							$("#logincontent").children("#infotext").append(data);
							$("#login_form").hide();
							
						});
	  			} else {
		  				$("#logincontent").children("#infotext").text('incorrect login').fadeIn();		
      			}
    		});
		return false; //not to post the  form physically
	});
	
	$("#password").blur(function()
	{
		$("#login_form").trigger('submit');
	});
	
	// bullits
	$("#bullit0").click(function(){
		beweegimages(0);
		fotonr = 0;
	});
	$("#bullit1").click(function(){
		beweegimages(1);
		fotonr = 1;
	});
	$("#bullit2").click(function(){
		beweegimages(2);
		fotonr = 2;
	});
	$("#bullit3").click(function(){
		beweegimages(3);
		fotonr = 3;
	});
}); 

