/* ================================ +
		 	  LIEVESTRO
 + ================================ */



/* 
** Made by: Thomas Lievestro
** Email:	info@lievestro.com
** Website:	www.lievestro.com
*/


/* ================================ +
              GENERAL
 + ================================ */

var lang = "en";
var busy = false;

    
var OS ;
var browser;
var version;


 

function initCycleElements(){
	elements = $(".portfolio_item_img");
	
	for(i=0; i < elements.length; i++){
		$(elements[i]).cycle({
		    timeout:       	450,  
		    speed:       	1000,
		    delay:			-300,
		    slideExpr: 		'img'
		});
	
		$(elements[i]).cycle('pause');
	}
}

function resetCycleElements(element){
	
	for(i=0; i < elements.length; i++){
		if(element[0] != elements[i]){
			$(elements[i]).cycle('pause');
			$(elements[i]).cycle(0);
			$(elements[i]).stop().animate({ marginTop: "0px"}, 300);
		}
	}
}








function displayLightbox(href){			

	_gaq.push(['_trackPageview',href]);
	
	$('#overlay_bg_container').fadeIn(700);									//Fade de overlay background in	
 
	height = $('#wrapper').height();
	$('#overlay_bg_container').css('height',height);
	
	var href = href.split('/');
	
	if($('#nav a:first').html() == 'nederlands'){
		lang = "en";
		var title = href[2];
		var id = href[3];
		$('#nav a:first').attr('href', "portfolio/"+title+"/"+id+"");
	}else{
		var title = href[1];
		var id = href[2];
		$('#nav a:first').attr('href', "en/portfolio/"+title+"/"+id+"");
	}
	
	$.ajax({
		type: "POST",
		url: "ajax/get_item_details.php",
		data: "id="+id+"&lang="+lang+"",
		success: function(data){
		
			$('#overlay_portfoliodetail').html(data);
			Cufon.refresh('#portfoliodetail_content h1');
			Cufon.refresh('h2');
			Cufon.refresh('h3');
			if(getBrowserSize()[0] < 479){
				window.setTimeout("initIphoneSlider()", 700);	//Fade de overlay in
			}
			
			// thumb item
			$(".portfoliodetail_thumb")
			.click(function(){
				goTo(this);
				return false;
			})
			
			$('#close_lightbox')
			.click(function(){
				window.location.hash = '#';
				hideLightbox();
				return false;
			});

   			$.scrollTo('body', 500);
		}
	});
	
	
    					
	window.setTimeout("$('#overlay_portfoliodetail').fadeIn(1000)", 700);	//Fade de overlay in

}

function hideLightbox(){

	_gaq.push(['_trackEvent','button','close_lightbox']);
	
	$('#overlay_portfoliodetail').fadeOut();			
	window.setTimeout(function(){
		$('#overlay_bg_container').fadeOut();
		$.scrollTo('body', 500);
		
	}, 400);
	
	if($('#close_lightbox.refresh').length != 0){
		href = $('#close_lightbox').attr('href');
		window.setTimeout(function(){ 
			window.location.href = href;
		}, 1000);
	}
	
	if($('#nav a:first').html() == 'nederlands'){
		lang = "en";
		$('#nav a:first').attr('href', "/");
	}else{
		$('#nav a:first').attr('href', "en/");
	}
} 



