//Random images - change here total images for each folder in other_files
var home_count          = 3;
var things_to_do_count  = 2;
var music_count         = 2;
var stories_count       = 2;
var share_count         = 2;

//DO NOT CHANGE ANYTHING BELOW THIS POINT

function doResize()
{
    stageWidth = document.documentElement.clientWidth;
    stageHeight = document.documentElement.clientHeight;

    width1 = stageWidth;
    height1 = width1 * scaler;

    height2 = stageHeight;
    width2 = height2 / scaler;

    if (width1 >= stageWidth && height1 >= stageHeight)
    {
        newWidth = width1;
        newHeight = newWidth * scaler;
    }
    else if (height2 >= stageHeight && height2 >= stageHeight)
    {
        newHeight = stageHeight;
        newWidth = newHeight / scaler;
    }
    else
    {
        newWidth = stageWidth;
        newHeight = newWidth * scaler;
    }

    $('#pic').css('width', newWidth + 'px');
    $('#pic').css('height', newHeight + 'px');

    $('.overlay').css('height',newHeight);
    $('.overlay').css('width',newWidth);

    $('#navigation-bottom').css('bottom', '0');
}

function doResizeOverlay () {
    stageWidth = document.documentElement.clientWidth;
    stageHeight = document.documentElement.clientHeight;

    width1 = stageWidth;
    height1 = width1 * scaler;

    height2 = stageHeight;
    width2 = height2 / scaler;

    if (width1 >= stageWidth && height1 >= stageHeight)
    {
        newWidth = width1;
        newHeight = newWidth * scaler;
    }
    else if (height2 >= stageHeight && height2 >= stageHeight)
    {
        newHeight = stageHeight;
        newWidth = newHeight / scaler;
    }
    else
    {
        newWidth = stageWidth;
        newHeight = newWidth * scaler;
    }

    $('.overlay').css('height',newHeight);
    $('.overlay').css('width',newWidth);
}


// SHOW and HIDE the main navigation
function checkNavi()
{
    if (naviDo == "hide")
    {
        $("#navigation-bottom").slideUp(400, function() {
            $("#navigation-showHide-btn").text("show navigation");
            $("#navigation-showHide-btn").show();
            $("#navigation-showHide-btn").css('bottom', "0px");
        });
        $("#navigation-showHide-btn").hide();
    }
}

// function to resize the video
// made it an extra function because there might come a better way of scaling with flash player 10.1


function doResizeVideo()
{
    if (videoSize == "large")
    {
        stageWidth = document.documentElement.clientWidth;
        stageHeight = document.documentElement.clientHeight;

        width1 = stageWidth;
        height1 = width1 * scaler_video;

        height2 = stageHeight;
        width2 = height2 / scaler_video;

        if (height1 >= stageHeight)
        {
            newWidth = width1;
            newHeight = newWidth * scaler_video;
        }
        else if (height2 >= stageHeight)
        {
            newHeight = stageHeight;
            newWidth = newHeight / scaler_video;
        }
        else
        {
            newWidth = stageWidth;
            newHeight = newWidth * scaler_video;
        }


        $('#mediaspace').css('width', newWidth + 'px');
        $('#mediaspace').css('height', newHeight + 'px');
    }
}

var cameras = {
    //'skolavordustigur' : {name : 'Skólavörðustígur', video : 'skolavordustigur.stream'},
    'jokulsarlon' :{name : 'Jökulsárlón', video : 'jokulsarlon.stream'},
    'austurstraeti' :{name : 'Tjörnin', video : 'austurstraeti.stream'},
    'gullfoss' :{name : 'Bláa lónið', video : 'gullfoss.stream'},
    'blaa-lonid' :{name : 'Bláa lónið', video : 'gullfoss.stream'},
    'tjornin' :{name : 'Tjörnin', video : 'austurstraeti.stream'}
};

