﻿var youtubePlayer;
var hasFlash = true;
var changeFunctions = new Object();
changeFunctions['feature'] = changeFeature;
changeFunctions['#feature'] = changeFeature;
changeFunctions['wallpaper'] = changeWallpaper;
changeFunctions['#wallpaper'] = changeWallpaper;
changeFunctions['gallery'] = changeGallery;
changeFunctions['#gallery'] = changeGallery;
changeFunctions['video'] = changeVideo;
changeFunctions['#video'] = changeVideo;

$(document).ready(function() {
    var flashVersion = swfobject.getFlashPlayerVersion();
    // handle the case where flash is not installed
    if (flashVersion.major == 0) { hasFlash = false; $('#videoControlBg').hide(); }
    //$('.content').addClass('mediaContentBg');

    if (hasFlash && (!youtubePlayer || youtubePlayer == null)) {
        var ops = {
            playerId: 'videoPlayer',
            infoId: 'videoInfo',
            viewcountId: 'videoViewcount',
            videoHeight: '365',
            videoWidth: '648'
        };
        youtubePlayer = new YoutubePlayer(ops);

        if (featureVideo && featureVideo != null && featureVideo != '') {
            changeVideo(featureVideo);
        }
    } else if (!hasFlash) {
        $('#featureWrapper').hide();
    }

    $.History.bind(onHashChange);

    jQuery("#mycarousel").jcarousel({
        scroll: 3,
        initCallback: mycarousel_initCallback,
        buttonNextCallback: mycarousel_buttonNextCallback,
        buttonPrevCallback: mycarousel_buttonPrevCallback,
        buttonNextHTML: null,
        buttonPrevHTML: null
    });

    //$(window).scroll(function() {
    $('.ui-dialog').center();
    //});

    $("a.goto_top").click(function() { //for stupid IE
        if ($.browser.msie) {
            $("html, body, html body").animate({ scrollTop: 0 }, "slow");
        }
    });
	
	$("a.fixielink").live('click', function(e) {  //for stupid IE
		if ($.browser.msie) {
			e.preventDefault();
			window.location.hash = $(this).attr('href');
		}
	});
		
});

jQuery.fn.center = function () {
	this.css("position","absolute");
	this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px");
	this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
	return this;
}

function onHashChange(state) {
    var args = state.split(';');

    for (i = 0; i < args.length; i++) {
        var kv = args[i].split('=');
        if (kv.length == 2) {
            changeFunctions[kv[0]](kv[1]);
        }
	}
}

function changeFeaturePage(page) {
    $(".featurePage").hide();
    $("#featurePage" + page).show();
}

function changeFeature(feature) {
    // don't do anything if flash player is not available
    if (!hasFlash) { return; }
    if (youtubePlayer) { youtubePlayer.stopVideo(); }
    $.getJSON(featureDataUrl + '/' + feature, function(data) {
        var playerId = 'player_' + feature;
        $('#flashPlayer').html('<div id="' + playerId + '"/>');
        $('#flashHeader').html('<span class="title">' + data.Title + '</span>');
        $('#flashDescription').html(data.Description);
        $('#flashPopup').show().focus().dialog({ 
			modal: true, draggable: false, minHeight: 677, minWidth: 965, resizable: false, 
			close: function(event, ui) { event.preventDefault(); swfobject.removeSWF(playerId); },
			open: function (event, ui) { 
				$(".ui-dialog-titlebar-close").click( function() { event.preventDefault(); window.location.hash = 'none'; } );
				$('.ui-widget-overlay').bind('click', function(){ $("#flashPopup").dialog('close');window.location.hash = 'none'; });
			}
		});

        var params = { allowScriptAccess: "always", flashVars: 'featureName=' + feature, base: '/Content/flash/features', wmode: 'transparent' };
        var atts = { id: playerId };
        swfobject.embedSWF('/Content/flash/features/content/' + feature + '/index.swf', playerId, "935", "507", "8", null, null, params, atts);
    });
}

