$(document).ready(function(){

	//GLOBAL STOP VARS
	var clearQueue = true;
	var gotoEnd = true;

	//$('#dsiDesc ul li:first-child').css({'font-size':'15px','font-weight':'bold'});
	//$('#dsiDesc ul li:first-child strong').empty();

	//MAIN MENU SELECTEDS
	$('#btn'+section).css({'background-position':'top right'});

	//FLAGS HOVER
	$('#flagsContainer ul li a').animate({opacity:0.5},1);
	// Remove opacity from the current localization flag
	$('#'+sActiveFlag).animate({opacity:1});
	$('#flagsContainer ul li a').hover(
		function()
		{
			if ( $(this).attr('id') != sActiveFlag  )
				$(this).animate({opacity:1},1);
		},
		function()
		{
			if ( $(this).attr('id') != sActiveFlag  )
				$(this).animate({opacity:0.5},1);
		}
	);

	//SUBMENU HOVERS
	/*
	$('#submenu li').hover(
		function(){ $(this).css({'background-position':'top right'}) },
		function(){ $(this).css({'background-position':'top left'}) }
	);
	*/

	if ( section == 'Dsi' )
	{
		//DSI DEFAULTS
		$('#dsiScreensList ul:first, #dsiScreensList li div:first').show();
		$('#dsiScreensList li label').animate({opacity:0.5},1);
		$('#dsiScreensList li label:first').animate({opacity:1},1);
		$('#dsiScreensList li label:first img').css({'border':'1px solid #feb300'});

		//DSI SCREENS CLICK FUNC
		$('.dsiSlideShow li label').click(function(){
			$('.dsiSlideShow li label').animate({opacity:0.5},100);
			$('.dsiSlideShow li label img').css({'border':'1px solid #ae3a01'});
			$('.dsiSlideShow li div').hide();
			$(this).animate({opacity:1},100);
			$(this).children('img').css({'border':'1px solid #feb300'});
			$(this).parent('li').children('div').fadeIn(150);
		});

		$('.dsiSlideShow li label img').hover(
			function(){ $(this).animate({opacity:0.5},200); },
			function(){
				$(this).stop(clearQueue,gotoEnd);
				$(this).animate({opacity:1},200);
			}
		);

		//DSI SCREENS PAGER
		var init = 0;
		var maximo = $('#dsiScreensList ul').size()-1; // PAGER MAX VALUE

		$('#dsiScreensPrev').hover(
			function(){ $(this).attr('src','images/btn_prev_hover.png'); },
			function(){ $(this).attr('src','images/btn_prev.png'); }
		);
		$('#dsiScreensNext').hover(
			function(){ $(this).attr('src','images/btn_next_hover.png'); },
			function(){ $(this).attr('src','images/btn_next.png'); }
		);

		// hiding the buttons
		$('#dsiScreensPrev').hide();
		if ( maximo < 1 )
			$('#dsiScreensNext').hide();

		$('#dsiScreensPrev').click(function(){
			if (init > 0)
			{
				$('#dsiScreensList_'+init).hide();
				init--;
				$('#dsiScreensList_'+init+' li label').animate({opacity:0.5},1);

    			// hide all the big images
    			$( '#dsiScreensList_'+init+' li div' ).each(
    				function( intIndex ){
    					$( this ).hide ();
    				}
    			);
				$('#dsiScreensList_'+init+' li label:first').animate({opacity:1},1);
				$('#dsiScreensList_'+init+' li label:first img').css({'border':'1px solid #feb300'});
				$('#dsiScreensList_'+init+' li div:first').show();
				$('#dsiScreensList_'+init).fadeIn(250);
			}
			// changing the buttons
			if ( init < 1 )
				$('#dsiScreensPrev').hide();
			if ( init < maximo )
				$('#dsiScreensNext').show();
		});

		$('#dsiScreensNext').click(function(){
			if (init < maximo)
			{
				$('#dsiScreensList_'+init).hide();
				init++;
    			$('#dsiScreensList_'+init+' li label').animate({opacity:0.5},1);
                // hide all the big images
    			$( '#dsiScreensList_'+init+' li div' ).each(
    				function( intIndex ){
    					$( this ).hide ();
    				}
    			);
				$('#dsiScreensList_'+init+' li label:first').animate({opacity:1},1);
				$('#dsiScreensList_'+init+' li label:first img').css({'border':'1px solid #feb300'});
				$('#dsiScreensList_'+init+' li div:first').show();
				$('#dsiScreensList_'+init).fadeIn(250);
			}
			// changing the buttons
			if ( init > 0 )
				$('#dsiScreensPrev').show();
			if ( init >= maximo )
				$('#dsiScreensNext').hide();
		});
	}


	if ( section == 'Ipad' )
	{
		//$('.iphoneScreensList, .iphoneVideosList').css({'background':'url(images/ipad_container.png) 85px 0px no-repeat'});
		$('.iphoneScreensList, .iphoneVideosList').css({'background':'none'});
		$('.iphoneSlideShow, .iphoneVideosSlideShow').css({'top':'335px'});
		$('.iphoneSlideShow li div').css({'top':'-345px','left':'135px'});
		$('.iphoneVideosSlideShow li div').css({'width':'400px','height':'298px','top':'-350px','left':'123px'});
		$('.iphoneScreensTitle, .iphoneVideosTitle').css({'top':'302px'});
		$('.iphoneScreensPager').css({'top':'351px','left':'40px'});
	}

	if ( section == 'Iphone' ||  section == 'Palm' || section == 'Ipad' )
	{
		//IPHONE SCREENS PAGER
		var init = 0;
		var maximo = $('.iphoneScreensList ul').size() - 1; // PAGER MAX VALUE

		// hiding the buttons
		$('.iphoneScreensPrev').hide();
		if ( maximo < 1 )
			$('.iphoneScreensNext').hide();

		$('.iphoneScreensList ul:first, .iphoneScreensList li div:first').show();
		$('.iphoneScreensList li label').animate({opacity:0.5},1);
		$('.iphoneScreensList li label:first').animate({opacity:1},1);
		$('.iphoneScreensList li label:first img').css({'border':'1px solid #feb300'});

		//IPHONE SCREENS CLICK FUNC
		$('.iphoneSlideShow li label').click(function(){
			$('.iphoneSlideShow li label').animate({opacity:0.5},100);
			$('.iphoneSlideShow li label img').css({'border':'1px solid #ae3a01'});
			$('.iphoneSlideShow li div').hide();
			$(this).animate({opacity:1},100);
			$(this).children('img').css({'border':'1px solid #feb300'});
			$(this).parent('li').children('div').fadeIn(150);
		});

		$('.iphoneSlideShow li label img').hover(
			function(){ $(this).animate({opacity:0.5},200); },
			function(){
				$(this).stop(clearQueue,gotoEnd);
				$(this).animate({opacity:1},200);
			}
		);

		$('.iphoneScreensPrev').hover(
			function(){ $(this).attr('src','images/btn_prev_hover.png'); },
			function(){ $(this).attr('src','images/btn_prev.png'); }
		);
		$('.iphoneScreensNext').hover(
			function(){ $(this).attr('src','images/btn_next_hover.png'); },
			function(){ $(this).attr('src','images/btn_next.png'); }
		);

		$('.iphoneScreensPrev').click(function(){
			if (init > 0)
			{
				$('.iphoneScreensList_'+init).hide();
				init--;
				$('.iphoneScreensList_'+init+' li label').animate({opacity:0.5},1);
                // hide all the big images
    			$( '.iphoneScreensList_'+init+' li div' ).each(
    				function( intIndex ){
    					$( this ).hide ();
    				}
    			);
				$('.iphoneScreensList_'+init+' li label:first').animate({opacity:1},1);
				$('.iphoneScreensList_'+init+' li label:first img').css({'border':'1px solid #feb300'});
				$('.iphoneScreensList_'+init+' li div:first').show();
				$('.iphoneScreensList_'+init).fadeIn(250);
			}
			// changing the buttons
			if ( init < 1 )
				$('.iphoneScreensPrev').hide();
			if ( init < maximo )
				$('.iphoneScreensNext').show();
		});

		$('.iphoneScreensNext').click(function(){
			if (init < maximo)
			{
				$('.iphoneScreensList_'+init).hide();
				init++;
				$('.iphoneScreensList_'+init+' li label').animate({opacity:0.5},1);
                // hide all the big images
    			$( '.iphoneScreensList_'+init+' li div' ).each(
    				function( intIndex ){
    					$( this ).hide ();
    				}
    			);
				$('.iphoneScreensList_'+init+' li label:first').animate({opacity:1},1);
				$('.iphoneScreensList_'+init+' li label:first img').css({'border':'1px solid #feb300'});
				$('.iphoneScreensList_'+init+' li div:first').show();
				$('.iphoneScreensList_'+init).fadeIn(250);
			}
			// changing the buttons
			if ( init > 0 )
				$('.iphoneScreensPrev').show();
			if ( init >= maximo )
				$('.iphoneScreensNext').hide();
		});

		//Palm  VIDEOS PAGER
		var init_video = 0;
		var max_video = $('.iphoneVideosList ul').size() - 1; // PAGER MAX VALUE

		$('.iphoneVideosList ul:first, .iphoneVideosList li div:first').show();
		$('.iphoneVideosList li label').animate({opacity:0.5},1);
		$('.iphoneVideosList li label:first').animate({opacity:1},1);
		$('.iphoneVideosList li label:first img').css({'border':'1px solid #feb300'});

		//IPHONE VIDEOS CLICK FUNC
		$('.iphoneVideosSlideShow li label').click(function(){
			$('.iphoneVideosSlideShow li label').animate({opacity:0.5},100);
			$('.iphoneVideosSlideShow li label img').css({'border':'1px solid #ae3a01'});
			$('.iphoneVideosSlideShow li div').hide();
			$(this).animate({opacity:1},100);
			$(this).children('img').css({'border':'1px solid #feb300'});
			$(this).parent('li').children('div').fadeIn(150);
		});

		$('.iphoneVideosSlideShow li label img').hover(
			function(){ $(this).animate({opacity:0.5},200); },
			function(){
				$(this).stop(clearQueue,gotoEnd);
				$(this).animate({opacity:1},200);
			}
		);

		//IPHONE && PALM DEFAULTS
		$('.palmScreensList ul:first, .palmScreensList li div:first').show();
		$('.palmScreensList li label').animate({opacity:0.5},1);
		$('.palmScreensList li label:first').animate({opacity:1},1);
		$('.palmScreensList li label:first img').css({'border':'1px solid #feb300'});

		//IPHONE && PALM VIDEO DEFAULTS
		$('.palmVideosList ul:first, .palmVideosList li div:first').show();
		$('.palmVideosList li label').animate({opacity:0.5},1);
		$('.palmVideosList li label:first').animate({opacity:1},1);
		$('.palmVideosList li label:first img').css({'border':'1px solid #feb300'});


		$('.iphoneVideosPrev').hover(
			function(){ $(this).attr('src','images/btn_prev_hover.png'); },
			function(){ $(this).attr('src','images/btn_prev.png'); }
		);
		$('.iphoneVideosNext').hover(
			function(){ $(this).attr('src','images/btn_next_hover.png'); },
			function(){ $(this).attr('src','images/btn_next.png'); }
		);

		$('.iphoneVideosPrev').click(function(){
			if (init > 0)
			{
				$('.iphoneVideosList_'+init_video).hide();
				init_video--;
				$('.iphoneVideosList_'+init_video+' li label').animate({opacity:0.5},1);
                // hide all the big images
    			$( '.iphoneVideosList_'+init_video+' li div' ).each(
    				function( intIndex ){
    					$( this ).hide ();
    				}
    			);
				$('.iphoneVideosList_'+init_video+' li label:first').animate({opacity:1},1);
				$('.iphoneVideosList_'+init_video+' li label:first img').css({'border':'1px solid #feb300'});
				$('.iphoneVideosList_'+init_video+' li div:first').show();
				$('.iphoneVideosList_'+init_video).fadeIn(250);
			}
		});

		$('.iphoneVideosNext').click(function(){
			if (init_video < max_video)
			{
				$('.iphoneVideosList_'+init_video).hide();
				init_video++;
				$('.iphoneVideosList_'+init_video+' li label').animate({opacity:0.5},1);
                // hide all the big images
    			$( '.iphoneVideosList_'+init_video+' li div' ).each(
    				function( intIndex ){
    					$( this ).hide ();
    				}
    			);
				$('.iphoneVideosList_'+init_video+' li label:first').animate({opacity:1},1);
				$('.iphoneVideosList_'+init_video+' li label:first img').css({'border':'1px solid #feb300'});
				$('.iphoneVideosList_'+init_video+' li div:first').show();
				$('.iphoneVideosList_'+init_video).fadeIn(250);
			}
		});

	}

	//SUB MENU SWITCHER
		//LOOP FOR SECTION'S EVENT
		for (var i=1; i < 6; i++){
			$('#submenuBtn0' + i).click(function(){
				var id_subSec = this.id.split("submenuBtn")[1];
				showSubSection(id_subSec);
			})
		}
		//READS URL AND CHECKS
		var loc = window.location.href;
		if (loc.indexOf('#') != -1)
		{
			var subSection = loc.split('#')[1];
			var totalSubSections = {"desc":"01","feats":"02","screens":"03","videos":"04","buy":"05"};

			if (typeof totalSubSections[subSection] != "undefined"){
				showSubSection(totalSubSections[subSection]);
			}
		}

});
		//SECTION SWITCH ANIMATION
		function showSubSection(sec){
			$('#subcontentContainer_01, #subcontentContainer_02, #subcontentContainer_03, #subcontentContainer_04, #subcontentContainer_05').hide();
			$('#subcontentContainer_' + sec).fadeIn(350);
			$('#submenu li a').css({'background-position':'top left'});
			$('#submenuBtn' + sec).find('a').css({'background-position':'top right'});
		}

		function completeUrl (element)
		{
			var loc = window.location.href;
			if (loc.indexOf('#') != -1)
			{
				var subSection = loc.split('#')[1];
				element.href = element.href + '#' + subSection;
			}
			return true;
		}