// STATION HEADER
$wn(document).ready(function(){
	if ($wn("#WNBrandingImage").attr("src") == "http://KFMB.images.worldnow.com/images/static/hdr/hdr_branding.jpg" ) {
		$wn("#WNHeader").find("#WNBranding").append("<div id='social_branding'><ul><li style='border-right:1px dotted #EEE;padding-right:7px;'><a href='http://www.sandiegojack.com' target='_blank' class='tooltip2'><img src='http://ftpcontent.worldnow.com/kfmb/images/new-homepage/header-jack-logo.png'><span class='boxroll6a'>100.7 Jack FM</span></a></li><li style='padding:0 7px 0 5px;border-right:1px dotted #EEE;'><a href='http://www.760kfmb.com' target='_blank' class='tooltip2'><img src='http://ftpcontent.worldnow.com/kfmb/images/new-homepage/header-760-logo.png'><span class='boxroll7a'>760 KFMB AM</span></a></li><li style='padding:0 15px 0 5px;'><a href='http://metvnetwork.com/index.php' target='_blank' class='tooltip2'><img src='http://ftpcontent.worldnow.com/kfmb/images/new-homepage/header-METV-logo.png'><span class='boxroll8a'>Me TV</span></a></li><li><a href='http://www.cbs8.com/global/Category.asp?c=162651' target='_blank' class='tooltip2'><img src='http://ftpcontent.worldnow.com/kfmb/images/new-homepage/header-but-newsletter.png'><span class='boxroll1a'>Sign up for Newsletters</span></a></li><li><a href='http://www.cbs8.com/Global/link.asp?L=360342' target='_blank' class='tooltip2'><img src='http://ftpcontent.worldnow.com/kfmb/images/new-homepage/header-but-mobile.png'><span class='boxroll2a'>CBS 8 Mobile</span></a></li><li><a href='http://twitter.com/cbs8/' target='_blank' class='tooltip2'><img src='http://ftpcontent.worldnow.com/kfmb/images/new-homepage/header-but-twitter.png'><span class='boxroll3a'>Follow us on Twitter</span></a></li><li><a href='http://www.facebook.com/News8' target='_blank' class='tooltip2'><img src='http://ftpcontent.worldnow.com/kfmb/images/new-homepage/header-but-facebook.png'><span class='boxroll4a'>Like us on Facebook</span></a></li><li><a href='http://www.cbs8.com/story/15198035/your-stories-submission' target='_blank' class='tooltip2'><img src='http://ftpcontent.worldnow.com/kfmb/images/new-homepage/header-but-stories.png'><span class='boxroll5a'>Submit your stories</span></a></li></ul></div>");
	}
});

// MOBDUB COMMENT COUNTS
(function() { //Add Comment Counts
	var commentCats = ['149651', '154671', '149654', '186595', '160058', '172581', '153990', '174894', '216525', '220297']; 
	
	var validNewCount = 0;
		if (location.pathname == '/') {
		validNewCount = 1;
	} else for (var i = 0; i < commentCats.length; i++) {
		var sq = "/category/"+commentCats[i]+"/";
		var match = location.href.match(sq);
		if (match) { validNewCount = validNewCount + 1; }
	}
	var validNew = ( validNewCount > 0 )  ? true : false;
	if( !validNew )  { return; } 

	var rand = Math.floor(Math.random() * 10000);
	var wnProxyUrl = '/global/interface/httprequest/hrproxy.asp?rand='+rand+'&contentType=application/json&url='; 
	var mdCommentUrl = 'http://api.mobdub.com/partners/18/smilTexts/comments.json?per_page=1&search[ref_uri]=s-';
	
	var linksArr = new Array(2);
	linksArr[0] = $("#WNCol2 .wnDVSummary p a");
	linksArr[1] = $("#WNCol2 .summary:not(.abridged) a");

	$.getScript("http://ftpcontent.worldnow.com/kfmb/custom/js/jquery.qtip-1.0.0-rc3.min.js",  function(){ 

		$(linksArr).each(function(i, links) { $(links).each(function(i, link) {	
			try{
				if ( $(link).html() == '<span class="text">&nbsp;</span>' ) {
					$(link).html('<span class="text">More&gt;&gt;</span>');
				}
			
				var storyId = $(link).attr('href').split('/story/')[1].split('/')[0]; 
				if (storyId) {
					while ($('#c_'+storyId).length) { storyId = '_' + storyId; }
					var countHTML = '<span class="comment-count" id="c_'+storyId+'"></span>';
		
					if( $(link).find('img.wnVideoIncluded').length > 0 ) { 	
						$( countHTML ).insertBefore( $(link).find('img.wnVideoIncluded') );
					} else {
						$(link).append( countHTML );
					}
		
					var getCommentUrl = wnProxyUrl + encodeURIComponent(mdCommentUrl + storyId.replace("_",""));
					
					$.getJSON(getCommentUrl, storyId, function(data) {
						if (data && (data.count > 0)) {
							var recentComment = "<i>"+data.items[0].user_name+' says "'+data.items[0].body+'"</i>';
							$("#c_"+storyId).html(data.count).qtip({
								 content: recentComment,
								 position: {
									corner: {
										tooltip: 'leftMiddle',
										target: 'rightMiddle'
									 }
								 },
								 style: {
									border: {
										width: 5,
										radius: 10
									},
									padding: 10, 
									textAlign: 'left',
									tip: true, // Give it a speech bubble tip with automatic corner detection
									name: 'light' // Style it according to the preset 'cream' style
								  }
							});
							
							
						} else {
							$("#c_"+storyId).html("0");
						}
					});
				}
			} catch(e) { }
		}); }); // END MULTI-ARRAY LOOP 
	});
})();

