﻿/*Player*/
var player;
var miniaturesPhotos;
var miniaturesVideos;
var mainPhoto;
//position pour le menu deroulant
var currentPhotoPos, initPhotoPos;
var currentVideoPos, initVideoPos;
var photoNext;
var videoNext;
var isBeingPhotoMove = false;
var isBeingVideoMove = false;
var timerPhoto = null;
var legendePhoto,legendeVideo;

//ecran media
var photos, videos, resa;
var itemsMenuMedia;
var isVideoInitialised = false;

//Calendrier
var jours;
var loader;
var listeMois;
var moisSelected;
var minisResa;
var legendeResa;
var idModele = -1;

//Promos
var promoList;
var curPromo = 0;
var promos;
var timerPromos = null;

$(document).ready(function()
{
    //Ecrans media    
    photos = $('#photos');
    videos = $('#videos');
    resa = $('#resa');

    //Player Photo
    mainPhoto = $('#mainPhoto');
    legendePhoto = $('#legendePhoto');
    player = $('#player');
    $('#photo').hover(function() { player.fadeIn('fast'); }, function() { player.fadeOut('fast'); });
    $('#player_play').click(togglePlay);
    $('#player_prev').click(function() { moveAndSelectPhoto(-1); });
    $('#player_next').click(function() { moveAndSelectPhoto(1); });

    initPhotoPos = $('#photoPrev').click(function() { movePhoto(-1); }).width();
    currentPhotoPos = initPhotoPos;
    photoNext = $('#photoNext').click(function() { movePhoto(1); });
    miniaturesPhotos = $('#navPhoto img.mini').click(function() { selectPhotoMini($(this)) }).each(initPhotoMini);
    endInitPhoto();
    timerPhoto = setInterval("moveAndSelectPhoto(1)", 4000);

    //Menu Media
    itemsMenuMedia = $('.menuMedia li');
    $('#btPhotos').click(function() { showEcran(photos, $(this)); });
    $('#btVideo').click(function() { showEcran(videos, $(this)); InitialisedVideo(); });
    /*$('#btResa').click(function() { showEcran(resa, $(this)); });*/

    //Partie Resa
    loader = $('#loader');
    jours = $('#jours').setTemplateElement('calendarTemplate').click(startLoc);
    $('#mois').click(selectMois);
    listeMois = $('#mois li');
    moisSelected = $('#moisSelected');
    minisResa = $('#navResa .mini').click(selectModele);
    legendeResa = $('#legendeResa');

    //Partie promos
    promos = $('#promos').setTemplateElement('promoTemplate');
    GetPromos();

    $('a[rel*=lightbox]').lightbox();

});

function process()
{
    processing = true;
    jours.processTemplate(null);
    loader.show();
}

function endProcess()
{
    processing = false;
    loader.hide();
}

function selectMois(e)
{
    var target = e.target, // e.target grabs the node that triggered the event.
	$target = $(target);  // wraps the node in a jQuery object
    if (target.nodeName === 'LI')
    {
        listeMois.removeClass('selected');
        GetCalendar(idModele, $target.addClass('selected').attr('rel'));
        moisSelected.html($target.attr('title'));
    }
}

function selectModele()
{
    minisResa.removeClass('selected');
    idModele = $(this).addClass('selected').attr('rel');    
    GetCalendar(idModele, $('#mois li[class=selected]').attr('rel'));
    legendeResa.html($(this).attr('title'));
}

function startLoc(e)
{
    var target = e.target, // e.target grabs the node that triggered the event.
	    $target = $(target);  // wraps the node in a jQuery object
    if (target.nodeName === 'TD' && $target.hasClass("dispo"))
    {
        document.location.href = 'reservation.aspx?idModele=' + idModele+'&debut='+$target.attr('rel');       
    }
    
}

/*Partie Calendrier*/
var processing = false;
function GetCalendar(idModele, idMonth)
{
    if (!processing)
    {
        process();
        $.ajax({
            type: "POST",
            url: '../WS/ResaManager.svc/GetCalendar',
            contentType: "application/json; charset=utf-8", dataType: "json",
            data: '{"idModele":"' + idModele + '","idMonth":"' + idMonth + '"}',
            success: GetCalendarResult,
            error: function(xhr) { alert('Erreur de recuperation des informations de disponibilités'); endProcess(); }
        });
    }
}

function GetCalendarResult(result)
{
    endProcess();
    jours.processTemplate(result);
    jours.click(startLoc);
}

/*Partie Promos*/
var processingPromo = false;
function GetPromos()
{
    if (!processingPromo)
    {
        processingPromo = true;
        $.ajax({
            type: "POST",
            url: '../WS/ResaManager.svc/GetPromos',
            contentType: "application/json; charset=utf-8", dataType: "json",
            data: '',
            success: GetPromosResult,
            error: function(xhr) { alert('Erreur de recuperation des promotions'); processingPromo = false; }
        });
    }
}

function GetPromosResult(result)
{
    processingPromo = false;
    promoList = result;
    timerPromos = setInterval("changePromo()", 6000);
}

function changePromo()
{
    if (promoList.length > 0)
    {
        curPromo++;
        if (curPromo >= promoList.length)
        {
            curPromo = 0;
        }
        promos.processTemplate(promoList[curPromo]);        
    }
}

/**/

