var hideSubTimeout;

function showSub(id,ob) {
	//jQuery('.default_submenu').hide();
	jQuery('.submenu').hide();
	jQuery('#sub'+id).show();
	offset=0;
	jQuery('#sub'+id).css( { display:'block',position:'absolute', left: ob.position().left-jQuery('#menuArea').position().left+'px',zindex:10000 });

	if (hideSubTimeout) clearTimeout(hideSubTimeout);
}

function hideSub() {
	//jQuery('.default_submenu').show();
	jQuery('.submenu').hide();
}

var zml={
	init: function() {

		jQuery('.survey-form').attr({action:window.location.href});
		jQuery('.survey-form').submit( function () {

			var dirty=0;
			var checks=0;
			jQuery('.user-answer').each(function() { dirty+=jQuery(this).val()==''; });
			jQuery('input[@type=radio]:checked').each(function() { checks+=1; });
			dirty+=(checks==0);
			if (dirty) {
				alert('Please complete the survey form first.');
				return false;
			}
			dirty=0;
			jQuery('.survey-from input:text').each(function() { dirty+=!jQuery(this).val(); } );
			if (dirty==3) {
				return confirm('Are you sure you don\'t want to enter the contest?');
			}
			if (dirty!=0) {
				alert('Please fill out all fields: Name, Email and Postal Code to enter the contest.'+dirty);
				return false;
			}
			jQuery('.survey-form:first').trigger('submit');

		});

		if (window.location.href.match(/\?showWelcome/)) {
			var h=jQuery(window).height()-20;
			jQuery('body').append("<div id='welcome'><img src='/images/50PlusBeta_surveyPre.jpg' width='730' height='480' /></div>");
			jQuery('#welcome').dialog( { closeText:'Close',width: 760, height: h, resizable: false, draggable: false, modal: true,position: 'center', title: '', dialogClass:'welcomer' } );
			jQuery('#welcome').click(function() {jQuery(this).remove(); });
			jQuery('.ui-dialog-titlebar').remove();
		}

		var livecat=jQuery('#liveCat').val();
		livecat=(livecat=='games'?'entertainment':livecat);
		jQuery('#menu_'+livecat+' a').addClass('active');
		

		/*Display submenu on rollover */
		jQuery('#mainNav li').mouseover(
			function () {
				var id=jQuery(this).attr('id');
				showSub(id,jQuery(this));
			}
		);


		if (jQuery.columnize) jQuery('.columnize').columnize({columns:2});

		if (jQuery('.postContent').height()<300) jQuery('.postContent').css({height:'300px'});

		jQuery('.trivia .postContent').append("<a id='triviaClick' href='javascript:void(0)'>Click here to see the answer</a>");
		jQuery('#triviaClick').click(function(){ jQuery('.trivia blockquote').prepend('<span>The answer is:</span>').css({fontWeight:'bold',display:'block'}); jQuery('#triviaClick').remove(); });


		//jQuery('.url_select').onchange(function(){window.location.href=jQuery(this).val();});

		jQuery('.submenu,.betaWelcomeImage').css({zIndex:1000,cursor:'pointer'});
		jQuery('#navBar').css({zIndex:1});

		jQuery('#zoomerRadio').click(function() { window.open('http://zoomerradio.ca/listen-live/','player','toolbar=no,location=no,menubar=yes,resizable=yes,scrollbars=yes,width=750,height=545,left=150,top=125');return false; });

		jQuery('#surveyButton, .betaWelcomeImage').click(function() {

			var clicksource="50plusSurveyBubble";
			if (jQuery(this).hasClass('.betaWelcomeImage')) clicksource="50plusSurveyTile";
			var clicktrack="http://www.50plus.com/redirect/redirect.cfm?source="+clicksource+"&account="+clicksource+"&url="+window.location.href+'?die';
			var ctf=jQuery("<iframe src='"+clicktrack+"' style='display:none'/>");
			jQuery('body').append(ctf);
			var h=jQuery(window).height()-20;
			if (jQuery('#surveyDialog').length==0) jQuery('body').append("<div id='surveyDialog'><iframe width='700' height='"+(h-60)+"' src='/survey/?useTemplate=pages/plain.xsl' id='surveyDialog'></iframe></div>");
			jQuery('#surveyDialog').dialog( { closeText:'Close',width: 740, height: h, resizable: false, draggable: false, modal: true,position: 'center', title: '' } );
			jQuery('#surveyDialog').css({overflow:'hidden'});
		} );
		
		jQuery('.newWindow').click(function() { window.open(jQuery(this).attr('href')); return false; });
		jQuery('#menuArea').mouseleave(function() {
			hideSubTimeout=setTimeout('hideSub();',1000);
		});
		/* Highlight active menu area (much easeier then rigging the XSL!) */
		jQuery('.rmenu a').each(
			function () {
				var wl=window.location.href.split('/');
				var al=jQuery(this).attr('href').split('/');
//				if (wl[wl.length-1]==al[al.length-1]) { jQuery(this).addClass('active');return;/*landing page */}
				if (wl[wl.length-2]==al[al.length-1]) { jQuery(this).addClass('active');return;/*browseall */}
				if (wl[wl.length-3]==al[al.length-1]) { jQuery(this).addClass('active');return;/*article page */}
				if (wl[wl.length-4]==al[al.length-1]) { jQuery(this).addClass('active');return;/*article page */}
				//if (wl[wl.length-5]==al[al.length-1]) { jQuery(this).addClass('active');return;/*article page */}
			}
		);

		jQuery('#navBar a').each(
			function () {
				var wl=window.location.href.split('/');
				var al=jQuery(this).attr('href').split('/');
				if (wl[wl.length-1]==al[al.length-1]) jQuery(this).addClass('active');//landing page
			}
		);

		jQuery('.postBlockContainer').removeClass('floatRight');
		jQuery('.postBlockContainer').removeClass('floatLeft');
		jQuery('.postBlockContainer').addClass('dontsplit');

		jQuery('.html_content p:empty').remove();

		jQuery('#navBar_openmore').mouseover(function () {
			jQuery(this).html('');
			jQuery('#navBar_more').show();
			jQuery('.flash').each(zml.flashify);
		});

		jQuery('.option_value').each(
			function () {
				var id='#'+jQuery(this).attr('id').replace('set_','');
				jQuery(id).val(jQuery(this).val());
			}
		);
		//jQuery('.datepicker').datepicker();
		
		jQuery('#textSizeSelector li a').click(
			function () {
				var sz=15;
				state=jQuery(this).text();
				//jQuery(this).css({ 'background-color':'#ededa0'} );
				if (state=='S') sz=15;
				if (state=='M') sz=17;
				if (state=='L') sz=19;
				var hsz=sz;
				var psz=sz-6;
				var asz=sz-2;
				var ysz=sz-3;
				jQuery('.postBoxWrap div a').css({fontSize:hsz+'px'});
				jQuery('.postBoxWrap p').css({fontSize:hsz+'px'});
				jQuery('.postBoxWide h2').css({fontSize:hsz+'px'});
				jQuery('.postBoxWide h2 a').css({fontSize:hsz+'px'});
				jQuery('.grid_8 p').css({fontSize:hsz+'px'});
				jQuery('.postContent bold').css({fontSize:hsz+'px'});
				jQuery('.postContent h3').css({fontSize:hsz+'px'});
				jQuery('.postBoxWrap h3').css({fontSize:psz+'px'});
				jQuery('.grid_8 a.more').css({fontSize:asz+'px'});
				jQuery('.yellow_box h2').css({fontSize:hsz+'px'});
				jQuery('.yellow_box a').css({fontSize:ysz+'px'});
				jQuery('.widgettitle').css({fontSize:hsz+'px'});
				jQuery('.recentcomments').css({fontSize:ysz+'px'});
				return false;
			}
		);

		jQuery('#s').addClass("s");
		
		jQuery('#s').focus(function() {
			jQuery(this).removeClass("s").addClass("sFocus");
			   if (this.value == this.defaultValue){ 
			   	this.value = '';
			}
			if(this.value != this.defaultValue){
				this.select();
			}
		});
		

		jQuery('.imageThumbs img').click(
			function () {
				newsrc=jQuery(this).attr('src').replace('width=80','width=360');
				jQuery('.mainImage img').attr({src:newsrc});
			}
		);


		jQuery('.shareArticle').mouseover(
			function() {
				jQuery('.addthis_toolbox').show();
			}
		);

		jQuery('.printArticle').click(
			function () {
			}
		);


	},
	flashify: function () {
		src=jQuery(this).attr('rel');
		//not done
	}
	
};


jQuery(zml.init);

function insertVideoPlugin() {
	document.write('<script type="text/javascript" src="http://media.zoomermedia.tv/flashplayer/js/inject.js?playerWidth=405&playerHeight=338"></script>');
}

function quickRollover(img_name, img_src) {
	if (document[img_name]) document[img_name].src = img_src;
}


jQuery(document).ready(function() {
	var zmDate = new Date();
	var zmDateFormat = jQuery.datepicker.formatDate('D M d, yy',zmDate);
	jQuery("#zmDate").html(zmDateFormat);
});

