/**
 * MISCELÁNEOS
 */

function onAccordionClickHandler() {
	
	var elem = $( this );
	
	if( elem.hasClass( "opened" ) ) {
		elem.attr( "class", "closed" );
	} else {
		elem.attr( "class", "opened" );
	}

	elem.next().slideToggle(function () {
	    elem.css({ "margin-top": "0" });
	    elem.css({ "margin-top": "13px" });
	});

	
}

function getCurrentSection() {
	return $( "#contentsContainer > div.content-selected" ).attr( "id" );
}


/**
 * PÁGINA FICHA TÉCNICA
 */


function onMenuClickHandler() {
	
	var id = $( this ).attr( "class" ).replace( "menu-", "" );
	
	showFlashSection(id);
	
	if( $( "#contentsContainer > div.content-selected" ).attr( "id" ) + "-selected" == id ) {
		return false;
	}
	
	$( ".yt_player" ).html( $( ".yt_player" ).html() );
		
	$( "#contentsContainer > div" ).removeClass( "content-selected" );
	$( "#contentsContainer > div#" + id ).addClass( "content-selected" );
	
	$( "a[class^=menu-][class$=selected]" ).each( function() {
		$( this ).attr( "class", $( this ).attr( "class" ).replace( "-selected", "" ) );
	} );
	
	$( this ).attr( "class", $( this ).attr( "class" ) + "-selected" );

	if( $( "#contentsContainer > div#" + id ).hasClass( "content-not-loaded" ) ) {
	    $( this ).removeClass( "content-not-loaded" );
	    onFilterChangeHandler();
    }
	
	return false;
	
}


/**
 * Paginado Reviews/Opiniones
 */

var hidden = { visibility: "hidden" };
var visible = { visibility: "visible" };

var currentElem;
var currentSection;
var listYear;
var listMonth;
var pagePetition;

var page = {
    reviews: 1,
    opiniones: 1
};

var lastPageLoaded = {
    reviews: 1,
    opiniones: 1
};


function loadPage( page ) {
	
	if( !page >= 1 ) {
		return false;
	}
	
	var listYear = $( "#" + currentSection + " [name=year]" ).val();
	var listMonth = $("#" + currentSection + " [name=month]").val();
	var modelId = $("#" + currentSection + " [name=modelId]").val();
	
	try {
		pagePetition.abort();
		pagePetition = null;
	} catch( e ) {}
	
	pagePetition = $.ajax( {
		url		: "/includes/getReviews.aspx",
		type	: "GET",
		data	: {
			page	: page,
			year	: listYear,
			month	: listMonth,
			section	: currentSection,
            ID : modelId
		},
		success	: onContentPetitionSuccessHandler,
		error	: onContentPetitionErrorHandler
	} );

}

function updatePages( section ) {
	
	updatePageCount();
	
	$( "#" + section + " .page" + page[ section ] ).addClass( "selected" );
	
	if( page[ section ] == 1 ) {
		$( "#" + section + " .previous" ).css( hidden );
	} else {
		$( "#" + section + " .previous" ).css( visible );
	}
	
	if( page[ section ] == $( "#" + section + " a[class^=page]" ).length ) {
		$( "#" + section + " .next" ).css( hidden );
	} else {
		$( "#" + section + " .next" ).css( visible );
	}
	
}

function updatePageCount() {
	
	var pages = $( "#" + currentSection + " a[class^=page]" );
	
	pages.removeClass( "selected" );

	
	var pageCount = parseInt( $( "#pageCount" ).val() );
	
	$( "#pageCount" ).remove();
	
	if( pageCount == pages.length || !pageCount >= 1 ) {
		return true;
	}
	
	
	if( page[ currentSection ] > pageCount ) {
		page[ currentSection ] = pageCount;
	}
	
	
	pages.remove();
	
	for( var i = 1; i <= pageCount; i++ ) {
		
		$( "#" + currentSection + " .next" ).before(
			'<a href="#" class="page' + i + '">' + i + "</a>"
		);
		
	}
	
	$( "#" + currentSection + " .pages a[class^=page]" ).click( onPageClickHandler );
	
}


function getCurrentSectionPageCount() {
	return $( "#" + currentSection + " a[class^=page]" ).length;
}


// Listeners

function onFilterChangeHandler() {
	currentSection = getCurrentSection();
	page[ currentSection ] = 1;
	loadPage( 1 );
}

function onPageClickHandler() {
	
	currentElem = $( this );
	
	if( currentElem.hasClass( "selected" ) ) {
		return false;
	}
	
	currentSection = getCurrentSection();

	page[ currentSection ] = parseInt( currentElem.html() );
	
	loadPage( page[ currentSection ] );
	
	return false;
	
}

function onPreviousPageClickHandler() {
	
	currentSection = getCurrentSection();

	page[ currentSection ] = lastPageLoaded[ currentSection ] - 1;
	
	loadPage( page[ currentSection ] );
	
	return false;
	
}

function onNextPageClickHandler() {

    currentSection = getCurrentSection();

    page[currentSection] = lastPageLoaded[currentSection] + 1;
	
	loadPage( page[ currentSection ] );
	
	return false;
	
}

function onContentPetitionSuccessHandler( r ) {
	
	$( "#" + currentSection + " .content" ).html( r );

	lastPageLoaded[currentSection] = page[currentSection];

	updatePages( currentSection );
	
}

function onContentPetitionErrorHandler() {
	alert( "No se pudo cargar la información solicitada" );
}


/**
 * INIT
 */

