$(document).ready(function() {
    // Bouton "Jouer"
	$("#menu3").click(function(event) {
		launchGame()
    });//click
    // Bouton "imageIndex"
	$(".play_btn").click(function(event) {
		launchGame();
		event.preventDefault();
    });//click
    //launchInscr
	$(".inscrlauncher").click(function(event) {
		launchInscr();
		event.preventDefault();
    });//click
    // Nouvelles fenetres
    $(".newwindow").click(function(event) {
        window.open($(this).text())
    });

    // Ajout des animations flash
    var params = {
        quality: "high",
        allowscriptaccess: "always",
        bgcolor: "#2681E8"
    };
    var flashvars = {};

    // Illustration
    var attributes = {id:"noflash"};
    if ($("div#noflash").length) {
        swfobject.embedSWF("medias/flash/index-top.swf", "noflash", "950", "550", "9.0.246", "expressInstall.swf", flashvars, params, attributes);
    }
    attributes = {id:"burst_noflash"};
    if ($("div#burst_noflash").length) {
        swfobject.embedSWF("medias/flash/burst_nouveau.swf", "burst_noflash", "152", "53", "9.0.246", "expressInstall.swf", flashvars, params, attributes);
    }

    // Index : effet de rollover
    $(".idx_case").hover(
        function() {
            $(this).children("a").addClass("light");
            var imgName = $(this).children("a").children("img").attr("src");
            imgName = imgName.substr(0, imgName.length - 4) + "_on.png";
            $(this).children("a").children("img").attr("src", imgName);
        }, function () {
            $(this).children("a").removeClass("light");
            var imgName = $(this).children("a").children("img").attr("src");
            imgName = imgName.substr(0, imgName.length - 7) + ".png";
            $(this).children("a").children("img").attr("src", imgName);
        }
    );

    // Coin parents : effet de rollover
    $(".round").hover(
        function() {
            roundOverIn($(this));
        }, function() {
            roundOverOut($(this));
        }
    );

    // Suppression du texte dans un champ de formulaire quand on clique dessus la première fois
    $("div#sendstory form input#name").click(
        function() {
            if ($(this).val() == "Ecrire ici votre nom") {
                $(this).val("");
            }
        }
    );
    $("div#sendstory form textarea#story").click(
        function() {
            if ($(this).val() == "Ecrire ici un témoignage") {
                $(this).val("");
            }
        }
    );

    // Ajout des avatars
    if ($("div.avatar").length) {
        params = {
            quality: "high",
            allowscriptaccess: "always",
            bgcolor: "#F4F4F4",
            wmode: 'transparent'
        };
        $("div.avatar").each(function(i) {
            var element = "element" + i;
            player_id = $("div#" + element + " p.p_id").text();
            flashvars = {id: player_id, animation: 'idle'};
            attributes = {id: element};
            swfobject.embedSWF("medias/flash/avatar_loader.swf", element, "120", "120", "9.0.124", "expressInstall.swf", flashvars, params, attributes);
        });
    }

    // Ajout des stats de temps sur les matieres
    if ($("div.timestats").length) {
        params = {
            quality: "high",
            allowscriptaccess: "always",
            bgcolor: "#FFFFFF"
        };
        player_id = $("div#substats p.p_id").text();
        flashvars = {id: player_id};
        attributes = {id: "substats"};
        swfobject.embedSWF("medias/flash/stats_loader.swf", "substats", "640", "100%", "9.0.124", "expressInstall.swf", flashvars, params, attributes);
    }

    // Ouverture d'un popup
    if ($("a.inpopup").length) {
        var url = '';
        $("a.inpopup").click(
            function() {
                url = $(this).attr("href");
                var winName = Math.round(9999*Math.random()) + new Date().getTime();
                window.open(url,winName,"toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width=820,height="+screen.height+",left=0,top=0,fullscreen=0");
                return false;
            }
        );
    }
    initialiseFeeds();

    // Ergonomie des tableaux
    //$("tr:nth-child(odd)").addClass("odd");
});// document ready

