/* JQUERY_SCRIPTS.JS 
	Contents: (1a) Fix the global nav on the home page, (1b) Modify the forecast "widget" 
	defined in functions.php, (1c) Add highlighting to tutorials page, (1d) Deal with the
	category thumbnail swapping, (1e) Remove date from stock quote widget, (1f) Deal with
	the multimedia player swapping, (1g) Pull quotes, (1h) Mark external links,
	(1i) Mark alternating table cells (in Valley Views) for styling,
	(1j) Open .news-map-link in a new window,
	(2) Call the liScroll script, (3) JavaScript DropDown Menu Script, (4) Call the jCarousel plugin, 
	(5) Scroll to top plugin, (6) Easy tooltip plugin (for multimedia box) */


$(document).ready(function() {
	// (1a) Fix the global nav on the home page
	$("#home #globalNav li.current_page_item").removeClass("current_page_item");
	$("#home #globalNav li:first").addClass("current_page_item");


	// (1b) Modifications to the forecast "widget"
	$("#forecast .forecasted:first").addClass("today");
	$("#forecast .forecasted:last").addClass("tomorrow");
	$(".today .title").text("Today");
	$(".tomorrow .title").text("Tomorrow");
	
	
	// (1c) Add highlighting to tutorials page
	$("div.tutorials li").hover(
		function() {
			$(this).addClass("highlight");
		},
		function() {
			$(this).removeClass("highlight");
		}
	);


	// (1d) Deal with the category thumbnail swapping
	$("#home li.has-thumb a").mouseover(function(){
		var currentDiv = $(this).parents("div").attr("id"); // This repeats below - find a way to declare globally
		var data = $(this).parent().metadata();
		$("div[id='" + currentDiv + "'] div.featured img").attr("src", data.imageurl);
		$("div[id='" + currentDiv + "'] div.featured span").text(data.imagelocation);
	}).mouseout(function(){
		var currentDiv = $(this).parents("div").attr("id");
		var defaultdata = $("div[id='" + currentDiv + "'] div.featured img").metadata();
		$("div[id='" + currentDiv + "'] div.featured img").attr("src", defaultdata.defaultimageurl);
		$("div[id='" + currentDiv + "'] div.featured span").text(defaultdata.defaultimagelocation);
	});
	
	
	// (1e) Remove date from stock quote widget
	$("div#stocks center").replaceWith("&nbsp;")
	
	
	// (1f) Deal with the multimedia player swapping
	$("#multimedia a.tooltip").click(function(){
		var data = $(this).parent().metadata();
		$("div.multimedia param").attr("value", "http://www.youtube.com/v/" + data.loadThisYouTubeID + "&hl=en&fs=1&");
		$("div.multimedia embed").attr("src", "http://www.youtube.com/v/" + data.loadThisYouTubeID + "&hl=en&fs=1&");
		$("div#multimedia span.caption").html(data.loadThisLocation + " &mdash; " + data.loadThisDescription);
		return false;
	});
	
	
	// (1g) Pull quotes (Easy Pullquotes by Mike Jolley)
	// Go through each span element with a classname of "pullquote"
	$('span.pullquote').each(function() {
		// Get the text of the span
		text = $(this).text();
		// Get rid of unwanted charactors
		text=text.replace( /\((.*)\)/gi, " " );
		// Check if this is to be a right or left pull quote and output it
		if ($(this).is(".right")) 
			$(this).parent().before('<blockquote class="pullquote right"><p>&quot;'+ text +'&quot;</p></blockquote>');
		else
			$(this).parent().before('<blockquote class="pullquote"><p>&quot;'+ text +'&quot;</p></blockquote>');
	});
	// End pull quote code
	
	
	// (1h) Mark external links
	$('div#scrollingAd a').addClass("image");
	$('.addthis a').addClass("image");
	$('#content a:not(.image)').filter(function() {
		return this.hostname && this.hostname !== location.hostname;
	}).after(' <img src="/wp-content/themes/ivnews/images/external.png" alt="" title="External link - Will open in a new window" height="10" width="10">').attr("title", "External link - Will open in a new window");
	
	
	// (1i) Mark alternating table cells (in Valley Views) for styling
	$('table.valley-views td:odd').addClass('odd-cell');
	
	
	// (1j) Open .news-map-link in a new window
	$('#news-map-link').popupWindow({ 
		height:600, 
		width:750, 
		centerBrowser:1 
	}); 

});