function selectCamera(which){

    var camera = cameras[which];

    var so = new SWFObject('/other_files/flash/player-licensed.swf', 'mpl', '100%', '100%', '9');
    //var so = new SWFObject( 'other_files/flash/test.swf','mpl','100%','100%','9' );
    so.addParam('allowfullscreen', 'true');
    so.addParam('allowscriptaccess', 'always');
    so.addParam('wmode', 'transparent');
    so.addVariable('start', '0');
    so.addVariable('autostart', 'true');
    so.addVariable('mute', 'false');
    so.addVariable('provider', 'rtmp');
    so.addVariable('bufferlength', '1');



    so.addVariable('file', camera.video);
    so.addVariable('streamer', 'rtmp://' + tengipunktur + '/inspired/');
    so.addVariable('controlbar', 'none');
    so.addVariable('scale', 'exactfit');
    so.addVariable('stretching', 'exactfit');
    so.addVariable('align', 't');
    so.write('mediaspace');

    recentCamera = activeCamera;
    activeCamera = which;
    $('#'+recentCamera).removeClass("camera-selected");
    $('#'+activeCamera).addClass("camera-selected");

    $('#'+recentCamera).stop().fadeTo('fast', 1);

    $('#'+which).pulse({
        opacity: [0,1]
    }, {
        times: 999999
    });

    window.location.hash = which;
    setInfoText(camera.name)
}

function setInfoText(locationName)
{
    document.title = locationName + ' - Live Webcam - Inspired by Iceland';
    $('#camera-location').text(locationName + ': ');
}


function initClock()
{
    $(function($) {
        var options1 = {
            utc: true,
            format: '%A, %B %d, %Y - %I:%M:%S %P' // 12-hour
        }
        $('#jclock1').jclock(options1);
    });
}

function switchVideoSize()
{
    if (videoSize == 'large')
    {
        $('#mediaspace').css('width', videoWidth + 'px');
        $('#mediaspace').css('height', videoHeight + 'px');
        $('#mediaspace').css('position', 'absolute');
        $("#mediaspace").css('left', "50%");
        $("#mediaspace").css('margin-left', "-400px");
        $("#mediaspace").css('top', "15%");
        //$( "body" ).css( 'background-image', "url(/other_files/backgrounds/icelandlive/background_live_1.jpg)" );
        //$( "body" ).css( 'background-position', "center center" );
        $("body").css('background-color', "#EAEAEA");
        $("#mediaspace").addClass("video-border");
        $("#video-btn").removeClass("video-btn-bigview");
        $("#video-btn").addClass("video-btn-smallview");
        videoSize = "small";
    }
    else
    {
        $("#mediaspace").css('left', "0");
        $("#mediaspace").css('margin-left', "0");
        $("#mediaspace").css('top', "0");
        $("#mediaspace").removeClass("video-border");
        $("#video-btn").removeClass("video-btn-smallview");
        $("#video-btn").addClass("video-btn-bigview");
        videoSize = "large";
        doResizeVideo();
    }
}


function hideNaviTop()
{
    $('#navigation-top-left').hide();
    $('#navigation-top-right').hide();
}
function showNaviTop()
{
    $('#navigation-top-left').show();
    $('#navigation-top-right').show();
}


// VIDEO ON HOME

