//these need to be global
var flashVideoPlayer;
var flashVideoPlayer2;

if (typeof console != 'object') {
	console = {
		'log': function (msg) {
			//alert(msg);
		}
	}
}

if(typeof vidWidth == "undefined")
  vidWidth = 335;

if(typeof vidHeight == "undefined")
  vidHeight = 210;

$(document).ready(function () {
    setTimeout('loadCurrentVideo()', 500);
    /*
    if (
        (typeof userMedia != "undefined" && userMedia['type'] == 'video') || 
        (
            typeof userMedia == "undefined" && 
            (typeof isPostcard == "undefined" || isPostcard==false )
        )
       )
    {
        //prepareFlashVideoPlayer();
        //$('#videoPreview,#flvPlayer').hide();
    }
    */
});

function buildFlashVideoPlayer(videoFile,container) {
    destroyFlashVideoPlayer();

    videoFile = videoFile || '';
    var flashvars = {
        trackingSystem:    "Google,HBX,Omniture",
        trackingSystemAccount: "mmediavisitflorida",
        filename:          videoFile,
        playlist:          "",
        autoplay:          true,
        allowFullScreen:   "true"
    };
    var params = {
        wmode:"transparent",
        menu: "false",
        allowFullScreen: "true"
    };
    container = ($('#'+container).length > 0) ? container : 'flvPlayer';
    var attributes = {
        id: container,
        name: container
    };

    var currentVideo;
    swfobject.embedSWF("/share/includes/flvPlayer.swf?cb="+Math.random(100,200), "myContent", vidWidth, vidHeight, "9.0.0","/js_css/swfobject/expressInstall.swf", flashvars, params, attributes);
    $('#videoPreview').hide();
    $('#videoWrapper').css({
        'height':  vidHeight,
        'display': 'inline'
    });
}

function destroyFlashVideoPlayer() {
    $('#videoPreview img').attr('src','/images/shell_v2/spacer.gif');
	$('#videoWrapper')
        .empty()
        .append($(document.createElement('div'))
            .attr('id','myContent')
        );
}

function playVideoEmbed(embedstring) {
    //console.log('playVideoEmbed('+embedstring+')');
	$('#postcardView').hide();
	$('#postcardText').hide();
	$('#videoPreview').hide();
	$('#videoWrapper').show();
	$('#videoWrapper').html(embedstring);
	$('#videoWrapper').css('width','400px');
	$('#videoWrapper').css('height','220px');
}

function playVideo(video,container) {
    //console.log('playVideo('+video+')');

    $('#postcardText').fadeOut(400);
    $('#postcardView').fadeOut(400, function () {

        var thisVideoFile;
        if (typeof(video)=='object'){
               $('#invitationtype').val(video['invitationType']);
               checkToggle($('#'+video['id']));
               thisVideo = video['URLname'];
               thisVideoFile = video['videoFile'];
        } else if (typeof(video)=='string'){
               thisVideoFile = video;
        }

        buildFlashVideoPlayer(thisVideoFile,container);

        // facebook does not include get params in shared links, server config maps /media.var to index.php
        if ($('#fb_share').length > 0) {
            $('#fb_share').attr({
                'href': 'http://www.facebook.com/sharer.php?u=http://www.visitflorida.com/share/media.'+thisVideo+'/?src=sp',
                'share_url': 'http://www.visitflorida.com/share/media.'+thisVideo
            });
        }
    });
}

function playPSAVideo(video2) {
    //console.log('playPSAVideo('+video2+')');
    $('#videoPreview,#flvPlayer').show();
    $('#psaPreview').css('display', 'none');
    $('#videoWrapper2').css('height',vidHeight);

    flashVideoPlayer2 = $('#flvPlayer2').get(0);
    if (typeof flashVideoPlayer2.playVideo != 'undefined') {
        setTimeout("flashVideoPlayer2.playVideo('"+video2+"');",500);
    }
    else {
        //console.log('flashVideoPlayer2.playVideo is undefined:'+flashVideoPlayer2.playVideo);
    }
}

function playRallyVideo(video) {
    //console.log('playPSAVideo('+video2+')');
    $('#videoPreview,#flvPlayer1').show();
    $('#rallyPreview').css('display', 'none');
    $('#videoWrapper').css('height',vidHeight);

    flashVideoPlayer = $('#flvPlayer1').get(0);
    if (typeof flashVideoPlayer.playVideo != 'undefined') {
        setTimeout("flashVideoPlayer.playVideo('"+video+"');",500);
    }
    else {
        //console.log('flashVideoPlayer2.playVideo is undefined:'+flashVideoPlayer2.playVideo);
    }
}

function loadCurrentVideo(){
    //console.log('loadCurrentVideo()');

    if(reqVideo != 'userphoto' && reqVideo != 'uservideo')
      var thisItem = getMedia(reqVideo);
    else
      var thisItem = reqVideo;

    if (typeof thisItem != 'object') {
        //console.log ('thisItem is not an object: '+thisItem);
    }

    if (thisItem){
        if (thisItem['type']=='video'){
            playVideo(thisItem, 'flvPlayer');
        } else if(thisItem['type']=='postcard'){
            showPostcard(thisItem);
        } else if (thisItem=='uservideo'){
            playUserVideo(userMedia['file']);
        } else if(thisItem=='userphoto'){
            showUserPostcard(userMedia);
        }
        $('#invitationtype').val(thisItem['invitationType']);
    }
}