function changeWallpaper(wallpaper) {
    $('.wall').hide();
    $(wallpaper).show();
}

function changeGallery(gallery) {
    if (youtubePlayer && hasFlash) { youtubePlayer.stopVideo(); }
    $.getJSON(galleryStreamUrl + '/' + gallery, function(data) {
        gid = 'gallery' + gallery;
        var slides = '<div id="' + gid + '">';
        var content;
        for (i = 0; i < data.length; i++) {
            var title = encodeURI(data[i].Title.replace(/ /gi, '_'));
            slides += '<img class="scrollContentItem" src="' + data[i].Filename + '" />';
        }
        slides += '</div>';
        $('#galleryContent').html(slides);
        $('#' + gid).cycle({ prev: "#galleryPrev", next: "#galleryNext" });
        $('#galleryPopup').show().focus().dialog({
			title: gallery, modal: true, draggable: false, minHeight: 550, minWidth: 833, resizable: false, 
			close: function(event, ui) { event.preventDefault(); $('#' + gid).cycle('destroy'); },
			open: function (event, ui){
				$(".ui-dialog-titlebar-close").click( function() { event.preventDefault(); window.location.hash = 'none'; } );
				$('.ui-widget-overlay').bind('click', function(){ $("#galleryPopup").dialog('close');window.location.hash = 'none'; });
			}
		});
    });
}

function changeGalleryPage(page) {
    $(".galleryPage").hide();
    $("#galleryPage" + page).show();
}

function changeVideo(video) {
    // handle the case where flash is not installed
    if (!hasFlash) {
        // send user to youbute instead
        window.open('http://www.youtube.com/watch?v=' + video);
        return;
    }

    if (youtubePlayer) { youtubePlayer.stopVideo(); }
    // load video
    youtubePlayer.initPlayer(video);
	if ( !$.browser.msie ) {
		$("html:not(:animated)"+( ! $.browser.opera ? ",body:not(:animated)" : "")).animate({scrollTop : 0},'slow');
	}
}

function changeVideoPage(page) {
    $(".videoPage").hide();
    $("#videoPage" + page).show();
}

function likeVideo(videoId, liked) {
    // send user to get authsub token
    var scope = encodeURI('http://gdata.youtube.com');
    var token = google.accounts.user.checkLogin(scope);
    if (token == undefined || token == '') {
        token = google.accounts.user.login(scope);
    }
    var xml = '<?xml version="1.0" encoding="UTF-8"?><entry xmlns="http://www.w3.org/2005/Atom" xmlns:yt="http://gdata.youtube.com/schemas/2007"><yt:rating value="' + liked + '"/></entry>';
    $.post('http://gdata.youtube.com/feeds/api/videos/' + videoId + '/ratings', xml);
}

function onYouTubePlayerReady(playerId) {
    youtubePlayer.loadVideo();
    $('#videoContent').focus();
}

function loadVideo(videoId) {
    youtubePlayer.loadVideo(videoId);
}

function playVideo() {
    youtubePlayer.playVideo();
}

function pauseVideo() {
    youtubePlayer.pauseVideo();
}

function mycarousel_initCallback(carousel) {
    jQuery('#mycarousel-next').bind('click', function() {
        carousel.next();
        return false;
    });

    jQuery('#mycarousel-prev').bind('click', function() {
        carousel.prev();
        return false;
    });
};

function mycarousel_buttonNextCallback(carousel, button, enabled) {
	if ( enabled ) 
		$('#mycarousel-next').css('color','#ccc').css('cursor','pointer');
	else
		$('#mycarousel-next').css('color','#555').css('cursor','default');
};

function mycarousel_buttonPrevCallback(carousel, button, enabled) {
	if ( enabled ) 
		$('#mycarousel-prev').css('color','#ccc').css('cursor','pointer');
	else
		$('#mycarousel-prev').css('color','#555').css('cursor','default');
};