// init videos
function initHomeVideo()
{
    video1 = '<div id=\"video-container\"><object width=\"420\" height=\"315\"><param name=\"wmode\" value=\"transparent\" /><param name=\"allowfullscreen\" value=\"true\" /><param name=\"allowscriptaccess\" value=\"always\" /><param name=\"movie\" value=\"http://vimeo.com/moogaloop.swf?clip_id=12236680&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=0&amp;show_portrait=0&amp;color=&amp;fullscreen=1\" /><embed src=\"http://vimeo.com/moogaloop.swf?clip_id=12236680&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=0&amp;show_portrait=0&amp;color=&amp;fullscreen=1\" type=\"application/x-shockwave-flash\" allowfullscreen=\"true\" allowscriptaccess=\"always\" width=\"420\" height=\"315\"></embed></object></div>';
    video2 = '<div id=\"video-container\"><object width=\"420\" height=\"315\"><param name=\"wmode\" value=\"transparent\" /><param name=\"allowfullscreen\" value=\"true\" /><param name=\"allowscriptaccess\" value=\"always\" /><param name=\"movie\" value=\"http://vimeo.com/moogaloop.swf?clip_id=12242033&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=0&amp;show_portrait=0&amp;color=&amp;fullscreen=1\" /><embed src=\"http://vimeo.com/moogaloop.swf?clip_id=12242033&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=0&amp;show_portrait=0&amp;color=&amp;fullscreen=1\" type=\"application/x-shockwave-flash\" allowfullscreen=\"true\" allowscriptaccess=\"always\" width=\"420\" height=\"315\"></embed></object></div>';
    video3 = '<div id=\"video-container\"><object width=\"420\" height=\"315\"><param name=\"wmode\" value=\"transparent\" /><param name=\"allowfullscreen\" value=\"true\" /><param name=\"allowscriptaccess\" value=\"always\" /><param name=\"movie\" value=\"http://vimeo.com/moogaloop.swf?clip_id=12273516&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=0&amp;show_portrait=0&amp;color=&amp;fullscreen=1\" /><embed src=\"http://vimeo.com/moogaloop.swf?clip_id=12273516&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=0&amp;show_portrait=0&amp;color=&amp;fullscreen=1\" type=\"application/x-shockwave-flash\" allowfullscreen=\"true\" allowscriptaccess=\"always\" width=\"420\" height=\"315\"></embed></object></div>';
    video4 = '<div id=\"video-container\"><object width=\"420\" height=\"315\"><param name=\"wmode\" value=\"transparent\" /><param name=\"allowfullscreen\" value=\"true\" /><param name=\"allowscriptaccess\" value=\"always\" /><param name=\"movie\" value=\"http://vimeo.com/moogaloop.swf?clip_id=12067783&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=0&amp;show_portrait=0&amp;color=&amp;fullscreen=1\" /><embed src=\"http://vimeo.com/moogaloop.swf?clip_id=12067783&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=0&amp;show_portrait=0&amp;color=&amp;fullscreen=1\" type=\"application/x-shockwave-flash\" allowfullscreen=\"true\" allowscriptaccess=\"always\" width=\"420\" height=\"315\"></embed></object></div>';
    video5 = '<div id=\"video-container\"><object width=\"420\" height=\"315\"><param name=\"wmode\" value=\"transparent\" /><param name=\"allowfullscreen\" value=\"true\" /><param name=\"allowscriptaccess\" value=\"always\" /><param name=\"movie\" value=\"http://vimeo.com/moogaloop.swf?clip_id=11838391&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=0&amp;show_portrait=0&amp;color=&amp;fullscreen=1\" /><embed src=\"http://vimeo.com/moogaloop.swf?clip_id=11838391&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=0&amp;show_portrait=0&amp;color=&amp;fullscreen=1\" type=\"application/x-shockwave-flash\" allowfullscreen=\"true\" allowscriptaccess=\"always\" width=\"420\" height=\"315\"></embed></object></div>';

    activeVideo = "#t-video1";

    $("#t-video1").click(function() {
        loadVideo(1, this);
    });
    $("#t-video2").click(function() {
        loadVideo(2, this);
    });
    $("#t-video3").click(function() {
        loadVideo(3, this);
    });
    $("#t-video4").click(function() {
        loadVideo(4, this);
    });
    $("#t-video5").click(function() {
        loadVideo(5, this);
    });

    loadVideo(1, activeVideo);

}

