// Global Vars
var questionPointer = '<span class="arrow"></span>';
var questionImg ='../../../../Templates/RSA/Images/question_mark.png';
var questionImgRollover = '../../../../Templates/RSA/Images/question_mark_rollover.png';
var jPlayer = '<div id="jquery_jplayer"></div><div class="jp-playlist-player"><div class="jp-interface"><ul class="jp-controls"><li id="jplayer_play" class="jp-play">play</li><li id="jplayer_pause" class="jp-pause">pause</li><li id="jplayer_stop" class="jp-stop">stop</li><li id="jplayer_volume_min" class="jp-volume-min">min volume</li><li id="jplayer_volume_max" class="jp-volume-max">max volume</li><li id="jplayer_previous" class="jp-previous">previous</li><li id="jplayer_next" class="jp-next">next</li></ul><div class="jp-progress"><div id="jplayer_load_bar" class="jp-load-bar"><div id="jplayer_play_bar" class="jp-play-bar"></div></div></div><div id="jplayer_volume_bar" class="jp-volume-bar"><div id="jplayer_volume_bar_value" class="jp-volume-bar-value"></div></div><div id="jplayer_play_time" class="jp-play-time"></div><div id="jplayer_total_time" class="jp-total-time"></div></div><div id="jplayer_playlist" class="jp-playlist"><ul></ul></div></div>';

// Init Function
$(function() {
	if($('#notifications').length > 0) 
	{
		$('#notifications #notifications-wrapper').cycle({
			height: 32,
			pause:  1
		});
	}
	
	if($('#tab-nav').length > 0)
	{
		rolloverTabNav();
	}
	
	if($('.first-question').length > 0)
	{
		observeQuestions();
	}
	
	if($('#accordion').length > 0)
	{
		$("#accordion").accordion
			({ 
			 	autoHeight: false,
				change: function(event, ui) { 
					pgView('/virtual/contactus/tab-'+ui.newHeader.text().toLowerCase()); 
				}
			});
	}
	
	if($('#social-links').length > 0)
	{
		addSocialLinks();
	}
	
	if($('#di_keyword').length > 0)
	{
		observe_dikeyword();
	}
	
	if($('.map-menu h2').length > 0)
	{
		setupMapUse();
	}
	
	if($('.modulequestion h1').length > 0)
	{
		setupMapUse2();
	}
	
//	if($('.video-warning').length > 0)
//	{
//		showVideoWarning();
	//	}

	if ($('.radio-ads').length > 0) {
	    setUpAudioPlayer();
	}
		
});

function rolloverTabNav()
{
	$('#tab-nav li').hover(
		function()
		{
			$(this).addClass('js-selected');
			$(this).css('background-color', 'transparent');
		},
		function()
		{
			$(this).removeClass('js-selected');
		}
	);
}

function observeQuestions() 
{
	$('.selection-wrapper').hide();
	$('.first-question').addClass('selected');
	
	$('.first-question input').focus
	(
	 	function()
		{
			$(this).closest('.question').removeClass('selected');
			$(this).closest('.question').append(questionPointer);
			var id = $(this).attr('id');
			
			if(!$('.'+id).is(':visible'))
			{
				if($('.selection-wrapper:visible').length > 0)
				{
					$('.selection-wrapper:visible').slideUp('slow',
						function() {
							// A little bit of housekeeping
							$('.selection-wrapper .question').removeClass('selected');
	
							// Show the new question
							showQuestion(id);
						}
					);
				}
				else
				{
					// Show the new question
					showQuestion(id);
				}
			}
		}
	 );
	
	$('.selection-wrapper input').focus(function(){checkFocus($(this));});
	$('.selection-wrapper select').focus(function(){checkFocus($(this));});
	$('.selection-wrapper textarea').focus(function(){checkFocus($(this));});
}

function showQuestion(id)
{
	$('.'+id).find('.question:first').addClass('selected');
	$('.'+id).slideDown('slow');
}

function checkFocus(input)
{
	if(!$(input).closest('.question').hasClass('selected'))
	{
		$('.selection-wrapper .question').removeClass('selected');
		$(input).closest('.question').addClass('selected');
	}
}

function addSocialLinks()
{
	// Get URL & Title
	var current_url = window.location;
	var page_title = document.title;
	
	// Set up Social Links
	var url_facebook  = 'http://www.facebook.com/sharer.php?u='+current_url;
	var url_twitter   = 'http://twitter.com/home?status='+page_title+' '+current_url;
	var url_bebo      = 'http://www.bebo.com/c/share?Url='+current_url+'&amp;Title='+page_title;
	var url_delicious = 'http://delicious.com/save?url='+current_url+'&amp;title='+page_title;
	
	// Add the links to the panel
	$('#url_facebook').attr('href',url_facebook);
	$('#url_twitter').attr('href',url_twitter);
	$('#url_bebo').attr('href',url_bebo);
	$('#url_delicious').attr('href',url_delicious);
	
	// Display the panel
	$('#social-links')
		.addClass('clearfix')
		.css('display','block');
}

function observe_dikeyword()
{
	var default_val = 'Enter an optional keyword';
	$('#di_keyword').addClass('default').val(default_val);
	$('#di_keyword').focus(
		function() {
			$(this).removeClass('default');
			var text = $(this).val();
			if(text == default_val)
				$(this).val('');
		}
	);
	$('#di_keyword').blur(
		function() {
			var text = $(this).val();
			if(text == '') 
			{
				$(this).addClass('default');
				$(this).val(default_val);
			}
		}
	);
}

