$(function(){
	if($('body').hasClass('homepage')){
		homepageGallery.activateThumbnails();
		homepageGallery.activateItemDeepLinking();
	}
});

var homepageGallery = {
	changeItemURL: function(sItemId){
		var sCurrentUrl = location.href;
		var aCurrentUrlHash = sCurrentUrl.split('#');		
		location.href = aCurrentUrlHash[0]+'#item-'+sItemId;	
	},	
	activateItemDeepLinking: function(){
		var sCurrentUrl = location.href;
		var aCurrentUrl = sCurrentUrl.split('#');
		
		if((aCurrentUrl[1] != null) && (aCurrentUrl[1] != '')){
			oSelectedItem = $('#item-'+aCurrentUrl[1]);
			if(oSelectedItem[0] != null){
				homepageGallery.activateItem(oSelectedItem);
			} 
		}		
		else if($('.gallery .block .list .item:first')[0] != null){
			homepageGallery.activateItem($('.gallery .block .list .item:first'));
		}
	},		
	activateThumbnails: function(){	
		$('.gallery .block .list .item .link').click(function(){
			var oSelectedItem = $(this).parents('.item');
			var sSelectedItemId = oSelectedItem.attr('id').replace("item-", "");
			homepageGallery.activateItem(oSelectedItem);					
			
			/*TRACKING*/
			tracking.event.activate('trackingevent', {EventClass: "Gallery", Action: "ViewImage", Vehicle: "HSD", Image: sSelectedItemId});
			
			return false;
		});
	},		
	activateItem: function(oCurrentItem){	
		var sCurrentItemId = oCurrentItem.attr('id').replace("item-", "");
		var oCurrentGalleryItem = $('#'+oCurrentItem.attr('id').replace("item-", "item-content-"));
		
		oCurrentGalleryItem.parents('.gallery').find('.item-content').removeClass('current');
		oCurrentGalleryItem.addClass('current');
		
		oCurrentItem.parents('.list').find('.item').removeClass('current');
		oCurrentItem.addClass('current');
		
		if (!oCurrentItem.hasClass('no-background')) {
			var sCurrentItemBackground = oCurrentGalleryItem.find('.media-box').attr('id').replace("background-", "");
			$('#main').css('background', 'url("/static/images/' + sCurrentItemBackground + '-background-2000x454.jpg") 50% 0 no-repeat');
		} else{
			$('#main').css('background', 'none');
		}

		var sCurrentUrl = location.href;
		var aCurrentUrlHash = sCurrentUrl.split('#');		
		location.href = aCurrentUrlHash[0]+'#'+sCurrentItemId;				
					
	}
}