// init videos on music page
function initMusicVideo()
{
    video1 = '<div id=\"video-container\"><object width=\"420\" height=\"315\"><param name=\"wmode\" value=\"transparent\" /><param name=\"allowfullscreen\" value=\"true\" /><param name=\"allowscriptaccess\" value=\"always\" /><param name=\"movie\" value=\"http://vimeo.com/moogaloop.swf?clip_id=12919389&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=0&amp;show_portrait=0&amp;color=&amp;fullscreen=1\" /><embed src=\"http://vimeo.com/moogaloop.swf?clip_id=12919389&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=0&amp;show_portrait=0&amp;color=&amp;fullscreen=1\" type=\"application/x-shockwave-flash\" allowfullscreen=\"true\" allowscriptaccess=\"always\" width=\"420\" height=\"315\"></embed></object></div>';
    video2 = '<div id=\"video-container\"><object width=\"420\" height=\"315\"><param name=\"wmode\" value=\"transparent\" /><param name=\"allowfullscreen\" value=\"true\" /><param name=\"allowscriptaccess\" value=\"always\" /><param name=\"movie\" value=\"http://vimeo.com/moogaloop.swf?clip_id=12919818&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=0&amp;show_portrait=0&amp;color=&amp;fullscreen=1\" /><embed src=\"http://vimeo.com/moogaloop.swf?clip_id=12919818&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=0&amp;show_portrait=0&amp;color=&amp;fullscreen=1\" type=\"application/x-shockwave-flash\" allowfullscreen=\"true\" allowscriptaccess=\"always\" width=\"420\" height=\"315\"></embed></object></div>';
    video3 = '<div id=\"video-container\"><object width=\"420\" height=\"315\"><param name=\"wmode\" value=\"transparent\" /><param name=\"allowfullscreen\" value=\"true\" /><param name=\"allowscriptaccess\" value=\"always\" /><param name=\"movie\" value=\"http://vimeo.com/moogaloop.swf?clip_id=12921458&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=0&amp;show_portrait=0&amp;color=&amp;fullscreen=1\" /><embed src=\"http://vimeo.com/moogaloop.swf?clip_id=12921458&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=0&amp;show_portrait=0&amp;color=&amp;fullscreen=1\" type=\"application/x-shockwave-flash\" allowfullscreen=\"true\" allowscriptaccess=\"always\" width=\"420\" height=\"315\"></embed></object></div>';
    
    activeVideo = "#t-video1";

    $("#t-video1").click(function() {
        loadVideo(1, this);
    });
    $("#t-video2").click(function() {
        loadVideo(2, this);
    });
    $("#t-video3").click(function() {
        loadVideo(3, this);
    });

    loadVideo(1, activeVideo);

}


// SELECT and DESELECT the video navigation items
function selectVideoNavigation(which)
{
    recentVideo = activeVideo;
    activeVideo = which;
    $(recentVideo).removeClass("selected");
    $(activeVideo).addClass("selected");
}

// load a video
function loadVideo(whichVideo, whichButton)
{
    $("#video-container").replaceWith(eval("video" + whichVideo));

    selectVideoNavigation(whichButton);
}

function add_commas(number) {
  if (number.length > 3) {
    var mod = number.length % 3;
    var output = (mod > 0 ? (number.substring(0, mod)) : '');
    for (i = 0; i < Math.floor(number.length / 3); i++) {
      if ((mod == 0) && (i == 0)) {
        output += number.substring(mod + 3 * i, mod + 3 * i + 3);
      } else {
        output += ',' + number.substring(mod + 3 * i, mod + 3 * i + 3);
      }
    }
    return (output);
  } else {
    return number;
  }
} 



