
var current_video_playlist_item = null;

function PlayVideo(autoplay) {
    var title = current_video_playlist_item.find('div.title').html();
    var link = current_video_playlist_item.find('div.link').html();
    var video_id = current_video_playlist_item.find('div.video-id').html();
    jwplayer('video-player').load({ file: 'http://www.youtube.com/watch?v=' + video_id, image: 'http://img.youtube.com/vi/' + video_id + '/0.jpg' });
    if (autoplay) {
        jwplayer('video-player').play();
    }
    $('div.home-video-title').html('<a href="' + link + '">' + title + '</a>');
    $('div.news-video-title').html('<a href="' + link + '">' + title + '</a>');
}

function PlayNextVideo() {
    current_video_playlist_item = current_video_playlist_item.next();
    if (current_video_playlist_item.length == 0) {
        current_video_playlist_item = $('div.video-playlist div.item').first();
    }
    PlayVideo(true);
}

function PlayPreviousVideo() {
    current_video_playlist_item = current_video_playlist_item.prev();
    if (current_video_playlist_item.length == 0) {
        current_video_playlist_item = $('div.video-playlist div.item').last();
    }
    PlayVideo(true);
}

var hard_pausing = true;

function PlayAudio(item) {
    jwplayer('audio-player').load({ file: decode_base64(item.find('div.file').html()) });
    $('div.header-player div.audio-title').html('<a href="' + item.find('div.link').html() + '">' + item.find('div.title').html() + '</a>');
    $('div.listen-button').html("Listen").removeClass('active');
    $('div#audiotrack-' + item.find('div.id').html()).html("<i>Playing</i>").addClass('active');
}

function PauseAudio() {
    hard_pausing = false;
    jwplayer('audio-player').pause(true);
}

function VideoPlayerOnPlay(event) {
    PauseAudio();
}

function AudioPlayerOnPause(event) {
    if (hard_pausing) {
        $.cookie('audio-player-state', 'paused', { path: '/' });
    }
    hard_pausing = true;
}
function AudioPlayerOnPlay(event) {
    $.cookie('audio-player-state', 'playing', { path: '/' });
    
}
function AudioPlayerOnComplete() {
    AudioPlayerNext();
}

function AudioPlayerNext() {
    current = $('div.audio-playlist div.active').first();

    current = current.next();

    if (current.length == 0) {
        current = $('div.audio-playlist div.item').first();
    }

    $('div.audio-playlist div.item').removeClass('active');
    current.addClass('active');

    PlayAudio(current);
}

function AudioPlayerPrevious() {
    current = $('div.audio-playlist div.active').first();

    current = current.prev();

    if (current.length == 0) {
        current = $('div.audio-playlist div.item').last();
    }

    $('div.audio-playlist div.item').removeClass('active');
    current.addClass('active');

    PlayAudio(current);
}

