$(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'));
			}
		}		
		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: "HomepageGallery", Action: "ViewImage", Vehicle: VEHICLE_ABBR, Image: sSelectedItemId, Section: "HomepageGallery", Type: "HomepageGalleryItem"});
			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('.viewer').find('.current').each(function(i,n) {
			var oCurrentGalleryItemFlash = $(n).find('.flash');
		
			if(oCurrentGalleryItemFlash[0] != null){
				var sCurrentGalleryItemFlash = oCurrentGalleryItemFlash.html();
				oCurrentGalleryItemFlash.html(sCurrentGalleryItemFlash);
			}
		});
		
		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-", "");
			
			if ($('body').hasClass('fj-cruiser')){
				$('#main').css('background', 'url("/static/images/' + sCurrentItemBackground + '-background-2000x555.jpg") 50% 0 no-repeat');
			} else if ($('body').hasClass('yaris')){
				$('#main').css('background', 'url("/static/images/' + sCurrentItemBackground + '-background-2000x539.jpg") 50% 80px no-repeat');
			} else if ($('body').hasClass('corolla')){
				$('#main').css('background', 'url("/static/images/' + sCurrentItemBackground + '-background-2000x527.jpg") 50% 80px no-repeat');
			}else {
				$('#main').css('background', 'url("/static/images/' + sCurrentItemBackground + '-background-2000x454.jpg") 50% 0 no-repeat');
			}
		} else if ($('body').hasClass('yaris')){
			$('#main').css('background', 'url("/static/images/16dh8-yaris-default-background-2000x539.jpg") 50% 80px no-repeat');
		} else if ($('body').hasClass('prado')){
			//$('#main').css('background', 'none');
		}else {
			$('#main').css('background', 'none');
		}

		var sCurrentUrl = location.href;
		var aCurrentUrlHash = sCurrentUrl.split('#');		
		location.href = aCurrentUrlHash[0]+'#'+sCurrentItemId;				
					
	}
}
