//utils golbal variables

//define url and ancre
var url = document.location.toString();
var ancre = false;
var _limit = url.indexOf('#');
if(_limit>0 && _limit != (url.length-1)){
    ancre = url.slice((_limit+1));
    url = url.slice(0,_limit);
}

//utils object and functions

//Function for ie6 test
var ie6 = function(){
    //alert('Version : '+jQuery.browser.version);
    if(jQuery.browser.msie && jQuery.browser.version < 7) return true;
    else return false;

}


//use seekTo on on swf player
function onYouTubePlayerReady(playerId) {
    ytplayer = document.getElementById("myytplayer");
    if(Player.time != 'undefine'){
        ytplayer.seekTo(Player.time, true);
    }
}


function loadVideo()
{
    var clip = $("#video").val();
    //alert(clip);
    swfobject.embedSWF("swf/flowplayer-3.1.4.swf", "playerobj", "425", "356", "9.0.0", null, {
                config: "{'clip': 'videos/"+clip+"' }}"
            }
            );
}

var Player ={
    init:function(){
        Player.div = $('#player');
        Player.div.css('display','block')
        Player.width = Player.div.css('width');
        Player.height = Player.div.css('height');
        Player.alert = 'La visualisation de la video requiert le Flash Player 8';
        Player.content = '<div class="head">\n\
                <a href="#" class="close">Fermer la video <img src="img/close_vid.gif" alt="" /></a>\n\
                    </div><div id="playerobj" >Chargement ...</div>';
        Player.close();
        Player.time = 'undefine';
    },

    play:function(videoID, time){		
        Player.time = time;
        Player.div
        .animate({
            height:15
        })
        .animate({
            width:Player.width
            })
        .animate({
            height:Player.height
            },200,'linear', function(){
            var params = {
                allowScriptAccess: "always",
                allowfullscreen:'true',
                wmode:'transparent'
            };
            var atts = {
                id: "myytplayer"
            };
            swfobject.embedSWF("http://www.youtube.com/v/"+videoID+"&amp;border=0&amp;enablejsapi=1&amp;playerapiid=ytplayer","playerobj", "425", "356", "8", null, null, params, atts);
        } );
    },
	
    playLilly:function(videoID, time){		
        Player.time = time;
        Player.div
        .animate({
            height:15
        })
        .animate({
            width:Player.width
            })
        .animate({
            height:Player.height
            },200,'linear', function(){
            var params = {
                allowScriptAccess: "always",
                allowfullscreen:'true',
                wmode:'transparent'
            };
            var atts = {
                id: "myytplayer"
            };

            //Homepage
            if( $("#video").val() == "" )
            {
                swfobject.embedSWF("http://formation.lillypro.net/spot/SPOT_lilly.swf","playerobj", "425", "356", "8", null, null, params, atts);
            }
            else
            {
                loadVideo();
            }

            

        } );
    },	
    
    close:function(){
        Player.time = 'undefine';
        Player.div.css({
            height:0,
            width:0
        })
        Player.div.html(Player.content);
        $('.head').click(function(){
            Player.close()
            } );
    }
};
var initQuizz = function(){
    if($('#formTestHomme').is('form')){
        //TEST
        $('.blocQuestion input').click(function(){
            $('#'+$(this).attr('name').replace('question','reponse')).attr('value',1);
        });

        $('form').submit(function(){
            var test = true;
            $('.blocQuestion input[type=hidden]').each(function(){ 
                if(this.value != 1) test = false;
            });
            if(test){ 
                this.submit();
            }
            else {
                $('#errorForm')
                .css('opacity',0)
                .html('Vous n\'avez pas repondu &agrave; toutes les questions.')
                .animate({
                    opacity:1
                })
                return false;
            }
        });
    }
}


var part = function(partName){
    
    var initVidBtn = function()
    {
        $('.vid').click(function()
        {
            var videoID = $(this).attr('href');
            var aBuffer = videoID.split(':');
            Player.play(aBuffer[1],0);
        //Player.play('sdfsdqfqsdfsd', $(this).attr('href').replace('#', '') );
        })
    }

    $('#part').prepend('<p class="bgdiv"><span></span></p>');
    $('#part').append('<p class="cursor"></p>');
    var nb_h3 = 0;
    initVidBtn();
    initQuizz();
    
    var c = $('#part > div:eq(0)');
    c.addClass('js-skin');
    $('#part').height(c.height());
    $('#part .cursor').css({
        top:$('#part .nav > li').eq(1).position().top-20
        })

    $('#part .nav a:not(.activ)').click(function(){
        var part = $(this).attr('href').slice(-1);
        var navPos = $(this).parent('li').position().top+20
        $.ajax({
            url:'block/part/'+partName+'/'+part+'.html',
            dataType: "html",
            success:function(a){
                c.remove();
                $('#part').append(a);
                c = $('#part > div:eq(0)');
                c.addClass('js-skin');
                c.css('opacity',0);
                $('#part').animate({
                    height:c.height()
                    });
                $('#part .cursor').animate({
                    top:navPos
                });
                $('#part .bgdiv')
                .animate({
                    width:0,
                    height:20
                },200)
                .animate({
                    width:500
                }, 250)
                .animate({
                    height:c.height()+15
                    },300,'linear',function(){
                    c.animate({
                        opacity:1
                    });
                     
                    initVidBtn();
                    initQuizz();
                });

            }
            })
       

        return false;

    });


    //replace cursor
    var currentUrl = document.location.toString();
    if(currentUrl.indexOf('?p=') > -1){
        var currentPart = parseInt(currentUrl.charAt(currentUrl.length - 1)) - 1;
         $('#part .cursor').css({
            top:$('#part .nav a:eq('+currentPart+')').parent('li').position().top+20
         });

    }

};

function wtTrkLink(oAnk) {
    //Get the Anchor text content
    var sTi = oAnk.textContent;
    //Replace FR characters
    var sUri = stripFRChrs(sTi);
    //Remove non supported characters for URI
    sUri = sUri.replace(/[^a-zA-Z 0-9]+/g, '');
    //Replace Space with underscore for URI
    sUri = sUri.split(' ').join('_');
    //Track Event
    dcsMultiTrack("DCS.dcsuri", "/" + sUri.toLowerCase(), "WT.ti",
        sTi);

}

function stripFRChrs(str) {
    var s = str;

    var rExps = [/[\xC0-\xC2]/g, /[\xE0-\xE2]/g,
    /[\xC8-\xCA]/g, /[\xE8-\xEB]/g,
    /[\xCC-\xCE]/g, /[\xEC-\xEE]/g,
    /[\xD2-\xD4]/g, /[\xF2-\xF4]/g,
    /[\xD9-\xDB]/g, /[\xF9-\xFB]/g,
    /[\u1E08\u00C7]/g, /[\u00E7]/g,
    /[\u0152]/g, /[\u0153]/g,
    /[\u0178]/g, /[\u00FF]/g];

    var repChar = ['A', 'a', 'E', 'e', 'I', 'i', 'O', 'o', 'U', 'u',
    'C', 'c', 'OE', 'oe', 'Y', 'y'];

    for (var i = 0; i < rExps.length; i++)
        s = s.replace(rExps[i], repChar[i]);

    return s;
}