function showPostcard(id) {
    id = eval(id);
    //console.log('showPostcard('+id+')');
    destroyFlashVideoPlayer();
    var $postcardText = $('#postcardText');
    var $postcardView = $('#postcardView');
    var text = ($('#recipientNote').length > 0) ? id.rcText : id.pcText;

    $postcardView
        .addClass('pcWhite')
        .removeClass('pcTrans')
        .html('<img src="'+id['postcardFile']+'" border="0" width="240" />');
        // I think some versions of jQuery do not return the original 
        // object from the html set method. I believe this was causing
        // the undefined method error on line 151 in reference to the 
        // fadeIn method. - CJ

    $postcardText
        .addClass('pcWhite')
        .removeClass('pcTrans');
    if (text)
        $postcardText.html(text); 
    else
        $postcardText.empty();
      
    if(id['URLname'] == 'upload')
      $postcardView.find('img').wrap('<a href="/share/upload.php"></a>');

    $postcardView.fadeIn();
    $postcardText.fadeIn();

    checkToggle($('#'+id['id']));

    thisCard = id['URLname'];

    $('#invitationtype').val(id['invitationType']);
    $('#fb_share').attr({
        'href': 'http://www.facebook.com/sharer.php?' + 
            'u=http://www.visitflorida.com/share/media.'+thisCard+'/?src=sp',
        'share_url': 'http://www.visitflorida.com/share/media.'+thisCard
    });
}

function showUserPostcard(id) {
    //console.log('showUserPostcard('+id+')');
    destroyFlashVideoPlayer();

    $('#postcardView').css('padding-bottom','0px');
    $('#videoPreview img').attr('src','/images/shell_v2/spacer.gif');
    $('#messageView').css('height','auto');
    if(window.location.hostname.indexOf('visitflorida.com') >= 0)
      $('#postcardView').addClass('pcWhite').removeClass('pcTrans').html('<img src="http://www.visitflorida.com:9001'+id['file']+'" border="0" width="240" />').fadeIn();
    else
      $('#postcardView').addClass('pcWhite').removeClass('pcTrans').html('<img src="http://'+window.location.hostname+id['file']+'" border="0" />').fadeIn();
    $('#postcardText').html('<p>'+id['text']+'</p>').show();

    checkToggle($('#'+id['id']));

    thisCard = id['id'];

    $('#invitationtype').val(id['invitationType']);
    $('#fb_share').attr({
        'href': 'http://www.facebook.com/sharer.php?' + 
            'u=http://www.visitflorida.com/share/usershare.php?' +
            'type=photo&id='+id['id']+'&src=sp',
        'share_url': 'http://www.visitflorida.com/share/usershare.php?' + 
            'type=photo&id='+id['id']
    });
}

function playUserVideo(video,player) {
    //console.log('playUserVideo('+video+', '+player+')');

    var thisVideo;
    var videoLocation = '';

    $('#videoPreview').show();

    $('#postcardText').fadeOut(400);
    $('#postcardView').fadeOut(400, function() {

        if(window.location.hostname.indexOf('visitflorida.com') >= 0)
          videoLocation = 'http://www.visitflorida.com:9001';
        else
          videoLocation = 'http://'+window.location.hostname;

        if (typeof(video)=='object'){
            checkToggle($('#'+video['id']));
            thisVideo = video['id'];
            buildFlashVideoPlayer(videoLocation + video['file']);
        } else if (typeof(video)=='string' && (video.indexOf(' src="') > 0)){
            $('#flvPlayer').replaceWith(video);
            return true;
        } else if (typeof(video)=='string'){
            buildFlashVideoPlayer(videoLocation + video);
        }

        // facebook does not include get params in shared links, server config maps /media.var to index.php
        if ($('#fb_share').length > 0) {
            $('#fb_share').attr({
                'href': 'http://www.facebook.com/sharer.php?' +
                    'u=http://www.visitflorida.com/share/usershare.php?' +
                    'type=video&id='+video['id']+'&src=sp',
                'share_url': 'http://www.visitflorida.com/share/usershare.php?' + 
                    'type=video&id='+video['id']
            });
        }

    });
}

function getMedia(id){
    //console.log('getMedia('+id+')');

    for (var i=0; i<smedia.length; i++){

        if (smedia[i].id==id || smedia[i].URLname==id){
            return smedia[i];
        }
    }
    return false;
}
/*
function addOnloadEvent(fnc){
    //console.log('addOnloadEvent('+fnc+')');
    if ( typeof window.addEventListener != "undefined" )
        window.addEventListener( "load", fnc, false );
    else if ( typeof window.attachEvent != "undefined" ) {
        window.attachEvent( "onload", fnc );
    }
    else {
        if ( window.onload != null ) {
            var oldOnload = window.onload;
            window.onload = function ( e ) {
                oldOnload( e );
                window[fnc]();
            };
        }
        else
            window.onload = fnc;
    }
}
*/
function checkToggle(exclude){
    //console.log('checkToggle('+exclude+')');
    $('.oThumb-check')
        .not(exclude)
        .attr('checked','');
    exclude
        .attr('checked','checked');
}


