/* Author: William Thomas

*/

var carouselTimeout,
	overlayTimeout,
	captionTimeout,
	myElem,
	mouse_is_inside = false;

$(document).ready(function(){
	
	$('.bgProfile:not(#profile1)').css({
		opacity: 0
	});
	
	var $accordion = $('#accordion');
		
	$accordion.accordion({
		autoHeight: false,
		navigation: true,
		collapsible: true,
		active: false
	});
	$accordion.accordion( "activate" , false );
	var blogCount = 3;
	var juptrQuery = 'from:GT_Lucy OR from:GT_Kieron OR from:GT_JamesP OR from:GT_TomW OR from:GT_Astrid OR from:GT_Emma OR from:GT_Rekha OR from:GT_Lizzie OR from:GT_NickB OR from:GT_Sajni OR from:GT_TomE OR from:GT_Tasha OR from:GT_Will OR from:GT_Chris1 OR from:GT_Campus OR from:GrantThorntonUK include:retweets';
	
	if( $('#insideWrap').length ){
		blogCount = 2;
		juptrQuery = 'from:GT_Astrid OR from:GT_Emma OR from:GT_Rekha OR from:GT_Lizzie OR from:GT_NickB OR from:GT_Sajni OR from:GT_TomE OR from:GT_Tasha include:retweets';	
	}
	
	if( $('#insideWrap #audit').length ){
		juptrQuery = 'from: GT_JamesP OR from:GT_Lizzie OR from:GT_Astrid OR from:GT_Rekha OR from:GT_Andrea include:retweets';	
	} else if( $('#insideWrap #busRisk').length ){
		juptrQuery = 'from:GT_Sajni include:retweets';	
	} else if( $('#insideWrap #recovOrg').length ){
		juptrQuery = 'from:GT_Emma include:retweets';	
	} else if( $('#insideWrap #tax').length ){
		juptrQuery = 'from:GT_Lucy OR from:GT_Kieron OR from:GT_TomW OR from:GT_Chris1 OR from:GT_NickB include:retweets';	
	} else if( $('#insideWrap #actural').length ){
		juptrQuery = 'from:GT_Tom_E include:retweets';	
	}
	
	$('#tweets').juptr({
		query: juptrQuery,
		count: 3,
		api: 'search',
		avatar: true,
		className: ''
	});
	
	$('#rss').rssfeed('http://traineeblog.grant-thornton.co.uk/feed/', {
			limit: blogCount,
			content: true,
			snippet: false,
			date: true,
			header: false
	});
	
	if ('ontouchstart' in document.documentElement) {
		
		$("a.adviceBalloon").bind('touchend',function() {
			$('#popUpReveal:hidden').not('.active').addClass('active').fadeIn();
			stopCarousel();
		});
		$("#overlayTrigger").bind('touchend',function() {
			$('#overlay').not('.active').addClass('active').fadeIn();
		});
		$('body').bind('touchstart',function() {
			$('#overlay.active').fadeOut(200,function(){
				$(this).removeClass('active');
			});
			$('#popUpReveal.active').fadeOut(200,function(){
				$(this).removeClass('active');
				startCarousel();
			});
		});
		

		
	} else {
	
		$('a.adviceBalloon').hover(function(){
			stopCarousel();
			$('#popUpReveal').stop(true,true).fadeIn(300);
		},function(){
			if(!$('#playPause').hasClass('active')){
				startCarousel();	
			}
			$('#popUpReveal').stop(true,true).fadeOut(300);
		});
		
		$('#overlayTrigger, #overlay').hover(function(){
			clearTimeout(overlayTimeout);
			$('#overlay').stop(true,true).fadeIn(300);
		},function(){
			overlayTimeout = setTimeout("hideOverlay()",50);		
		});
	
	}
	
	$('#enterComp').click(function(){
		if(!$('html').hasClass('oldie')){
			$( "#dialog" ).dialog({
				width: 645,
				modal: true
			});
			return false;
		}
	});
	
	$('a.adviceBalloon').click(function(){
		return false;
	});
	
	$('.gallery .caption').hide();
	
	$('.gallery a').hover(function(){
		clearTimeout(captionTimeout);
		myElem = $(this);
		captionTimeout = setTimeout("revealCaption(myElem)",300);	
	},function(){
		clearTimeout(captionTimeout);
		myElem = '';
		$(this).children('.caption').slideUp();	
	});
		
	var $profiles = $('.bgProfile'),
		loadProfiles = new Array(),
		profilecount = $profiles.length;
	
	/*
	
	$($profiles).each(function(){
		$this = $(this);
		var myImg = $this.css('background-image');
		myImg = myImg.replace(/[\w]+\(\"([^"]+)\"\)/,'$1');
		myImg = myImg + '?v=' + new Date().getTime();
		$('<img/>').attr( 'src', myImg ).load(function() {
			loadProfiles.push(myImg);
			if(loadProfiles.length >= profilecount){
				startCarousel();
			}
		});
	});
	
	*/
	
	$('#playPause').click(function(){
		var $this = $(this);
		if($this.hasClass('active')){
			startCarousel();
		} else {
			stopCarousel();
		}
		$this.toggleClass('active');
	});
	
	startCarousel();
		
	initMap();
		
});

function revealCaption(elem){
	elem.children('.caption').slideDown();
}

function hideOverlay(){
	$('#overlay').stop(true,true).fadeOut(300);	
}

function startCarousel(){
	clearTimeout(carouselTimeout);
	carouselTimeout = setTimeout("switchCarousel(1800)",5000);
}
function stopCarousel(){
	clearTimeout(carouselTimeout);
}
function switchCarousel(speed){
	var $carouselProfile = $('.bgProfile');
	//subtract 1 from length as we index from 0
	var totalProfiles = $carouselProfile.length - 1;
	var $currentProfile = $('.bgProfile.selected');
	var currentProfileIndex = $carouselProfile.index($currentProfile);
	$currentProfile.removeClass('selected').animate({
		opacity: 0
	},speed, function(){
		$(this).hide();
	});
	var nextProfile = currentProfileIndex + 1; 
	if(nextProfile > totalProfiles)
		nextProfile = 0;
	$carouselProfile.eq(nextProfile).addClass('selected').show(100,function(){
		$(this).animate({
			opacity: 1
		}, speed, function(){
			startCarousel();	
		});
	});
}

function initMap() {
	if($('#map_canvas').length){
    
		if (GBrowserIsCompatible()) {
      		var map = new GMap2(document.getElementById("map_canvas")),
	  			longtitude = $('#map_canvas').attr('longtitude'), 
		  	    latittude = $('#map_canvas').attr('lattitude');
      		map.setCenter(new GLatLng(latittude,longtitude), 16);
      		map.setUIToDefault();

			var marker = new GMarker(map.getCenter());
			map.addOverlay (marker);	
		}	
		
    }
}