function fichaTecnicaInit( section ) {

	// Menu
	$( "a[class^=menu-]" ).click( onMenuClickHandler );
	
	// Accordions
	$( ".accordion > div > a" ).click( onAccordionClickHandler );

	// Date Filters
	$( "[name=year],[name=month]" ).change( onFilterChangeHandler );
	
	// Paginations
	$( "#reviews .pages a[class^=page],#opiniones .pages a[class^=page]" ).click( onPageClickHandler );
	$( "#reviews .pages a.previous,#opiniones .pages a.previous" ).click( onPreviousPageClickHandler );
	$( "#reviews .pages a.next,#opiniones .pages a.next" ).click( onNextPageClickHandler );

	//updatePages( "reviews" );
	//updatePages( "opiniones" );
	
	$( "#menu [class=menu-" + section + "]" ).attr( "class",
		$( "[class=menu-" + section + "]" ).attr( "class" ) + "-selected"
	);
	
	$( "#contentsContainer > [id=" + section + "]" ).attr( "class", "content-selected");
	
}


var flash_visible=true;
var html_visible=true;
var currentFlashSection="elauto";

function showFlashSection(id)
{
	var showflash=true;
	var showhtml=true;
	var flashCommand="";
	
	
	/** Desselecciono todo */
	$("#menu_flash #elauto").attr( "class", "menu_flash_elauto_off" );
	$("#menu_flash #colores").attr( "class", "menu_flash_colores_off" );
	$("#menu_flash #fotos").attr( "class", "menu_flash_fotos_off" );
	
	$( "a[class^=menu-][class$=selected]" ).each( function() {
		$( this ).attr( "class", $( this ).attr( "class" ).replace( "-selected", "" ) );
	} );
	
	
	switch(id)
	{
	case "elauto": 	showflash=true; showhtml=false; flashCommand="elauto";	break;
	case "colores": showflash=true; showhtml=false; flashCommand="colores";	break;
	case "fotos":	showflash=true; showhtml=false; flashCommand="fotos";	break;
	default:		showflash=false; showhtml=true; break;
		
	}
	
	if((flashCommand!="")&&(flashCommand!=currentFlashSection))
	{
		currentFlashSection=flashCommand;
		$("#header_ficha")[0].showSection(flashCommand);
	}
	
	if(flashCommand)
	{
		$("#menu_flash #"+flashCommand).attr( "class", "menu_flash_"+flashCommand+"_on" );
	}
	
	
	if(showflash)
	{
		if(!flash_visible)
		{
			flash_visible=true;
			
			try{
				$("#head_swf").animate({ "height": "462px" });
				$("#header_ficha").animate({ "height": "462px" });
			}catch(e){}
			
			$("#menu_flash").animate({ "top": "423px" });
			$("#menu_flash_spacer_bt").animate({ "height": "370px" });

		}
		
	}
	else
	{
		if(flash_visible)
		{
			flash_visible=false;
			try{
				$("#head_swf").animate({ "height": "0px" });
				$("#header_ficha").animate({ "height": "0px" });
			}catch(e){}

			var sp=28;
			$("#menu_flash").animate({ "top": (sp+53)+"px" });
			$("#menu_flash_spacer_bt").animate({ "height": (sp)+"px" });
			
		}
	}
	
	
	
	
	if(showhtml)
	{
		if(!html_visible)
		{
			html_visible=true;
			$("#contentsContainer").fadeIn();
		}
		
	}
	else
	{
		if(html_visible)
		{
			html_visible=false;
			$("#contentsContainer").fadeOut();
		}
	}
}


var flashAnimStep=0;
var flashAnimInterval=0;


function startFlashAnim()
{
	
	if(flashAnimInterval!=0)
	{
		clearInterval(flashAnimStep);
		flashAnimStep=0;
	}
	flashAnimStep=0;
	flashAnimInterval=setInterval("animateFlash()",100);
}

function scaleTo100(p,start,end)
{
	return (p*((end-start)/100.0) +start);
}

function animateFlash()
{
	var p=flashAnimStep*10;
	
	if(flash_visible) // Lo estoy haciendo visible
	{
		/*
		if(p>=100)
		{
			$("#header_ficha")[0].height=462;
		}
		
		var v=scaleTo100(p,100,462);
		$("#head_swf").css({ "height": v+"px" });
		*/
		
		
		if(p>=100)
		{
			$("#head_swf")[0].css({ "height": "462px" });
		}
		
		var v=scaleTo100(p,100,462);
		$("#header_ficha")[0].height=v;

		// 370  --> 28
		var sp=scaleTo100(p,28,370);
		$("#menu_flash").css({ "top": (sp+53)+"px" });
		$("#menu_flash_spacer_bt").css({ "height": (sp)+"px" });
	}
	else // LO estoy ocultando
	{
		
		
		/*
		if(p==0)
		{
			$("#header_ficha")[0].height=0;
		}
		
		var v=scaleTo100(p,462,100);
		if(p>=100) {v=0;}
		$("#head_swf").css({ "height": v+"px" });
		
		
		
		*/
		
		if(p==0)
		{
			$("#head_swf").css({ "height": "0px" });
		}
		var v=scaleTo100(p,462,100);
		if(p>=100) {v=0;}
		$("#header_ficha")[0].height=v;

		
		
		

		// 370  --> 28
		var sp=scaleTo100(p,370,28);
		$("#menu_flash").css({ "top": (sp+53)+"px" });
		$("#menu_flash_spacer_bt").css({ "height": (sp)+"px" });
	}
	
	
	
	if(flashAnimStep>=10)
	{
		clearInterval(flashAnimInterval);
		flashAnimInterval=0;
	}
	
	flashAnimStep++;
}