function InitialisedVideo()
{
    if (!isVideoInitialised)
    {
        /*Player Video*/
        legendeVideo = $('#legendeVideo');

        initVideoPos = $('#videoPrev').click(function() { moveVideo(-1); }).width();
        currentVideoPos = initVideoPos;
        videoNext = $('#videoNext').click(function() { moveVideo(1); });
        miniaturesVideos = $('#navVideo img.video').click(selectVideoMini).each(initVideoMini);        
        endInitVideo();
        
        isVideoInitialised = true;
    }
}

function showEcran(ecran,bt)
{    
    photos.hide();
    videos.hide();
    resa.hide();
    ecran.show();
    itemsMenuMedia.removeClass('selected');
    bt.addClass('selected');
}

/*Partie Player Photo*/
function initPhotoMini()
{
    $(this).css({ position: 'absolute', left: currentPhotoPos, top: '0px' });
    currentPhotoPos += 122;
}

function endInitPhoto()
{
    //Ajoute des photos manquante
    if (miniaturesPhotos.size() >= 1)
    {   
        var idx = 0;
        while (miniaturesPhotos.size() + idx * miniaturesPhotos.size() < 8)
        {
            miniaturesPhotos.each(addPhoto);
            idx++;
        }
        miniaturesPhotos = $('#navPhoto img.mini');
        miniaturesPhotos.last().css('left', initPhotoPos - 122);
    }
}

function addPhoto()
{
    photoNext.before($(this).clone(true).css('left', currentPhotoPos).removeClass('selected'));
    currentPhotoPos += 122;
}

function selectPhotoMini(img)
{
    miniaturesPhotos.removeClass('selected');
    mainPhoto.attr('src', img.addClass('selected').attr('src').replace('/mini/', '/maxi/'));
    legendePhoto.html(img.attr('title'));
}

function togglePlay()
{
    if ($(this).hasClass('play'))
    {
        $(this).removeClass('play').addClass('pause');
        clearInterval(timerPhoto);
    }
    else
    {
        $(this).removeClass('pause').addClass('play');
        moveAndSelectPhoto(1);
        timerPhoto = setInterval("moveAndSelectPhoto(1)", 4000);
    }
}

function movePhoto(offset)
{
    if (!isBeingPhotoMove)
    {
        isBeingPhotoMove = true;
        offset = offset > 0 ? -1 : 1;
        miniaturesPhotos.each(function()
        {
            $(this).animate({ 'left': '+=' + (offset * 122) }, updatePhotoPos);
        });
    }
}

function updatePhotoPos()
{
    var left = Number($(this).css('left').replace('px',''));
    if (left > initPhotoPos + (miniaturesPhotos.size() - 2) * 122) $(this).css('left', initPhotoPos - 122);
    else if (left < initPhotoPos - 122) $(this).css('left', initPhotoPos + (miniaturesPhotos.size() - 2) * 122);
    isBeingPhotoMove = false;
}

function selectPhoto(offset)
{    
    if(offset > 0)
    {
        var newimg = $("#navPhoto img.selected").next('img.mini:first');
        if(newimg.is('*')) selectPhotoMini(newimg);
        else selectPhotoMini(miniaturesPhotos.first());
    }
    else
    {
        var newimg = $("#navPhoto img.selected").prev('img.mini:first');
        if(newimg.is('*')) selectPhotoMini(newimg);
        else selectPhotoMini(miniaturesPhotos.last());
    }
}

function moveAndSelectPhoto(offset)
{
    if (!isBeingPhotoMove)
    {
        movePhoto(offset);
        selectPhoto(offset);
    }
}



/*Partie Player Video*/
function initVideoMini()
{
    $(this).css({ position: 'absolute', left: currentVideoPos, top: '0px' });
    currentVideoPos += 122;
}

function endInitVideo()
{
    //Ajoute des photos manquante
    if (miniaturesVideos.size() >= 1)
    {
        var idx = 0;
        while (miniaturesVideos.size() + idx * miniaturesVideos.size() < 8)
        {
            miniaturesVideos.each(addVideo);
            idx++;
        }
        miniaturesVideos = $('#navVideo img.video');
        miniaturesVideos.last().css('left', initVideoPos - 122);
    }
}

function addVideo()
{
    videoNext.before($(this).clone(true).css('left', currentVideoPos).removeClass('selected'));
    currentVideoPos += 122;
}

function selectVideoMini()
{
    miniaturesVideos.removeClass('selected');
    var urlVideo = $(this).addClass('selected').attr('rel');
    $("#mainVideo").html('<param name="movie" value="' + urlVideo + '"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="' + urlVideo + '" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="593" height="289"></embed>');
    
    //mainPhoto.attr('src', img.addClass('selected').attr('src').replace('/mini/', '/maxi/'));
    legendeVideo.html($(this).attr('title'));
}

function moveVideo(offset)
{
    if (!isBeingVideoMove)
    {
        isBeingVideoMove = true;
        offset = offset > 0 ? -1 : 1;
        miniaturesVideos.each(function()
        {
            $(this).animate({ 'left': '+=' + (offset * 122) }, updateVideoPos);
        });
    }
}

function updateVideoPos()
{
    var left = Number($(this).css('left').replace('px', ''));
    if (left > initVideoPos + (miniaturesVideos.size() - 2) * 122) $(this).css('left', initVideoPos - 122);
    else if (left < initVideoPos - 122) $(this).css('left', initVideoPos + (miniaturesVideos.size() - 2) * 122);
    isBeingVideoMove = false;
}
