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);

    /* 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();
});

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

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

    /* 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 setupAudioPlayer() {
        $('a.panel-player').each(function(i) {
            var $this = $(this);
            var width = $(this).width();
            width = (width < 400) ? '400' : '' + width;
            $this.replaceWith("<div class='panel-player active' id='panel-player-" + i + "'></div>");
            var so = new SWFObject('/media/js/player.swf', 'swf-player-'+i, width, '24', '9');
            so.addParam('allowfullscreen','true');
            so.addParam('allowscriptaccess','always');
            so.addParam('wmode','opaque');
            so.addParam('class','swf-player');
            if (location.href.indexOf('popup-player') !== -1) {
                so.addVariable('autostart', 'true');
            }
            so.addVariable('id', 'swf-player-'+i);
            so.addVariable('duration',$this.attr('title'));
            so.addVariable('file',$this.attr('href'));
            so.addVariable('backcolor','790098');
            so.addVariable('frontcolor','FFFFFF');
            so.addVariable('lightcolor','c0d62e');
            so.addVariable('plugins','gapro-1');
						so.addVariable('gapro.accountid','UA-948741-10');
						so.addVariable('gapro.trackstarts','true');
						so.addVariable('gapro.trackpercentage','true');
						so.addVariable('gapro.tracktime','true');
            so.write('panel-player-' + i);
        });
    };

    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');
    	}
    }