// (2) Call the jQuery liScroll script
$(function(){ 
    $("ul#ticker").liScroll({travelocity: 0.04}); 
});


// (3) JSDDM (Dropdown) Script
var timeout    = 500;
var closetimer = 0;
var ddmenuitem = 0;

function jsddm_open()
{  jsddm_canceltimer();
   jsddm_close();
   ddmenuitem = $(this).find('ul').css('visibility', 'visible');}

function jsddm_close()
{  if(ddmenuitem) ddmenuitem.css('visibility', 'hidden');}

function jsddm_timer()
{  closetimer = window.setTimeout(jsddm_close, timeout);}

function jsddm_canceltimer()
{  if(closetimer)
   {  window.clearTimeout(closetimer);
      closetimer = null;}}

$(document).ready(function()
{  $('#jsddm > li').bind('mouseover', jsddm_open)
   $('#jsddm > li').bind('mouseout',  jsddm_timer)});

document.onclick = jsddm_close;


// (4) Call the jCarousel plugin
$(document).ready(function() {
    jQuery('#mycarousel').jcarousel();
});


// (5)
/*-----------------------
* jQuery Plugin: Scroll to Top
* by Craig Wilson, Ph.Creative (http://www.ph-creative.com)
* 
* Copyright (c) 2009 Ph.Creative Ltd.
* Description: Adds an unobtrusive "Scroll to Top" link to your page with smooth scrolling.
* For usage instructions and version updates to go http://blog.ph-creative.com/post/jquery-plugin-scroll-to-top.aspx
* 
* Version: 1.0, 12/03/2009
-----------------------*/
$(function(){$.fn.scrollToTop=function(){$(this).hide().removeAttr("href");if($(window).scrollTop()!="0"){$(this).fadeIn("slow")}var scrollDiv=$(this);$(window).scroll(function(){if($(window).scrollTop()=="0"){$(scrollDiv).fadeOut("slow")}else{$(scrollDiv).fadeIn("slow")}});$(this).click(function(){$("html, body").animate({scrollTop:0},"slow")})}});

// Activate scroll to top
$(function() {
	$("#toTop").scrollToTop();
});


// (6)
/*
 * 	Easy Tooltip 1.0 - jQuery plugin
 *	written by Alen Grakalic	
 *	http://cssglobe.com/post/4380/easy-tooltip--jquery-plugin
 *
 *	Copyright (c) 2009 Alen Grakalic (http://cssglobe.com)
 *	Dual licensed under the MIT (MIT-LICENSE.txt)
 *	and GPL (GPL-LICENSE.txt) licenses.
 *
 *	Built for jQuery library
 *	http://jquery.com
 *
 */ 
(function($) {
	$.fn.easyTooltip = function(options){
	  
		// default configuration properties
		var defaults = {	
			xOffset: 10,		
			yOffset: 25,
			tooltipId: "easyTooltip",
			clickRemove: false,
			content: "",
			useElement: ""
		}; 
			
		var options = $.extend(defaults, options);  
		var content;
				
		this.each(function() {  				
			var title = $(this).attr("title");				
			$(this).hover(function(e){											 							   
				content = (options.content != "") ? options.content : title;
				content = (options.useElement != "") ? $("#" + options.useElement).html() : content;
				$(this).attr("title","");									  				
				if (content != "" && content != undefined){			
					$("body").append("<div id='"+ options.tooltipId +"'>"+ content +"</div>");		
					$("#" + options.tooltipId)
						.css("position","absolute")
						.css("top",(e.pageY - options.yOffset) + "px")
						.css("left",(e.pageX + options.xOffset) + "px")						
						.css("display","none")
						.fadeIn("fast")
				}
			},
			function(){	
				$("#" + options.tooltipId).remove();
				$(this).attr("title",title);
			});	
			$(this).mousemove(function(e){
				$("#" + options.tooltipId)
					.css("top",(e.pageY - options.yOffset) + "px")
					.css("left",(e.pageX + options.xOffset) + "px")					
			});	
			if(options.clickRemove){
				$(this).mousedown(function(e){
					$("#" + options.tooltipId).remove();
					$(this).attr("title",title);
				});				
			}
		});
	};
})(jQuery);

$(document).ready(function(){	
	$("a.tooltip").easyTooltip();
});
