/****************************************************************************************************
* home.js
* ScreenToaster
* Michael Bagur
*
* home page special behaviour
*
*****************************************************************************************************/

jQuery(function($) {
	try {
		// Onglets Home
		$('.tabs', '#tabs-home').find('li').find('a').each(
			function(index) {
				// click sur les onglets
				$(this).click(
					function() {
						try {
							$('.tabs', '#tabs-home').find('li').find('a').removeClass('active');
							$(this).addClass('active');
							
							$('.tabs-inner').hide();
							$('.tabs-inner').eq(index).show();
		
							// store active menu index in cookie
							$.cookie('home_menu',index, {expires: 7});
						} catch (e) {
							logger.error("home.js > "+e);
						}
						
						return false;
					}
				);
			}
		);

		// Fetch cookie value to know what menu we were looking at
		var menuIndex = parseInt($.cookie('home_menu'));
		if(!menuIndex || menuIndex < 0 || menuIndex > 2) {
			menuIndex = 0;
		}
		$('.tabs', '#tabs-home').find('li').eq(menuIndex).find('a').addClass('active');
		$('.tabs-inner').eq(menuIndex).show();
	
		/*****************************************************************************/
		/*****************************************************************************/

		// Onglets Just Toasted
		$('.just-toasted-menu').find('a').each(
			function(index) {
				// click sur les onglets
				$(this).click(
					function() {
						try {
							// Change active class on links
							$('.just-toasted-menu').find('a').removeClass('active');
							$(this).addClass('active');
							
							var id = $(this).attr('id');
							
							// Change visible div
							$('.just-toasted-videos').hide();
							$('#'+id+'_videos').show();
		
							// Store active menu index in cookie
							$.cookie('just-toasted-menu', id, {expires: 7});
						} catch (e) {
							logger.error("home.js > "+e);
						}
						
						return false;
					}
				);
			}
		);
		
		// Get ids from cookies
		var justToastedId = $.cookie('just-toasted-menu');
		// Put default values
		if (!justToastedId || $('.just-toasted-menu').find('#'+justToastedId).length == 0) {
			justToastedId = $('.just-toasted-menu').find(":first").attr('id');
		}
		// Simulate click
		$('.just-toasted-menu').find('#'+justToastedId).click();
	
		/*****************************************************************************/
		/*****************************************************************************/
	
		// Onglets Videos
		$('#videos-menu-videos').find('a').each(
			function(index) {
				// click sur les onglets
				$(this).click(
					function() {
						try  {
							var parentClass = $(this).parent().attr('class');
							
							// Change active class on links
							$('#videos-menu-videos').find('.'+parentClass).find('a').removeClass('active');
							$(this).addClass('active');
							
							var rightId = $('#videos-menu-videos').find('.right').find('.active').attr('id');
							var leftId = $('#videos-menu-videos').find('.left').find('.active').attr('id');
							
							// Change visible div
							$('.videos-videos').hide();
							$('#'+leftId+'_'+rightId+'_videos').show();
		
							// Store active menu index in cookie
							$.cookie('videos-menu-right', rightId, {expires: 7});
							$.cookie('videos-menu-left', leftId, {expires: 7});
						} catch (e) {
							logger.error("home.js > "+e);
						}
						
						return false;
					}
				);
			}
		);
	
		// Get ids from cookies
		var rightId = $.cookie('videos-menu-right');
		var leftId = $.cookie('videos-menu-left');
		// Put default values
		if (!rightId || $('#videos-menu-videos').find('#'+rightId).length == 0){
			rightId = $('#videos-menu-videos').find('.right').find(":first").attr('id');
		}
		if (!leftId || $('#videos-menu-videos').find('#'+leftId).length == 0){
			leftId = $('#videos-menu-videos').find('.left').find(":first").attr('id');
		}
		// Simulate click
		$('#videos-menu-videos').find('#'+rightId).click();
		$('#videos-menu-videos').find('#'+leftId).click();

	// Catch errors
	} catch (e) {
		logger.error("home.js > "+e);
	}

});