$j = jQuery.noConflict();
var currentCRPos = 0;
var crLib = Array();
var autonomousID = 0;

crLib.push({image:'http://www.screenplay.no/wordpress/wp-content/themes/screenplay/img/casereel_onda.jpg',title:'Onda Pal&Ness',subTitle:'&nbsp;|&nbsp;&nbsp;Profil',link:"http://www.screenplay.no/case/onda",titleColor:'#2f3438',size:'170px'});
crLib.push({image:'http://www.screenplay.no/wordpress/wp-content/themes/screenplay/img/casereel_image7.jpg',title:'Hafslund',subTitle:'&nbsp;|&nbsp;&nbsp;Hurra torpedo velger elektrisk',link:"http://www.screenplay.no/case/hurra-elektrisk",titleColor:'#003a94',size:'109px'});
crLib.push({image:'http://www.screenplay.no/wordpress/wp-content/themes/screenplay/img/casereel_image6.jpg',title:'IKEA',subTitle:'&nbsp;|&nbsp;&nbsp;Folkets IKEA-katalog',link:"http://www.screenplay.no/case/folkets-ikea-katalog",titleColor:'#5d5d5d',size:'50px'});
crLib.push({image:'http://www.screenplay.no/wordpress/wp-content/themes/screenplay/img/casereel_image1.jpg',title:'Freia',subTitle:'&nbsp;|&nbsp;&nbsp;Sjokolademaskinen og jakten på de forsvunne bitene',link:"http://www.screenplay.no/case/freia-2010",titleColor:'#c82a23',size:'63px'});
crLib.push({image:'http://www.screenplay.no/wordpress/wp-content/themes/screenplay/img/casereel_image2.jpg',title:'Kiwi',subTitle:'&nbsp;|&nbsp;&nbsp;Ja til billigere sunn mat',link:"http://www.screenplay.no/case/kiwi-nokkelhullkampanje",titleColor:'#41ac29',size:'50px'});
crLib.push({image:'http://www.screenplay.no/wordpress/wp-content/themes/screenplay/img/casereel_image3.jpg',title:'J&oslash;lstad',subTitle:'&nbsp;|&nbsp;&nbsp;Profilkampanje',link:"http://www.screenplay.no/case/jolstad-nye-testamenter",titleColor:'#7f6b3b',size:'94px'});
crLib.push({image:'http://www.screenplay.no/wordpress/wp-content/themes/screenplay/img/casereel_image4.jpg',title:'Avinor',subTitle:'&nbsp;|&nbsp;&nbsp;Hjelp jeg vil bli flyveleder',link:"http://www.screenplay.no/case/avinor",titleColor:'#f26901',size:'78px'});
//crLib.push({image:'http://www.screenplay.no/wordpress/wp-content/themes/screenplay/img/casereel_image5.jpg',title:'Storebrand',subTitle:'&nbsp;|&nbsp;&nbsp;Storebrandfamilien',link:"http://www.screenplay.no/case/storebrandfamilien",titleColor:'#b9221c',size:'141px'});


$j(document).ready(function() {
	Cufon.replace('.site-title',{fontFamily:'HelveticaCufon'});
	Cufon.replace('.page-title',{fontFamily:'HelveticaCufon',hover:true});
	Cufon.replace('h1.entry-title',{fontFamily:'HelveticaCufon',hover:true});
	Cufon.replace('body.four04 h2',{fontFamily:'HelveticaCufon',hover:true});
	Cufon.replace('body.four04 h3',{fontFamily:'HelveticaCufon',hover:true});
	Cufon.replace('.access li',{fontFamily:'ProximaCufon', hover:true, fontSize:'13px'});
	Cufon.replace('.widgettitle',{fontFamily:'ProximaCufon',hover:true});
	$j.preloadCssImages();
	
	
	
	if($j("body.home").length){
		initFrontPageShowreel();
	}
	
	//Life's a pitch
	//prepareEventHandlers();
});

function addThumbRollOvers(){
	/*$j(".case-thumbnail").each(function(){
		var _this = $j(this);
		var thumbsrc = $j(this).find("img").attr("src");
		var linksrc = $j(this).find("a.thumb").attr("href");
		$j(this).click(function(){
			window.location = linksrc;
		});
		if(thumbsrc != undefined){
			$j(this).find("a.thumb").hide();
			$j(this).css("cursor","pointer");
			$j(this).css("background-image","url("+thumbsrc+")");
			$j(this).find("a.thumb").remove();
			var img = new Image();
			img.onload = function() {
				Pixastic.process(img, "desaturate", {average : false});
				$j(_this).css({opacity:1});
				$j(_this).show();
				$j(_this).find(".thumb").hide();
			}
			$j(img).addClass("thumb");
			img.src = thumbsrc;
			$j(this).prepend(img);
			$j(".case-thumbnail").hover(function() {	
			$j(this).find(".thumb").stop().fadeTo('normal', 1 , function() {
				$j(this).show();
			});
			} , function() { //on hover out...
				$j(this).find(".thumb").stop().fadeTo('normal', 0, function(){
					$j(this).hide();
				});
			});
		}
	});*/
}