$(document).ready(function() {

    // we might want to scale some images, keeping their proportions
    /*
     scaler = 1200 / 1920;
     doResize();

     $(window).resize(function() {
     doResize();
     });
     */

    // choose background image randomly
    // in the final version of the site, every 'section' will have it's own, individual background
    /*
     var randomNum=Math.floor(Math.random()*5);
     imgsrc = "/other_files/backgrounds/home/background_" + randomNum + ".jpg";
     $( 'body' ).css( 'background-image', 'url(' + imgsrc + ')' );
     */

     scaler = 1200 / 1920;
     //doResizeOverlay();

    // TOP NAVIGATION
    // FLIGHTS Overlay
    $("#navigation-top-flight").hover(
            function () {
                $(".flights").css('display', "inline");
            },
            function () {
                $(".flights").css('display', "none");
            }
            );

    // LANGUAGE Overlay
    $("#navigation-top-language").hover(
            function () {
                $(".languages").css('display', "inline");
            },
            function () {
                $(".languages").css('display', "none");
            }
            );


    // MAIN NAVIGATION
    // SHOW and HIDE the main navigation
    $("#navigation-showHide-btn").click(function () {
        if ($("#navigation-bottom").is(":hidden")) {
            $("#navigation-bottom").slideDown(400, function() {
                $("#navigation-showHide-btn").text("hide navigation");
                $("#navigation-showHide-btn").css('bottom', "124px");
                $("#navigation-showHide-btn").show();
            });
            $("#navigation-showHide-btn").hide();
            // call to jStore to save the current state/position of the main navigation
            telljStore("no");
        }
        else
        {
            $("#navigation-bottom").slideUp(400, function() {
                $("#navigation-showHide-btn").text("show navigation");
                $("#navigation-showHide-btn").show();
                $("#navigation-showHide-btn").css('bottom', "0px");
            });
            $("#navigation-showHide-btn").hide();

            // call to jStore to save the current state/position of the main navigation
            telljStore("yes");
        }
    });


    // MAIN NAVIGATION
    // SELECT and DESELECT the navigation items
    var activeNavi = "#navi1";

    $(this).find(".navi-elements.navi-border").click(function() {
        selectMainNavigation(this, true);
    });

    $(this).find(".navi-elements.navi-logo").click(function() {
        selectMainNavigation(this, false);
    });

    function selectMainNavigation(which, showBackground)
    {
        recentNavi = activeNavi;
        activeNavi = which;
        $(recentNavi).removeClass("navi-selected");
        if (showBackground == true)
        {
            $(activeNavi).addClass("navi-selected");
        }
    }

    $(this).find("#navigation-top-left ul li a").click(function() {
        selectSubNavigation(this);
    });


    // SUB NAVIGATION (top left)
    // SELECT and DESELECT the navigation items
    var activeSubnav = "#subnav1";

    function selectSubNavigation(which)
    {
        recentSubnav = activeSubnav;
        activeSubnav = which;
        $(recentSubnav).removeClass("subnav-selected");
        $(activeSubnav).addClass("subnav-selected");
    }


    // if the current page is 'home', the following executes:
    if ($('#home').length != 0 || $('#home-2jun').length != 0 || $('#home-3jun').length != 0 || $('#home-okt').length != 0)
    {
        // init video
        initHomeVideo();

		if (home_count > 1) {
          var randomNum = Math.floor(Math.random() * home_count);
          imgsrc = "http://www.inspiredbyiceland.com/other_files/backgrounds/home/background_home_" + randomNum + ".jpg";
          $('body').css('background-image', 'url(' + imgsrc + ')');
		}

        //Counter
        $.getJSON('http://stories.inspiredbyiceland.com/story.json?&callback=?',
                function(data) {
                    $('#counter').text(data.globalcount)
                });
    }

    // if the current page is 'things to do', the following executes:
    if ($('#things-to-do').length != 0)
    {
        // set the main navigation
        selectMainNavigation("#navi1", true);
		
		if (things_to_do_count > 1) {
          var randomNum = Math.floor(Math.random() * things_to_do_count);
          imgsrc = "http://www.inspiredbyiceland.com/other_files/backgrounds/thingstodo/background_thingstodo_" + randomNum + ".jpg";
          $('body').css('background-image', 'url(' + imgsrc + ')');
		}	
    }

    // if the current page is 'iceland live', the following executes:
    if ($('#icelandlive').length != 0)
    {
        // set the main navigation
        selectMainNavigation("#navi2", true);

        videoWidth = 800;
        videoHeight = 450;
        scaler_video = videoHeight / videoWidth;

        $('#mediaspace').css('width', videoWidth + 'px');
        $('#mediaspace').css('height', videoHeight + 'px');

        videoSize = "large";
        doResizeVideo();
        $(window).resize(function() {
            doResizeVideo();
        });

        // set the active camera

        if(window.location.hash.length > 1){
           activeCamera = window.location.hash.substring(1);
        } else {
           activeCamera = "blaa-lonid";
        }

        selectCamera(activeCamera);
        initClock();


        var chooseCameraText = "Choose Camera";


        // Cameras rollovers
        $("#video-navigation div").mouseover(function() {
            $("#cameras-text").text(cameras[this.id].name);
        });

        // Cameras rollout
        $("#video-navigation div").mouseout(function() {
            $("#cameras-text").text(chooseCameraText);
        });

        // Cameras select
        $("#video-navigation div").click(function() {
            selectCamera(this.id);
        });

    }


    // if the current page is 'music', the following executes:
    if ($('#music').length != 0)
    {
        // set the main navigation
        selectMainNavigation("#navi3", true);
		
		if (music_count > 1) {
          var randomNum = Math.floor(Math.random() * music_count);
          imgsrc = "http://www.inspiredbyiceland.com/other_files/backgrounds/music/background_music_" + randomNum + ".jpg";
          $('body').css('background-image', 'url(' + imgsrc + ')');
		}		

        //init video player
        //initMusicVideo();

        // initialize the countdown
        /*
         $(function () {
         var austDay = new Date();
         // test date is: may 27, 2010, 8pm
         austDay = new Date(2010, 4, 27, 20, 0, 0, 0);
         $('#defaultCountdown').countdown({until: austDay});
         $('#year').text(austDay.getFullYear());
         });
         */

        // function to open the player in it's own window
        // put the player to the left of the screen
/*
        playerXpos = screen.width - 320;
        $('.musicplayer').popupWindow({
            height:900,
            width:320,
            top:0,
            left:playerXpos,
            scrollbars:1
        });
*/
    }

    // if the current page is 'music_1', the following executes:
    if ($('#music_1').length != 0)
    {
        initMusicVideo();
    }

    // if the current page is 'stories', the following executes:
    if ($('#stories').length != 0 || $('#stories-list').length != 0 || $('#stories-map').length != 0 || $('#stories-story').length != 0 || $('#stories-tellyourstory').length != 0 || $('#spread-the-word.featured').length != 0 || $('#spread-the-word.activity').length != 0)
    {
        // set the main navigation
        selectMainNavigation("#navi4", true);
        if ($('#stories-map').length == 0)
        {
            var randomNum = Math.floor(Math.random() * 2);
            imgsrc = "http://www.inspiredbyiceland.com/other_files/backgrounds/stories/background_stories_" + randomNum + ".jpg";
            $('body').css('background-image', 'url(' + imgsrc + ')');
        }
    }

    // if the current page is 'share', the following executes:
    if ($('#share').length != 0 || $('#share-ecards').length != 0 || $('#share-ecards-write').length != 0 || $('#share-ecards-read').length != 0)
    {
        // set the main navigation
        selectMainNavigation("#navi5", true);
		if (share_count > 1) {
          var randomNum = Math.floor(Math.random() * share_count);
          imgsrc = "http://www.inspiredbyiceland.com/other_files/backgrounds/share/background_share_" + randomNum + ".jpg";
          $('body').css('background-image', 'url(' + imgsrc + ')');
		}
       		

        if ($('#share.banners').length != 0)
        {
            $("#bannerbox-right").fadeIn(1800);
            $("#arrow_badge").fadeIn(3400);
        }
    }

    // if the current page is 'activity', the following executes:
    if ($('#stories.activity').length != 0 || $('#stories.featured').length != 0)
    {
        //Counter
        $.getJSON('http://stories.inspiredbyiceland.com/story.json?&callback=?',
                function(data) {
                    $('#counter').text(data.globalcount)
                });
    }

    // if the current page is 'home3-jun', the following executes:
    if ($('#home-3jun').length != 0 || $('#splash-test').length != 0)
    {
      //$("a#splash").fancybox();
      $("a#splash").fancybox({
        'width' : '914',
        'height' : '464', 
        'autoScale' : false,
        'transitionIn' : 'none',
        'transitionOut' : 'none',
        'overlayColor' : '#overlayColor',
        'type' : 'iframe'
      }); 
      $('a#splash').click();
   

    }

    if ($('#music-sudur').length != 0)
    {
      function startBlink(){
        window.blinker = setInterval(function(){
          if(window.blink){
             $('a.playing').css('color','#2E9C9A');
             window.blink=false;
          }
          else{
             $('a.playing').css('color','white');
             window.blink = true;
          }
      },1000);
    }

  function stopBlink(){
    if(window.blinker) clearInterval(window.blinker);
  } 

startBlink()
    }

});