function block_ready(block) {
    block.find('div.tour-block-shadow').addClass('tour-block-shadow-active');
    block.find('div.news-block-shadow').addClass('news-block-shadow-active');
    block.find('h3.tour-block-header').addClass('tour-block-header-active');
    block.find('h3.news-block-header').addClass('news-block-header-active');
    block.find('h3.music-block-header').addClass('music-block-header-active');
    block.find('h3.photos-block-header').addClass('photos-block-header-active');
    block.find('div.home-photo-wrapper').show();
    
    if (navigator.userAgent.toLowerCase().indexOf('chrome') > -1) {
    //    block.find('div.header').addClass('header-chrome-fix');
    }

    block.find('a.home-video-previous').click(function() {
        PlayPreviousVideo();
        return false;
    });
    block.find('a.news-video-previous').click(function() {
        PlayPreviousVideo();
        return false;
    });
    block.find('a.home-video-next').click(function() {
        PlayNextVideo();
        return false;
    });
    block.find('a.news-video-next').click(function() {
        PlayNextVideo();
        return false;
    });

    block.find('div.video-playlist').each(function() {
        current_video_playlist_item = $('div.video-playlist div.item').first();
        $('div.home-video-player').each(function() {
            jwplayer('video-player').setup({
                'flashplayer': '/Includes/player.swf',
                'id': 'player',
                'width': '330',
                'height': '216',
                'controlbar': 'over',
                'autostart': 'false',
                'controlbar.idlehide': 'true',
                'events': {
                    'onPlay': function(event) {
                        VideoPlayerOnPlay();
                    }
                }
            });
        });
        $('div.news-video-player').each(function() {
            jwplayer('video-player').setup({
                'flashplayer': '/Includes/player.swf',
                'id': 'player',
                'width': '260',
                'height': '180',
                'controlbar': 'over',
                'autostart': 'false',
                'controlbar.idlehide': 'true',
                'events': {
                    'onPlay': function(event) {
                        VideoPlayerOnPlay();
                    }
                }
            });
        });
        PlayVideo(false);
    });

    block.find('div.media-container div.player-container div.video-id').each(function() {
        jwplayer('video-player').setup({
            'flashplayer': '/Includes/player.swf',
            'id': 'player',
            'width': '670',
            'height': '407',
            'controlbar': 'over',
            'autostart': 'true',
            'controlbar.idlehide': 'true',
            'file': 'http://www.youtube.com/watch?v=' + $(this).html(),
            'events': {
                'onPlay': function(event) {
                    VideoPlayerOnPlay();
                }
            }
        });
    });

    block.find('div.listen-button').html("Listen").hover(
        function() { $(this).addClass('listen-button-hover'); },
        function() { $(this).removeClass('listen-button-hover'); }
    ).click(function() {
        if ($(this).hasClass('active')) return;
        var new_audio_id = $(this).attr('id').replace('audiotrack-', '');

        $('div.audio-playlist div.item div.id').each(function() {
            if ($(this).html() == new_audio_id) {
                $('div.audio-playlist div.item').removeClass('active');
                $(this).parent().addClass('active');
                PlayAudio($(this).parent());
            }
        });
    });

    block.find('div.audio-playlist').each(function() {
        autostart = 'true';
        if (block.find('div.media-container div.player-container div.video-id').length > 0) {
            autostart = 'false';
        }
        if ($.cookie('audio-player-state') == 'paused') {
            autostart = 'false';
        }
        items = $('div.audio-playlist div.item');
        current_audio_playlist_item = $(items[Math.floor(Math.random() * items.length)]);
        current_audio_playlist_item.addClass('active');
        jwplayer('audio-player').setup({
            'flashplayer': '/Includes/player.swf',
            'id': 'audio-player',
            'width': '220',
            'height': '24',
            'controlbar': 'bottom',
            'backcolor': '000000',
            'frontcolor': '878068',
            'lightcolor': '7a745e',
            'screencolor': '000000',
            'autostart': autostart,
            'events': {
                'onPause': function(event) {
                    AudioPlayerOnPause(event);
                },
                'onPlay': function(event) {
                    AudioPlayerOnPlay(event);
                },
                'onComplete': function() {
                    AudioPlayerOnComplete();
                }
            }
        });

        PlayAudio(current_audio_playlist_item);
    });
    block.find('div.header-player a.audio-next').click(function() {
        AudioPlayerNext();
        return false;
    });
    block.find('div.header-player a.audio-previous').click(function() {
        AudioPlayerPrevious();
        return false;
    });

    block.find('div.home-photos-content div.gallery a').lightBox();
    block.find('div.player-container div.gallery a').lightBox();

    block.find('a.popup-link').click(function() {
        window.open($(this).attr('href'), "Popup", "location=no,menubar=no,scrollbars=yes,status=no,titlebar=no,width=600,height=550,toolbar=no");
        return false;
    });

    block.find('div.playlist-container div.media-item').hover(
        function() {
            if (!$(this).hasClass('media-item-active')) {
                $(this).addClass('media-item-hover');
            }
        },
        function() { $(this).removeClass('media-item-hover'); }
    );
    block.find('div.playlist-container div.media-item').click(function() {
        if (!$(this).hasClass('media-item-active')) {
            window.location = $(this).find('div.media-title a').attr('href');
        }
        return false;
    });
}

$(document).ready(function() {
    block_ready($(this));
    if (typeof (Sys) !== 'undefined') {
        var manager = Sys.WebForms.PageRequestManager.getInstance();
        manager.add_endRequest(EndRequestHandler);
        manager.add_pageLoaded(global_pageloaded_initializer);
    }
});

function EndRequestHandler(sender, args) {
    if (args.get_error() != undefined) {
        var errorMessage = args.get_error().message;
        alert(errorMessage);
    }
}

function global_pageloaded_initializer(sender, args) {
    var panels = args.get_panelsUpdated();
    if (panels.length > 0) {
        var reload_addthis = false;
        for (i = 0; i < panels.length; i++) {
            block_ready($(panels[i]));
            if ($(panels[i]).find('.addthis_toolbox').length > 0) {
                reload_addthis = true;
            }
        }
        if (reload_addthis) {
            window.addthis.ost = 0;
            window.addthis.ready();
        }
    }
}

function decode_base64(input) {
    var output = "";
    var chr1, chr2, chr3;
    var enc1, enc2, enc3, enc4;
    var i = 0;
    var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",

    input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

    while (i < input.length) {

	    enc1 = keyStr.indexOf(input.charAt(i++));
	    enc2 = keyStr.indexOf(input.charAt(i++));
	    enc3 = keyStr.indexOf(input.charAt(i++));
	    enc4 = keyStr.indexOf(input.charAt(i++));

	    chr1 = (enc1 << 2) | (enc2 >> 4);
	    chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
	    chr3 = ((enc3 & 3) << 6) | enc4;

	    output = output + String.fromCharCode(chr1);

	    if (enc3 != 64) {
		    output = output + String.fromCharCode(chr2);
	    }
	    if (enc4 != 64) {
		    output = output + String.fromCharCode(chr3);
	    }

    }

    output = decode_utf8(output);

    return output;
}

function decode_utf8(utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;
 
		while ( i < utftext.length ) {
 
			c = utftext.charCodeAt(i);
 
			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}
 
		}
 
		return string;
	}

