var RFM = RFM || {};
jQuery(function () {
    RFM.kc.initialize();
    Cufon.replace('#content h2');

    /* STATION page functions */
    $("#country").change(changeCountry);
    $("#states").change(changeState);
    $("#provinces").change(changeProvince);

    /* CONTACT page Under Age 14 function */
    $("input[name='AgeFourteen']").change(disableFields);

	$('#main a').tooltip({
		track: true,
		delay: 0,
		showURL: false,
		showBody: " - ",
		fade: 250
	});

    /* CHARACTER page click functions */
    var switches = $('#switches > li');
    var slides = $('#slides > div');
    var player = $('.panel-player');
    switches.each(function(idx) {
            $(this).data('slide', slides.eq(idx));
            $(this).data('player', player.eq(idx));
        }).click(
        function() {
            slides.hide();
            $(this).data('slide').show();
            $(this).data('player').show();
        });

    /* STORE image click functions */
    var thumbnails = $('#thumbnails > li');
    thumbnails.each(function(idx) {
            $(this).data('slide', slides.eq(idx));
        }).click(
        function() {
            thumbnails.show();
          	slides.hide();
            $(this).hide();
            $(this).data('slide').show();
        });

    /* STORE lightbox */
    $("a[rel='product']").colorbox();

    /* VIDEOS lightboxes */
    $("a.videos").colorbox({iframe:true, innerWidth:440, height:"90%"});
    $("a.instructions").colorbox({width:"50%", inline:true, href:"#instructions"});

    /* OFFER lightboxes */
    $("a[rel='offer']").colorbox();
    $("a[rel='story']").colorbox({inline:true, href:"#story"});
    $("a[rel='notes']").colorbox({inline:true, href:"#notes"});
});

RFM.kc = (function ($) {
    var that = {};

    /* public functions */
    that.initialize = function () {
        setupPrintLink();
        setupNewAudioPlayer();
        setupPopupPlayer();
        setupExternalPlayer();
        setupArticleTooltip();
        setupVideoPlayer();
    };

    /* private functions */

    function setupPopupPlayer() {
        $('a.popup-player').click(function() {
            var newwindow = window.open(this.href,'',
                'resizable=1,toolbar=0,location=0,status=0,menubar=0,scrollbars=1,width=515,height=180');
            newwindow.focus();
            return false;
        });
    };

    function setupExternalPlayer() {
        $('a.hiskids-player').click(function() {
            var newwindow = window.open('http://www.hiskids.net/player/?show=kc','KidsCorner', 'width=560,height=410,status=0,toolbar=0,location=0,menubar=0,directories=0,resizable=0,scrollbars=0');
            newwindow.focus();
            return false;
        });
    };

    function setupPrintLink() {
        $('a.print-page').click(function() {
            $('link[media=screen]').attr('disabled', 'disabled');
            $('link[media=print]').attr('media', 'screen');
            window.print();
            return false;
        });
    };

    function setupNewAudioPlayer() {
        $('a.panel-player').each(function(i) {
            var $this = $(this);
            var width = $(this).width();
            width = (width < 330) ? '330' : '' + width;
            var autostart = 'false';
            
            if (location.href.indexOf('popup-player') !== -1) {
                autostart='true';
            }
            if (location.href.indexOf('listen') !== -1) {
                autostart='true';
            }
            $this.replaceWith("<div class='panel-player active' id='panel-player-" + i + "'></div>");
            jwplayer('panel-player-' + i).setup({
				'id': 'playerID',
				'width': width,
				'height': '24',
				'controlbar': 'top',
		    	'frontcolor': 'ffffff',
				'backcolor': '790098',
		    	'lightcolor': 'c0d62e',
		    	'autostart': autostart,
				'file': $this.attr('href'),
				'plugins': {
			       'gapro-2': {}
		   		},
		    	'modes': [
		        	{type: 'html5'},
		        	{type: 'flash', src: '/media/js/player.swf'},
		        	{type: 'download'}
		    	]
			});            
        });
    };
 
    function setupVideoPlayer() {
        $('a.video-player').each(function(i) {
            var $this = $(this);
            var autostart = 'true';
            $this.replaceWith("<div class='video-player' id='video-player-" + i + "'></div>");
            jwplayer('video-player-' + i).setup({
				'id': 'playerID',
				'width': '470',
				'height': '320',
				'controlbar': 'bottom',
		    	'frontcolor': 'ffffff',
				'backcolor': '790098',
		    	'lightcolor': 'c0d62e',
				'file': $this.attr('href'),
				'plugins': {
			       'gapro-2': {}
		   		},
		    	'modes': [
		        	{type: 'html5'},
		        	{type: 'flash', src: '/media/js/player.swf'},
		        	{type: 'download'}
		    	]
			});            
        });
    };
       
    function setupArticleTooltip() {
      $('#article_tools_nav a[title]').tooltip({
            tip: '#tip',
            position:'bottom center',
            offset:[0,-75],
            effect: 'slide',
            direction:'down'
      });
     };



    return that;
}(jQuery));

    /* custom JS functions */

		function changeCountry() {
			$('#united-states').css('display', 'none');
			$('#canada').css('display', 'none');
			$('#international').css('display', 'none');
			$('#online').css('display', 'none');
			$('#network').css('display', 'none');
			$('#territories').css('display', 'none');
			if ($('#station_state')) {
				$('#station_state').css('display', 'none');
			}
			if ($('#station_province')) {
				$('#station_province').css('display', 'none');
			}
			var selected = $('#country option:selected').val();
	    if(selected != 0){
	    		var selectdiv = '#' + selected;
	        $(selectdiv).css('display','inline');
	    }
	    return false;
		}

		function changeState() {
			var selected = $('#states option:selected').val();
	    if(selected != 0){
					var page = 'http://kidscorner.net/parents/stations/united-states/'+selected;
	        location.href = page;
	    }
		}

		function changeProvince() {
			var selected = $('#provinces option:selected').val();
	    if(selected != 0){
					var page = 'http://kidscorner.net/parents/stations/canada/'+selected;
	        location.href = page;
	    }
		}

		function disableFields() {
    	if ($("input[name='AgeFourteen']:checked").val() == 'Yes') {
    			$('#AgeFourteenRow1').css('display', 'none');
					$('#AgeFourteenRow2').css('display', 'none');
					$('#AgeFourteenRow3').css('display', 'none');
					$('#MailingAddress0').css('display', 'none');
					$('#MailingAddress1').css('display', 'none');
					$('#MailingAddress2').css('display', 'none');
					$('#MailingAddress3').css('display', 'none');
					$('#MailingAddress4').css('display', 'none');
					$('#AgeFourteenN').attr('disabled', 'disabled');
					$('input#form_first_name').val('Under 14');
					$('input#form_last_name').val('Under 14');
					$('input#email').val('nobody@nobody.com');
    	}
    }