function goTo(item){

	//check if goTo is busy
	if(busy == false && $(item).find('.active').length == 0){
		//set busy = true
		busy = true;
		//remove active class from active navigation_item
		$('.portfoliodetail_thumb img.active').removeClass('active');
		//add active class to new active navigation item
		$(item).find('img:first').addClass('active');

		//get file type
		var type = $(item).attr('class').replace('portfoliodetail_thumb ','');
		
		//get src from image of navigation item
		var src = $('.portfoliodetail_thumb img.active').attr('src');
		//split src to array
		var name = src.split("/");
		
		_gaq.push(['_trackEvent','image',src]);
		
		//if file type is a movie
		if(type == 'movie'){
			//get href from movie of navigation item
			var href = $(item).attr('href');
			//split href to array
			var name_movie = href.split("/");
		}	
	 
		
		var imageObj = new Image();		
		//fade out old image in 0.1 seconden
		$('#portfoliodetail_big .file_holder:first').animate({
			opacity: 0.0
		}, 100, function(){
		
			//show loader
			var loaderTimeout = window.setTimeout(function(){
				$('#loader').show();
			},1000);
			//If image is loaded
			imageObj.onload = function() {  
				
				if(type == 'image'){
					//add new big_view image (opacity:0) now the real big image
					$('#portfoliodetail_big .file_holder:first')
					.after(''+
						'<div class="file_holder" style="opacity:0;"> '+
							'<img src="http://www.lievestro.com/footage/img/591x/'+name[6]+'" />'+
						'</div>'
					
					);
				}else if(type == 'movie'){
					//add new big_view movie (opacity:0) now the real big movie
					$('#portfoliodetail_big .file_holder:first')
					.after(''+
						'<div class="file_holder" style="opacity:0;"> '+
							'<script type="text/javascript">'+
								'file = "http://www.lievestro.com/footage/movie/'+name_movie[2]+'"; '+
								'image = "footage/img/591x/'+name[6]+'"; '+
								''+
								'var flashvars = { '+
								  'file: file, '+
								  'image: image '+
								'}; '+
								'var params = { '+
								  'movie: "flash/player-viral.swf", '+
								  'allowfullscreen: "true", '+
								  'allowscriptaccess: "always" '+
								'}; '+
								''+
								'swfobject.embedSWF("flash/player-viral.swf", "alternative_content", "591", "355", "9", "expressInstall.swf", flashvars, params); '+
							'</script> '+
							''+
							'<div id="alternative_content"> '+
								'<h1>Alternative content</h1> '+
								'<p><a href="http://www.adobe.com/go/getflashplayer"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" /></a></p> '+
							'</div>'+
						'</div>'
					
					);
				}
				
				//clear timeout and hide loader 
				window.clearTimeout(loaderTimeout);
				$('#loader').hide();
				
				//fade in new image in 1 seconden
				$('#portfoliodetail_big .file_holder:last').animate({
					opacity: 1.0
				}, 1000, function(){ 
					//remove old image
					$('#portfoliodetail_big .file_holder:first').remove();
									
					//set busy = true
					busy = false;
				});
			} 
								
			imageObj.src = "http://www.lievestro.com/footage/img/591x/"+name[6];  
		});
	}
}



function initIphoneSlider(){
	_gaq.push(['_trackEvent','IphoneSlider','init']);

	$('.portfoliodetail_thumb img').each(function(index) {
		var src = $(this).attr('src');
		var name = src.split("/");
		$(this).attr('src','http://www.lievestro.com/footage/img/591x/'+name[6]+'');
	});

	$('#portfoliodetail_thumbs').iphoneSlide({
		 handler: "#paging",
		 pageHandler: ".portfoliodetail_thumb",
		 bounce: false
	   
	});
	
}


function removeIphoneSlider(){

	$('.portfoliodetail_thumb img').each(function(index) {
		var src = $(this).attr('src');
		var name = src.split("/");
		$(this).attr('src','http://www.lievestro.com/footage/img/80x55/'+name[6]+'');
	});

	$('#portfoliodetail_thumbs').unbind().removeAttr('style');
	$('#paging').unbind(".jqiphoneslide").removeAttr('style');
	
}







/* ================================ +
           SKILFUL FUNCTIONS
 + ================================ */

var OS;
var browser;
var version;

var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{
			string: navigator.userAgent,
			subString: "Chrome",
			identity: "Chrome"
		},
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari",
			versionSearch: "Version"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();



function getBrowserSize() {
          
    var theWidth, theHeight;
  // Window dimensions: 
  if (window.innerWidth) {
  theWidth=window.innerWidth;
  }
  else if (document.documentElement && document.documentElement.clientWidth) {
  theWidth=document.documentElement.clientWidth;
  }
  else if (document.body) {
  theWidth=document.body.clientWidth;
  }
  if (window.innerHeight) {
  theHeight=window.innerHeight;
  }
  else if (document.documentElement && document.documentElement.clientHeight) {
  theHeight=document.documentElement.clientHeight;
  }
  else if (document.body) {
  theHeight=document.body.clientHeight;
  }

  return [theWidth,theHeight];
}

