// SO GLAD YOU LOOKED HERE!
// COZ IT'S FRIDAY FRIDAY FRIDAY
// FUN FUN FUN




var winW;
var winH;
var portView = 0;
var numPorts = -1;

$(document).ready(function() {
	niceLayout();
	winW = $(window).width();
	winH = $(window).height();


	
});

$(window).load(function(){
	//niceLayout(); // Produces: Maximum call stack error in safari

});

$(document).ready(function() {
	
	$("a:visited > .article-over").fadeOut();
	
	$("#theImg ul li").click(function(e){	$("#pageNext").click();		});
	
	$("#theImg ul li:eq(0)").addClass('activeView');
	columnify();
	
	// $("#mainInfo").mailto();
	// $("#mainInfo").tweet();

	$(window).resize(function() {
		niceLayout($("#theImg").width());
		
	});
	
	$("#theImg ul li").each(function(index, gall){
		numPorts++;
		if (index == 0){
			$(".pageBullet").html("<ul></ul>");
			$(".pageBullet ul").append('<li id="'+index+'">&#9679;</li>');
		} else {
			$(".pageBullet ul").append('<li id="'+index+'">&#9675;</li>');
		} 
		
	});
	
	$(".pageBullet ul li").click(function(e){
		e.preventDefault();
		stopVideoz();
		var aktivVy = $(".activeView").clone();
		
		viewIndex = parseInt($(this).attr('id'));
		//aktivVy.clone().insertAfter(aktivVy).detach();
		
		$("#theImg").scrollTo($("#theImg ul li:eq("+viewIndex+")"), 800);
		aktivVy.remove().insertAfter(aktivVy);
		//$(".activeView").detach();
		$("#theImg ul li").removeClass('activeView');
		$("#theImg ul li:eq("+viewIndex+")").addClass('activeView')
		
		portView = viewIndex;
		
		
		
		$(".pageBullet ul li").each(function(){
			$(this).html('&#9675;')
		});
		$(this).html('&#9679;');
	});
	
	// Remove Navigation if only 1 gallery pic is showing
	if (numPorts <= 0) {
			$("#pagePrev").hide();
			$("#pageNext").hide();
			$(".pageBullet").hide();
			$("#main").css({'padding-bottom': '20px'});
			
		}
	
	
	// Navigate portfolioView
	$("#pageNext").click(function(){portViewChange(1,800);});
	$("#pagePrev").click(function(){portViewChange(0,800);});
	// $("#theImg").swipe({
	// 	     swipeLeft: function() { portViewChange(1,100); },
	// 	     swipeRight: function() { $("#pagePrev").click() },
	// 	 });
	$("body").keyup(function(event) {
		if (event.keyCode == '39') {
				portViewChange(1,800);
		} else if (event.keyCode == '37') {
			// if left arrow pressed, go back
				portViewChange(0,800);
		} else {}
	});
	
	// Removes the borders if there is no fact box
	$('#fakta').filter(function() { 
			return $.trim($(this).text()) === ''
	    }).remove();
		
	$(window).konami({	
				cheat: function() {
						$("head").append($("<link rel='stylesheet' href='/3d.css' type='text/css' media='screen' />"));
				}
			});
	
		

});


function columnify(){
	// MAKE COLUMN
			jQuery('.mcol').makeacolumnlists({cols: 3, colWidth: 305, equalHeight: 'ul', startN: 0});
		//$("body").hide();
		// if (winW < 1264) {
		// 			
		// 			jQuery('.mcol').makeacolumnlists({cols: 3, colWidth: 305, equalHeight: 'ul', startN: 0});
		// 			$("#li_container1").width(960);
		// 			$("body").show();
		// 		} else {
		// 			// jQuery('.mcol').makeacolumnlists({cols: 4, colWidth: 305, equalHeight: 'ul', startN: 0});
		// 			// 	$("#li_container1").width(1265);
		// 			//	$("body").show();
		// 		}
}

function stopVideoz(){
	$(".activeView iframe").delay(8000).each(function() {
	
		this.src=this.src;
		//this.contentWindow.location.reload(true);
		
	});
}

function portViewChange(dir,speed){
	niceLayout();
//	$(".activeView").clone(true).insertBefore(".activeView");
	stopVideoz();
	switch(dir){
		case 0:
			if (portView == 0){ 
				$("#theImg").scrollTo($("#theImg ul li:last-child"), 400);  
				
				$("#theImg ul li:eq("+portView+")").removeClass('activeView');
				$(".pageBullet ul li:eq("+portView+")").html('');
				$(".pageBullet ul li:eq("+portView+")").html('&#9675;');
				portView = numPorts;
				
				$(".pageBullet ul li:eq("+portView+")").html('');
				$(".pageBullet ul li:eq("+portView+")").html('&#9679;');
			} 
			else {
		
				$("#theImg ul li:eq("+portView+")").removeClass('activeView');
				$(".pageBullet ul li:eq("+portView+")").html('');
				$(".pageBullet ul li:eq("+portView+")").html('&#9675;');
				portView--;
				$("#theImg ul li:eq("+portView+")").addClass('activeView');
				// $("#theImg ul li:eq("+portView+")").show();
				$("#theImg").scrollTo($("#theImg ul li:eq("+portView+")"), speed);
				$(".pageBullet ul li:eq("+portView+")").html('&#9679;');
			}
		break;
		case 1:
			if (portView == numPorts) { 
				$("#theImg").scrollTo($("#theImg ul li:first-child"), 400);  
			
				$("#theImg ul li:eq("+portView+")").removeClass('activeView');
				$(".pageBullet ul li:eq("+portView+")").html('');
				$(".pageBullet ul li:eq("+portView+")").html('&#9675;');
				portView = 0;
				$("#theImg ul li:eq("+portView+")").addClass('activeView');
				$(".pageBullet ul li:eq("+portView+")").html('');
				$(".pageBullet ul li:eq("+portView+")").html('&#9679;');
			}
			else {
		
				$("#theImg ul li:eq("+portView+")").removeClass('activeView');
				$(".pageBullet ul li:eq("+portView+")").html('');
				$(".pageBullet ul li:eq("+portView+")").html('&#9675;');
				portView++;
				$("#theImg ul li:eq("+portView+")").addClass('activeView');
				// $("#theImg ul li:eq("+portView+")").show();
				$("#theImg").scrollTo($("#theImg ul li:eq("+portView+")"), speed);
				$(".pageBullet ul li:eq("+portView+")").html('');
				$(".pageBullet ul li:eq("+portView+")").html('&#9679;');
			}
		break;
	}

}