function setupMapUse()
{
	$('.map-menu h2').append('<span class="question"><img src="'+questionImg+'" alt="" /></span>');
	var popupContent = $('#how-to-use-map').html();
	$('#how-to-use-map').remove();
	
	$('.map-menu h2 span.question').hover(
		function() {
			$(this).find('img').attr('src',questionImgRollover);
			$(this).append('<div class="info-window-wrapper"><div class="info-window">'+popupContent+'</div></div>');
			$('.map-menu select').hide();
		},
		function() {
			$(this).find('img').attr('src',questionImg);
			$(this).find('.info-window-wrapper').remove();
			$('.map-menu select').show();
		}
	);
}

function setupMapUse2()
{
	$('.modulequestion h1').append('<span class="question"><img src="'+questionImg+'" alt="" /></span>');
	var popupContent = $('#how-to-use-map').html();
	$('#how-to-use-map').remove();
	
	$('.modulequestion h1 span.question').hover(
		function() {
			$(this).find('img').attr('src',questionImgRollover);
			$(this).append('<div class="info-window-wrapper"><div class="info-window">'+popupContent+'</div></div>');
			$('.modulequestion select').hide();
		},
		function() {
			$(this).find('img').attr('src',questionImg);
			$(this).find('.info-window-wrapper').remove();
			$('.modulequestion select').show();
		}
	);
}

function observeSearch()
{
	$('.gsc-input').hide();	
}

function showVideoWarning() 
{
	var height = $('.video-warning').height();
	var width  = $('.video-warning').width();
	
	$('.video-warning').append('<div class="overlay" style="width: '+width+'px; height: '+height+'px;"><div class="warning"><p>You must be over 18 to view this video</p><p><a href="#">Ok</a></p></div></div>');
	$('.overlay a').click(
		function() {
	        $('.overlay').remove();
	        return false; 
		}
	);
}

function setUpAudioPlayer() {
    var myPlayList = new Array();
    var playItem = 0;

    // Add all the radio ads to a playlist
    $('.radio-ads li').each(function() {
        title = $(this).find('a').text();
        mp3 = $(this).find('a').attr('href');
        myPlayList[myPlayList.length] = { name: title, mp3: mp3 };
    });
    $('.radio-ads .module').html(jPlayer);

    // Local copy of jQuery selectors, for performance.
    var jpPlayTime = $("#jplayer_play_time");
    var jpTotalTime = $("#jplayer_total_time");
    var jpStatus = $("#demo_status"); // For displaying information about jPlayer's status in the demo page

    $("#jquery_jplayer").jPlayer({
        ready: function() {
            displayPlayList();
            playListInit(false); // Parameter is a boolean for autoplay.
            //demoInstanceInfo(this.element, $("#demo_info")); // This displays information about jPlayer's configuration in the demo page
        },
        oggSupport: false,
        swfPath: '/Templates/RSA/js/'
    })
	.jPlayer("onProgressChange", function(loadPercent, playedPercentRelative, playedPercentAbsolute, playedTime, totalTime) {
	    jpPlayTime.text($.jPlayer.convertTime(playedTime));
	    jpTotalTime.text($.jPlayer.convertTime(totalTime));

	    demoStatusInfo(this.element, jpStatus); // This displays information about jPlayer's status in the demo page

	})
	.jPlayer("onSoundComplete", function() {
	    playListNext();
	});

    $("#jplayer_previous").click(function() {
        playListPrev();
        return false;
    });

    $("#jplayer_next").click(function() {
        playListNext();
        return false;
    });

    function displayPlayList() {
        for (i = 0; i < myPlayList.length; i++) {
            $("#jplayer_playlist ul").append("<li id='jplayer_playlist_item_" + i + "'>" + myPlayList[i].name + "</li>");
            $("#jplayer_playlist_item_" + i).data("index", i).click(function() {
                var index = $(this).data("index");
                if (playItem != index) {
                    playListChange(index);
                } else {
                    $("#jquery_jplayer").jPlayer("play");
                }
            });
        }
    }

    function playListInit(autoplay) {
        if (autoplay) {
            playListChange(playItem);
        } else {
            playListConfig(playItem);
        }
    }

    function playListConfig(index) {
        $("#jplayer_playlist_item_" + playItem).removeClass("jplayer_playlist_current");
        $("#jplayer_playlist_item_" + index).addClass("jplayer_playlist_current");
        playItem = index;
        $("#jquery_jplayer").jPlayer("setFile", myPlayList[playItem].mp3);
    }

    function playListChange(index) {
        playListConfig(index);
        $("#jquery_jplayer").jPlayer("play");
    }

    function playListNext() {
        var index = (playItem + 1 < myPlayList.length) ? playItem + 1 : 0;
        playListChange(index);
    }

    function playListPrev() {
        var index = (playItem - 1 >= 0) ? playItem - 1 : myPlayList.length - 1;
        playListChange(index);
    }
}

/*
===========================================
  Log virtual page view
=========================================== 
*/

function pgView(url)
{
	if (typeof pageTracker != 'undefined') {
		pageTracker._link(url);
		return true;
	}
	else if (typeof urchinTracker != 'undefined') {
		__utmLinker(url);
		return true;
	}
	return false;
}