function getPageSize() {
          
    var xScroll, yScroll;
  
  if((OS == "Windows") && (browser == "Explorer") && (version == "7")){     
    xScroll = document.body.parentNode.scrollWidth;
    yScroll = document.body.parentNode.scrollHeight;
    }else{ 
    if (window.innerHeight && window.scrollMaxY) {  
      xScroll = window.innerWidth + window.scrollMaxX;
      yScroll = window.innerHeight + window.scrollMaxY;
    } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
      xScroll = document.body.scrollWidth;
      yScroll = document.body.scrollHeight;
    } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
      xScroll = document.body.offsetWidth;
      yScroll = document.body.offsetHeight;
    }
  }
  
  var windowWidth, windowHeight;
  
  var windowWidth, windowHeight;
  if (self.innerHeight) { // all except Explorer
    windowWidth = self.innerWidth;
    windowHeight = self.innerHeight;
  } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
    windowWidth = document.documentElement.clientWidth;
    windowHeight = document.documentElement.clientHeight;
  } else if (document.body) { // other Explorers
    windowWidth = document.body.clientWidth;
    windowHeight = document.body.clientHeight;
  } 

  
  
  // for small pages with total height less then height of the viewport
  if(yScroll < windowHeight){
    pageHeight = windowHeight;
  } else { 
    pageHeight = yScroll;
  }

  // for small pages with total width less then width of the viewport
  if(xScroll < windowWidth){  
    pageWidth = xScroll;    
  } else {
    pageWidth = windowWidth;
  }

  return [pageWidth,pageHeight];
}