function roundOverIn(div)
{
    var imgName = div.css('backgroundImage');
    imgName = imgName.replace(/['"]/g, "");
    imgName = imgName.substr(0, imgName.length - 5);
    div.css('background-image', imgName + "_on.png)");
    div.children("div.round_top").css('background-image', imgName + "_top_on.png)");
    div.children("div.round_bottom").css('background-image', imgName + "_bottom_on.png)");
}
function roundOverOut(div)
{
    var imgName = div.css('backgroundImage');
    imgName = imgName.replace(/['"]/g, "");
    imgName = imgName.substr(0, imgName.length - 8);
    div.css('background-image', imgName + ".png)");
    div.children("div.round_top").css('background-image', imgName + "_top.png)");
    div.children("div.round_bottom").css('background-image', imgName + "_bottom.png)");
}

function launchGame()
{
    //var url = window.location.host;
    window.open($('base')[0].href + 'index_game.php','',
    'top=0,left=0,width='+screen.width+',height='+screen.height+',directories=no,'
    + 'location=no,menubar=no,resizable=no,scrollbars=no,status=no,'
    + 'toolbar=no,fullscreen=yes');
}

function launchInscr()
{
    //var url = window.location.host;
    window.open($('base')[0].href + 'index_game.html#/inscription','',
    'top=0,left=0,width='+screen.width+',height='+screen.height+',directories=no,'
    + 'location=no,menubar=no,resizable=no,scrollbars=no,status=no,'
    + 'toolbar=no,fullscreen=yes');
}

function hideFlash()
{
    swfobject.removeSWF("noflash");
    $("#flashcontent").html('<div id="noflash"><h1><img src="medias/images/fr/index-thanks.png" id="imageIndex" alt="Image de pr&eacute;sentation de Simply-Land.com" /></h1></div>');
	$("#noflash").css({'visibility' : 'visible'});
    // Bouton "imageIndex"
	$("#imageIndex").click(function(event) {
		launchGame();
    });//click
}

function whatissl()
{
	window.location.href='/parents/#whatis';
}

function parents()
{
	window.location.href='/parents/';
}

function doesntwork()
{
	window.location.href='';
}

function newgame()
{
	window.location.href='';
}

function presskit()
{
	window.location.href='';
}

function contact()
{
	window.location.href='mailto:contact@simply-land.com';
}

var swfId = "flashcontent";

function openWindow(pageUrl) {
    var winName = Math.round(9999*Math.random()) + new Date().getTime();
    var winNew = window.open(pageUrl,winName,"toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width="+screen.width+",height="+screen.height+",left=0,top=0,fullscreen=1");

    if(!winNew) {
        getSwf(swfId).openWindowFromSwf(pageUrl);
    }
    else {
        winNew.focus();
    }
}

function getSwf(id) {
    if (navigator.appName.indexOf("Microsoft") != -1) {
        return window[id];
    }
    else {
        return document[id];
    }
}

function PleinEcran(url) {
    var version;
    version = parseInt(navigator.appVersion);

    if (navigator.appName.indexOf('Netscape') == -1) {
        browser = 'IE';
    } else {
        browser = 'Netscape';
    }

    if (navigator.userAgent.indexOf('Win') == -1) {
        plateForme = 'Macintosh';
    } else {
        plateForme = 'Windows';
    }

    if ((plateForme == 'Windows') && (browser == 'IE')) {
        var fenetre = window.open(url, "fenetreFull", "fullscreen=yes", "scrollbars=no");
        fenetre.moveTo(0,0);
        fenetre.resizeBy(16,0);
    } else {
        if (plateForme == 'Windows') {
            if (browser == 'Netscape') {
                var decalLargeur = -12;
                var decalHauteur = 0;
                var largeurEcran = screen.availWidth + decalLargeur;
                var hauteurEcran = screen.availHeight + decalHauteur;
                var fenetre = window.open(url,"titre","width=" + largeurEcran + ",height=" +hauteurEcran);
                fenetre.moveTo(0 - decalLargeur,0 - decalHauteur);
            }
        } else {
            if (plateForme == 'Macintosh') {
                if (browser == 'Netscape') {
                    var decalLargeur = -10;
                    var decalHauteur = -30;
                    var largeurEcran = screen.availWidth + decalLargeur;
                    var hauteurEcran = screen.availHeight + decalHauteur;
                    var fenetre = window.open(url,"titre","width=" + largeurEcran + ",height=" +hauteurEcran);
                    fenetre.moveTo(0,0);
                }
                if (browser == 'IE') {
                    var decalLargeur = 0;
                    var decalHauteur = 0;
                    var largeurEcran = screen.availWidth + decalLargeur;
                    var hauteurEcran = screen.availHeight + decalHauteur;
                    var fenetre = window.open(url,"titre");
                    fenetre.moveTo(0,0);
                    fenetre.resizeTo(largeurEcran,hauteurEcran);
                }
            }
        }
    }
}

function initialiseFeeds()
{
  initialiseDivFromUrl('daily_feed_container', '/fr/dailyfeed');
  initialiseDivFromUrl('pinpin_feed_container', '/fr/pinpinfeed');
}