function landPortrait(){
	
	winRatio = $(window).width()/$(window).height();

	if (winRatio > 1.100){
	$("#perspektiv").attr({
		"href": "http://www.volontaire.se/detnyasvarta/wp-content/themes/volontaire/landscape.css"
		}); 
		niceLayout($("#theImg").width()*.7)
		

			
	} else {
		$("#perspektiv").attr({
			"href": "http://www.volontaire.se/detnyasvarta/wp-content/themes/volontaire/portrait.css"
		});
		niceLayout($("#theImg").width())
	

	}
		
}

function setTitle(titleToSet) {
	if (titleToSet == null) {
		document.title = "Volontaire";
	} else {
		document.title = titleToSet + " | Volontaire";
	}
}

// Gets the pic and the text flowing. 
function niceLayout(vidderna){

	winW 		= $(window).width();					
	winH 		= $(window).height();
 	ticWidth 	= parseFloat($("#theImg").width());
	ticHeight 	= parseFloat($("#theImg").height());
	imgWidth	= $("#theImg ul li img").width();
	imgHeight	= $("#theImg ul li img").height();

	 widHeight	= Math.round((ticWidth/16)*9);
	
	// give the main some bottom space
	//$("#main").height(winH-120);


					
	// scale the li proportionally afte the width of #theImg
	//$("#theImg ul li").aeImageResize({ width:theWidth });
	// scale the child of the first li proportionally after the width of #theImg
	
//	$("#theImg").css({'float': [winW<winH ? 'none' : 'left']});
//	$(".page .article").css({'float': [winW<winH ? 'none' : 'right']});


	// put height to parents height
	$(".theInfo").height($(this).parent().height());

	// set width of ul to number of kids
	$("#theImg ul").width(ticWidth*$(this).children());
	
	//$("#theImg ul li img")[ticWidth > ticHeight ? 'width' : 'height'](200);
	
	$("#theImg ul li img").aeImageResize({ width:ticWidth });
	// scale all the iframes after #theImg
	$("#theImg ul li iframe").attr("width", ticWidth)
	$("#theImg ul li iframe").attr("height", widHeight);
	
	
	
	
	// scale font with that, 52 and 34 are arbitrary numbers	
	// $(".theInfo .article").css({'font-size': ticWidth/42});
	// $(".article H1").css({'font-size': ticWidth/42});
	
	


		// $("#pageTitle").width($(".article").width());
 
	
	if (portView > 0) {
		$("#theImg").scrollTo($("#theImg ul li:eq("+portView+")"));
	}
}
	


// KONAMI LULZ
(function($){$.fn.konami=function(options){var opts=$.extend({},$.fn.konami.defaults,options);return this.each(function(){var masterKey=[38,38,40,40,37,39,37,39,66,65];var controllerCode=[];$(window).keyup(function(evt){var code=evt.keyCode?evt.keyCode:evt.which;controllerCode.push(code);if(controllerCode.length===10){var bIsValid=true;for(var i=0,l=masterKey.length;i<l;i++){if(masterKey[i]!==controllerCode[i]){bIsValid=false;}}
if(bIsValid){opts.cheat();}
controllerCode=[];}});});};$.fn.konami.defaults={cheat:null};})(jQuery);

jQuery.fn.highlight = function (text, o) {
	return this.each( function(){
		var replace = o || '<span class="highlight">$1</span>';
		$(this).html( $(this).html().replace( new RegExp('('+text+'(?![\\w\\s?&.\\/;#~%"=-]*>))', "ig"), replace) );
	});
}

jQuery.fn.autolink = function () {
	return this.each( function(){
		var re = /((http|https|ftp):\/\/[\w?=&.\/-;#~%-]+(?![\w\s?&.\/;#~%"=-]*>))/g;
		$(this).html( $(this).html().replace(re, '<a href="$1">$1</a> ') );
	});
}

jQuery.fn.tweet = function(){
	return this.each( function(){
		var re = /([\.|\,|\:|\¡|\¿|\>|\{|\(]?)@{1}(\w*)([\.|\,|\:|\!|\?|\>|\}|\)]?)\s/i;
		$(this).html( $(this).html().replace(re, '<a href="http://twitter.com/$1" class="tweet-username">$1</a>'));
	});
}

jQuery.fn.mailto = function () {
	return this.each( function() {
		var re = /(([a-z0-9*._+]){1,}\@(([a-z0-9]+[-]?){1,}[a-z0-9]+\.){1,}([a-z]{2,4}|museum)(?![\w\s?&.\/;#~%"=-]*>))/g
		$(this).html( $(this).html().replace( re, '<a href="mailto:$1">$1</a>' ) );
	});
}