/* parse the Jquery functions after document ready */
$(document).ready(function(){
	
    OS = BrowserDetect.OS;
	browser = BrowserDetect.browser;
	version = BrowserDetect.version;
	set_950 = new Array(3,7,11,15,19,23,27,31,35,39,43,47,51);
	set_712 = new Array(2,5,8,11,14,17,20,23,26,29,32,35,38,41,44,47,50,53);
	set_475 = new Array(1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35,37,39,41,43,45,47,49,51);
	set_239 = new Array(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51);

	
	initCycleElements();

	
	/*cufon*/
	Cufon.replace('#portfoliodetail_content h1');
	Cufon.replace('h2');
	Cufon.replace('h3');
	
	
	
	$.address.change(function(event){
		//console.log(window.location.hash);
	})

	if($('#nav a:first').html() == 'nederlands'){
		lang = "en";
	}else{
		lang = "nl";
	}

	var contact_details = ''
	+'<div class="contact_details">'
		+'<h3>lievestro</h3>'
		+'<br />'
		+'<br />'
		+'<table>'
			+'<tr>'
				+'<td class="columnleft">mail</td> <td><a tabindex="11" href="&#109;&#97;&#105;&#108;&#116;&#111;&#58;info@lievestro.com">info@lievestro.com</a></td>'
			+'</tr>'
			+'<tr>'
				+'<td class="columnleft">tel</td> <td>+316 412 06 231</td>'
			+'</tr>'
			+'<tr>'
				+'<td>&nbsp;</td><td> </td>'
			+'</tr>'
			+'<tr>'
				+'<td class="columnleft">btw</td> <td>NL1173.99.449.B01</td>'
			+'</tr>'
			+'<tr>'
				+'<td class="columnleft">kvk</td> <td>343.26.222</td>'
			+'</tr>'
		+'</table>'
	+'</div>'
	+'<div class="contact_eastern_egg">'
	+'</div>'

	
	$('#contact_item').html(contact_details);
	
	
	
	
	/* MOUSE EVENTS AND CLICKS */
	
	

	$('.more').toggle(function() {
		_gaq.push(['_trackEvent','button','show_more']);
		$('#info').stop().animate({ height: '+=280'}, 300);
		$(this).html('&laquo; Lees minder');
		return false;
	}, function() {
		_gaq.push(['_trackEvent','button','hide_more']);
		$('#info').stop().animate({ height: '-=280'}, 300);
		$(this).html('Lees meer &raquo;');
		return false;
	});
	
	

	$('.portfolio_item')
	.mouseover(function(){
		
		element = $(this).find(".portfolio_item_img");
		
		resetCycleElements(element);
	
		$(element).cycle('resume');
		$(element).stop().animate({ marginTop: "-100px"}, 300);
		
		var allPrevious = $(this).prevAll();
		
		if(getBrowserSize()[0] > 950 + 24){	 
			var	arr = set_950;
		}else if(getBrowserSize()[0] > 712 + 24){
			var	arr = set_712;
		}else if(getBrowserSize()[0] > 479){
			var	arr = set_475;
		}else{
			var	arr = set_239;
		}

		
		for(i=0; i < allPrevious.length; i++){
			if(jQuery.inArray(i, arr) != -1){
				if($(allPrevious[i]).length > 0){
				
					$(allPrevious[i]).find(".portfolio_item_img").stop().animate({ marginTop: "-100px"}, 300);
				}else{
					break;
				}
			}
		}
	})
	.mouseout(function(){ 
		element = this;
		$(element).find(".portfolio_item_img").stop().animate({ marginTop: "0px"}, 300, function(){
			setTimeout(function(){
			$(element).find(".portfolio_item_img").cycle('pause').stop();
			$(element).find(".portfolio_item_img").cycle(0);
			},10);
		});
		
		var allPrevious = $(this).prevAll();
		
		if(getBrowserSize()[0] > 950 + 24){	 
			var	arr = set_950;
		}else if(getBrowserSize()[0] > 712 + 24){
			var	arr = set_712;
		}else if(getBrowserSize()[0] > 479){
			var	arr = set_475;
		}else{
			var	arr = set_239;
		}
		
		for(i=0; i < allPrevious.length; i++){
			if(jQuery.inArray(i, arr) != -1){
				if($(allPrevious[i]).length > 0){
					$(allPrevious[i]).find(".portfolio_item_img").stop().animate({ marginTop: "-0"}, 300);
				}else{
					break;
				}
			}
		}
	})
	.click(function(){
		var href = $(this).find('a').attr('href');
		window.location.hash = '#/'+href;
		displayLightbox(href);
		return false;
	});
	
	
	$('.portfolio_item_content')
	.click(function(){
		var href = $(this).parent().find('a').attr('href');
		window.location.hash = '#/'+href;
		displayLightbox(href);
		return false;
	});
		
	$('.portfolio_item a')
	.click(function(){
		var href = $(this).attr('href');
		$.address.value(href);  
	//	window.location.hash = '#/'+href;
		displayLightbox(href);
		return false;
	});
	
	
		
	$('#close_lightbox')
	.click(function(){
		window.location.hash = '#';
		hideLightbox();
		return false;
	});

	
	$(".portfoliodetail_thumb")
	.click(function(){
		goTo(this);
		return false;
	})	
	
	
	$("#contact_button")
	.click(function(){

		_gaq.push(['_trackEvent','button','contact']);

		if($('#overlay_portfoliodetail:visible')){
			$('#overlay_portfoliodetail').fadeOut();			
			window.setTimeout(function(){
				$('#overlay_bg_container').fadeOut();				
			}, 400);
			setTimeout(function(){
				$.scrollTo( '#contact_item', 1500);		
				setTimeout(function(){
					$('.contact_details').effect('highlight', {color:"#666666"}, 3000); 
				},500);
			},1000);
		}else{
			$.scrollTo( '#contact_item', 1500);		
			setTimeout(function(){
				$('.contact_details').effect('highlight', {color:"#666666"}, 3000); 
			},500);
		}
		if($('#nav a:first').html() == 'nederlands'){
			lang = "en";
			$('#nav a:first').attr('href', "/");
		}else{
			$('#nav a:first').attr('href', "en/");
		}
		return false;
	})	
	
			
	height = $('#wrapper').height();
	$('#overlay_bg_container').css('height',height);
	
	
	if(getBrowserSize()[0] < 479){
		initIphoneSlider();		
	}
	
	$(window).resize(function() {
		if(getBrowserSize()[0] < 479){
			initIphoneSlider();		
		}else{
			removeIphoneSlider();
		}
	});
	
 });
