/*
	
	Project Name Global Javascript Functions
	By  - ISITE Design

*/

// jquery no conflict. use $j or jQuery outside of ready function
var $j = jQuery.noConflict(); 


// jQuery document ready
jQuery(function($) {

	// JS enabled
	$('html').addClass('js');
	
	// Few setups for IE6
	if(document.all){
		//add class to drop downs and buttons 
	    $('#nav li, button').hover(
			function() { $(this).addClass('over'); },
			function() { $(this).removeClass('over'); }
	    );
	}// if document.all
	
	// table striping for IE
	if ($.browser.msie) { $('tbody tr:odd td').addClass('even'); }
	
	
	/* jQuery Tools widgets */
	
	// scrolling FAQ widget
	if ($('.faq-links .ajaxDropDown').html() == "" || $('.faq-links .ajaxDropDown').length == 0) {
		
		$('.faq-links').find('.faq-list').addClass('scrollable-faq').prepend('<div class="scrollable-topshadow"></div><div class="scrollable-bottomshadow"></div>').end()
			.find('h2').wrap('<div class="scrollable-head"></div>').parent().append('<a class="prev">Previous</a>').end().end()
			.append('<div class="scrollable-foot"><a class="next">Next</a></div>');
		
		$(".scrollable-faq").scrollable({ 
				vertical: true,
				items: 'ul',
				item: 'li',
				next: '',
				visible: 3
			});
		var scrollableApi = $(".scrollable-faq").data('scrollable');
		$('.faq-links .next').click(function(){
			var currentIndex = scrollableApi.getIndex(),
				limit = scrollableApi.getSize(),
				visible = scrollableApi.getConf().visible;
			if (limit - currentIndex <= visible) { return false; } else { scrollableApi.next(); }
		});
	
	}
	
	// home page video tooltips
	/**/$(".video-listing li").each(function() {
		var $img = $('img', this),
			$a = $('a[title]', this);
		 if ($a.attr('title') == undefined) return false;
		$img.attr('title', $a.attr('title'));
		$a.removeAttr('title');
		$img.tooltip({effect: 'fade', fadeInSpeed:400, fadeOutSpeed:200});
	});
	
	
	$(".video-listing img[title]").tooltip({effect: 'fade', fadeInSpeed:400, fadeOutSpeed:200});
	
	// home page video overlay
	if($('.video-listing a[rel]').length) {
		$('body').append('<div id="mask"></div><div class="simple-overlay" id="video-overlay"><div class="contentWrap"></div><p class="more"><a href="http://tour.insightschools.net/">Take our interactive tour</a></p></div>');
	}
	
	$('.video-listing a[rel]').overlay({
			top:"25%",			
			onBeforeLoad: function() {
				var wrap = this.getOverlay().find(".contentWrap"),
					height = $('body').outerHeight() + 'px';
				wrap.append('<iframe src="'+this.getTrigger().attr("href")+'" frameborder="0" id="overlay-iframe"></iframe>');
				$('#mask').css({'display': 'block', 'height': height});
			},
			onClose: function() {
				$('#overlay-iframe').remove();
				$('#mask').css('display', 'none');
			}
	});
	
	
	// schools jump links in footer
	$('#school-finder').jumpWidget();
	

});// document ready


// application logic


// jQuery plugins
jQuery.fn.jumpWidget = function() {
	return this.each(function(){
		var $this = jQuery(this),
			$selected = jQuery('h3', $this),
			$items = jQuery('ul a', $this),
			$select = jQuery('<select id="quicklinks"></select>'),
			$button = jQuery('<a class="button find-school" href="">Find Your School</a>');
			
		// fill the select with the h3 as selected value and links in the list
		$select.append('<option value="" selected="selected">'+$selected.text()+'</option>');
		$items.each(function(){
			$select.append('<option value="'+this.href+'">'+this.firstChild.nodeValue+'</option>');
		});
		
		// bind change behavior to the select to change the href on the button link to match currently selected option
		$select.change(function(){
			var val = jQuery('option:selected', $select).attr('value');
			$button.attr('href', val);
		});
		
		//remove the old stuff and insert the select and the button
		$selected.remove();
		jQuery('ul', $this).remove();
		$this.append($select).append($button);
	});
};

// etc

// clean console.log
function cl(){ if(window.console&&window.console.firebug) { var args = [].splice.call(arguments,0); console.log(args.join(" ")); } }//cl()
// example: cl("If you use cl() instead of console.log(), it won't break IE when you forget to take it out.");


// IE6 fixes
// make sure IE has the abbr and acronym tag
if(document.all){
	document.createElement("abbr");
	document.createElement("acronym");
}
// prevent IE6 flicker
try { document.execCommand('BackgroundImageCache', false, true); } catch(e) {}