function initUsersNavigation(){
	$j('#sp-users').cycle({ 
    	fx:     'fade', 
   	 	speed:  'fast', 
    	timeout: 0, 
    	next:   '.user-nav-next', 
    	prev:   '.user-nav-prev'  
	});
	$j('li.widget_users').find('.user-nav').each(function(){
		$j(this).css('display','block');
		$j(this).find('div').each(function(){
			$j(this).css('cursor','pointer');
			$j(this).hover(function(){
				$j(this).css('color','#FFFFFF');
			},function(){
				$j(this).css('color','#e2eff5');
			});
		});
	});
}

function initCaseSlideshow(){
		var numKids = $j('.case-image').size();
		if(numKids <= 1){
			return false;
		}
		$j('#case-header-images').cycle({ 
    		fx:     'scrollVert', 
   		 	speed:  500, 
    		timeout: 0,
    		next:   '#case-nav-next', 
    		prev:   '#case-nav-prev',
    		pager: '#case-nav-pager'
		});
		$j('#case-header-nav').find('div').each(function(){
			$j(this).css('display','block');
		});
		$j('#case-header-nav').find('#case-nav-pager').each(function(){
			var navWidth = 0;
			var parentWidth = $j('#case-header').width();
			$j(this).find('a').each(function(){
				navWidth += $j(this).width()+12;
			});
			$j(this).css('width',navWidth+'px');
			$j(this).css('margin-left',(parentWidth/2)-(navWidth/2)+'px');
		});
		$j('#case-header-nav').hide();
		$j('#case-header').mouseenter(function(){
			$j('#case-header-nav').fadeIn('medium',function(){
				$j(this).show();
			});
		});
		$j('#case-header').mouseleave(function(){
			$j('#case-header-nav').fadeOut('slow',function(){
				$j(this).hide();
			});
		});
	
}

function getHeadingWidth(txt){
	//calculate cr-header text width
	var header = document.getElementById("crheaderwidth");	
	header.innerHTML = txt;
	
	//console.log(header.clientWidth);
	var width = (header.clientWidth + 30) + "px";
	
	return width;
}

function initFrontPageShowreel(){
	//Insert images and nav items according to the case reel library
	for(var i = 0; i < crLib.length ; i++){
		//Image and nav DOM Object
		//var img = $('<a href="' + crLib[i].link + '"><img src="' + crLib[i]["image"] + '" class="cr-image cr-image' + i + '"/></a>');
		var img = $j('<a href="' + crLib[i].link + '" class="' + _gaq.push(["_trackEvent", "slideshowFront", crLib[i].subTitle]) +'"><img src="' + crLib[i]["image"] + '" class="cr-image cr-image' + i + '"/></a>');
		if(i == 0){
			var focusclass = 'cr-focus';
		} else {
			var focusclass = 'cr-unfocus';
		}
		
		var navitem = $j('<div class="cr-nav-item cr-nav-item' + i + ' ' + focusclass + '" crpos="' + i + '"></div>');
		
		//Activate nav buttons by attaching event handlers
		navitem.click(function(){slideTo($j(this).attr("crpos"));stopAutonomous();});

		
		//Hide all images but the first
		if(i > 0) $j(img).find(".cr-image").hide();
		
		$j('.cr-mask').append(img);
		$j('.cr-nav').append(navitem);
	}
	
	//Calculate nav width in order to center
	$j(".cr-nav").css("width",$j(".cr-nav-item").size() * 24);
	
	//Add title to first image
	$j(".cr-header").html(crLib[0].title);
	$j(".cr-header").css({'color':crLib[0].titleColor});
	
	//Add subTilte to first image
	$j(".cr-sub-header").html(crLib[0].subTitle);
	
	//console.log("headerWidth ", getHeadingWidth(crLib[0].title));
	$j(".cr-sub-header").css({'left': crLib[0].size});
	
	startAutonomous();
}

function slideTo(id){
			var reelID = parseInt(id);
			
			if(reelID != currentCRPos){
				var direction = ((reelID - currentCRPos) > 0) ? 1 : -1;
				
				//Switch focus marking on nav items
				$j(".cr-nav-item" + currentCRPos).removeClass("cr-focus").addClass("cr-unfocus");
				$j(".cr-nav-item" + reelID).removeClass("cr-unfocus").addClass("cr-focus");
				$j(".cr-header").html(crLib[reelID].title);	
				$j(".cr-header").css({color:crLib[reelID].titleColor});	
				$j(".cr-sub-header").html(crLib[reelID].subTitle);
				$j(".cr-sub-header").css({'left': crLib[reelID].size});//getHeadingWidth(crLib[reelID].title)});
			
				$j(".cr-image" + reelID).show();
				$j(".cr-image" + reelID).css('left',703 * direction);
				$j(".cr-image" + currentCRPos).animate({left:-702 * direction});
				$j(".cr-image" + reelID).animate({left:'0'});
				currentCRPos = reelID;
			}
		}

function startAutonomous(){
	autonomousID = setInterval(slideOnce,5000);
}

function stopAutonomous(){
	clearInterval(autonomousID);
}

function slideOnce(){
	if(currentCRPos < $j(".cr-nav-item").size() - 1){
		slideTo(currentCRPos + 1);
	} else {
		slideTo(0);
	}
}