(function() { //Add MP3 Players
	var page_links = $("#WNCols234-5 a[href]");
	$(page_links).each(function(i, a) {				
		if ($(a).attr("href").match(/\.mp3$/i)) {
			var span = document.createElement("span");
			span.id = "mp3" + i;
			a.parentNode.insertBefore(span, a.nextSibling);
			swfobject.embedSWF("http://ftpcontent.worldnow.com/kfmbfm/mp3player/player.swf", span.id, '290', '24', "6", '', {soundFile:$(a).attr("href")})
		} 
	});
	$(page_links).each(function(i, obj) {
		if ($(obj).attr("href").match(/\.mp3$/i)) {
			$(obj).remove();
		} else if ( ($(obj).attr("href").indexOf('youtube.com') > -1) || ($(obj).attr("href").indexOf('vimeo.com') > -1) ) {
			$(obj).after(videoplayer($(obj).attr("href"))); 
			if (this.target != "_blank") { $(obj).remove(); }
		}
	});
})();

function videoplayer(url,width) {
	if (!width) { var width = 625; }
	var height = Math.round(width/1.78);
	
	// Youtube
	if (url.indexOf('youtube.com') > -1) {
		var vidid = url.split('/')[3].split('v=')[1].split('&')[0];
		var videoplayer = '<iframe src="http://www.youtube.com/embed/'+vidid+'" width="'+width+'" height="'+height+'" frameborder="0"></iframe>';
	}
	// Vimeo
	else if (url.indexOf('vimeo.com') > -1) {
		if (url.match(/vimeo.com\/[0-9]+/)) { var vidid = url.split('/')[3]; }
		else if (url.match(/vimeo.com\/channels\/[\d\w]+#[0-9]+/)) { var vidid = url.split('#')[3]; }
		else if (url.match(/vimeo.com\/groups\/[\d\w]+\/videos\/[0-9]+/)) { var vidid = url.split('/')[6]; }
		var videoplayer = '<iframe src="http://player.vimeo.com/video/'+vidid+'" width="'+width+'" height="'+height+'" frameborder="0"></iframe>';	
	}
	// Error
	else {
		var videoplayer = false;
	}			
	return videoplayer;
}

// SEND US YOUR TIPS
$(document).ready(function() {
	setTimeout(function(){
		if ( $('#mdc').length ){
		   $('#wnSocialToolsSection').before('<div id="sendustips"><b>Do you have information about this story to share with News 8? <a href="http://www.cbs8.com/story/15198035/your-stories-submission">Click Here</a></b></div>');
		} 
	},500);	
});

/*
// BYLINES
function processbylines() {
	$.getScript("http://ftpcontent.worldnow.com/kfmb/custom/js/jquery.jgfeed-min.js",  function(){
		var bylines = new Array('Craig McKee','Larry Himmel','Matt Baylow','Steve Price','Kimberly King');
		for (x in bylines) {
			var foundin = "";
			foundin = $('#WNStoryBody strong:contains('+bylines[x]+'):first');
			if (foundin.html() != null) {
				var shortname = bylines[x].replace(" ","");
				
				foundin.html( foundin.html().replace(bylines[x],'<a href="#" id="a_'+shortname+'">'+bylines[x]+'</a>') );
				bylinebox( bylines[x] );			 
			}
		}
	});
}

function bylinebox( bylinename ) {
		var rss = 'http://ftpcontent.worldnow.com/kfmb/byline/'+bylinename+'.rss';
		var shortname = bylinename.replace(" ","");
	
		$.jGFeed(rss, function(feeds){
			if(!feeds) { return false; }
			
			var linkcontent = '<b>'+bylinename+'<hr noshade/><ul class="bylinebox">';
			for(var i=0; i<feeds.entries.length; i++){
				var entry = feeds.entries[i];
				linkcontent = linkcontent + '<li><a href="'+entry.link+'">'+entry.title+'</a></li>';
			}
			linkcontent = linkcontent + '</ul>';
			
			//$('#by_'+shortname).html(linkcontent);
			$('#a_'+shortname).click(function(){ return false; });
			// Notice the use of the each method to gain access to each element individually
			$('#a_'+shortname).qtip({
				 content: linkcontent,
				 position: {
					corner: {
						tooltip: 'leftMiddle',
						target: 'rightMiddle'
					 }
				 },
				 show: {
					when: 'click', solo: true
				 },
				 hide: 'unfocus',
				 style: {
					border: {
						width: 5,
						radius: 10
					},
					padding: 10, 
					textAlign: 'center',
					tip: true, // Give it a speech bubble tip with automatic corner detection
					name: 'light' // Style it according to the preset 'cream' style
				  }
			});
		}, 10);
	}

// STORY IMAGE ENLARGEMENT
$(document).ready(function() {

	// IF STORY PAGE
	if (wng_pageInfo.containerType == 'S') {

		// LOAD FANCYBOX CSS
		$('<link rel="stylesheet" type="text/css" href="http://ftpcontent.worldnow.com/kfmb/custom/js/fancybox.1.3.1/jquery.fancybox-1.3.1.css" media="screen" />').appendTo('body');	

		// LOAD FANCYBOX JS
		$.getScript("http://ftpcontent.worldnow.com/kfmb/custom/js/fancybox.1.3.1/jquery.fancybox-1.3.1.pack.js",  function(){
			// REWRITE PHOTO DISPLAY
			$('.wnStoryBodyGraphic img').wrap('<a></a>');

			$('<div style="height:25px;"><img style="width: 16px;float:left;padding:8px 0 0 15px;" src="http://ftpcontent.worldnow.com/kfmb/images/enlarge-icon.png"><span style="padding:0 0 0 5px;float:left;"> Click image to enlarge</span></div>').appendTo(".wnStoryBodyGraphic:last");
			
			$('.wnStoryBodyGraphic a').each(function(){
				$(this).attr('href', $(this).find('img').attr('src'));
				$(this).attr('rel','storyimage');
				$(this).attr('title', $(this).find('img').attr('title'));
			});	
			
			$('.wnStoryBodyGraphic a').fancybox({
				'zoomOpacity':true,
				'zoomSpeedIn':300,
				'zoomSpeedOut':300,
				'titlePosition':'inside'
			});
		});
		
		// if (jQuery.fn.qtip) { processbylines(); }
		// else $.getScript("http://ftpcontent.worldnow.com/kfmb/custom/js/jquery.qtip-1.0.0-rc3.min.js",  function(){ processbylines(); });
	}
});

*/

function $_GET(q,s) { 
	s = s ? s : window.location.search; 
	var re = new RegExp('&'+q+'(?:=([^&]*))?(?=&|$)','i'); 
	return (s=s.replace('^?','&').match(re)) ? (typeof s[1] == 'undefined' ? '' : decodeURIComponent(s[1])) : undefined; 
} 
