/*
 *  Author: Barney Fox (http://www.barneyfox.com) on behalf of Karmarama
 *  Data: 07/03/2011
 *
 */

$(document).ready(function(){
	
	//ALL

	//initiate news ticker	
	$('#ticker').liScroll();


	//HOME
	//slideshow
	$('.slideshow:gt(0)').hide();
	
	setInterval(function()
	{
		$('.slideshow:first').fadeOut(400).next('.slideshow').fadeIn(400).end().appendTo('.slideshow-wrapper');
	}, 6000);

	//display random image
	//var numRand = Math.floor(Math.random()*($('#home-body .home-image-wrapper img').length) );
	//var randomImg = $('#home-body .home-image-wrapper').children()[numRand];
	//$(randomImg).show();
	//console.log($(randomImg).height());
	//$(randomImg).parent().css("height", $(randomImg).outerHeight());

    $("#home-body .home-image-wrapper img").load(function() {
        var imageHeight = $(this).height();
        $(this).parent().css("height",imageHeight);

    });


	//video show
	$('#home-body .example-videos li a').click(function() {

		var vidAddress = $(this).attr("rel");

		$('body > .overlay').fadeTo(300,0.85);
		$('body > .lightbox-style-player').fadeIn(300, function() {
			//var so = new SWFObject('player/player.swf','playerID','580','380','9');
			/*
var so = new SWFObject('player/player.swf','playerID','580','326','9');
		    so.addParam('allowfullscreen','true');
		    so.addParam('allowscriptaccess','always');
		    so.addVariable('file', vidAddress);
		    so.addVariable('autostart', '1');
		    so.addVariable('controlbar', 'over');
		    so.addVariable('controlbar.idlehide', 'true');
		    //so.addVariable('image', '/thumbs/bunny.jpg');
		    so.write('video-player');
*/

			jwplayer("video-player").setup({
		        flashplayer: "player/player.swf",
		        file: vidAddress,
		        width: 580,
		        height: 326,
		        controlbar: "over",
		        autostart: true,
		        'stretching': 'exactfit',
		    });
		    
		});
		$('body > #overlay-close').fadeIn(300);
	});

	//video hide
	$('body > .overlay, body > #overlay-close').click(function() {
		$('body > .overlay').fadeOut(300);
		$('body > .lightbox-style-player').fadeOut(300);
		$('body > #overlay-close').fadeOut(300);
	})

	//ABOUT US

	//scroll copy
	$('.about-subnav').click(function() { moveAboutCopy($(this).attr("rel")) })

	moveAboutCopy = function(index) {
		var distance = 0;
		for(var i = 0; i < $('#about-body #content .mask .copy').children().length; i++) {

			var section = $('#about-body #content .mask .copy .section:nth-child('+(i+1)+')');

			//add the height of all section above the chosen one together
			if(i < index)
			{
				distance += section.outerHeight();
				distance += parseFloat(section.css("margin-bottom"), 10); //remember to also add margins
			}
			//highlight title
			if(i == index)
				$(section).children("h3").addClass("selected");
			else
				$(section).children("h3").removeClass("selected");
		}
		//move copy upwards by the distance calculated above
		$('#about-body #content .mask .copy').animate({top: -1*distance}, 1000, 'easeInOutExpo');
	}

	//WORK CLIENT PAGE

	//swap placeholder for video
	$('.video-placeholder-link').click(function(){

		var vidAddress = $(this).attr("rel");
		
		jwplayer("video-player").setup({
	        flashplayer: "player/player.swf",
	        file: vidAddress,
	        width: 790,
	        height: 444,
	        controlbar: "over",
	        autostart: true,
	        'stretching': 'exactfit',
	    });

		/*
var so = new SWFObject('player/player.swf','playerID','794','444','9');
	    so.addParam('allowfullscreen','true');
	    so.addParam('allowscriptaccess','always');
	    so.addVariable('file', vidAddress);
	    so.addVariable('autostart', 'true');
	    so.addVariable('controlbar', 'over');
	    so.addVariable('controlbar.idlehide', 'true');
	    //so.addVariable('skin', 'player/KreamProjectPage.zip');
	    
	    so.write('video-player');
*/
	})
	

	//CONTACT

	//Swap picture and map
	$('#map-link').click(function() {
		$('#contact-body #contact-content #slider').toggle();
		$('#contact-body #contact-content #contact-map').toggle();
	})

});



$(window).load(function() {
    $('#slider').nivoSlider({
    	 effect: 'fade',
    	 directionNav: false,
    	 controlNav: false,
    	 pauseTime:5000
    });
});

