// general
require_once("/templates/js/lib/json2.js");
require_once("/templates/js/lib/jquery/jquery.metadata.js");
require_once("/templates/js/lib/jquery/jquery.flash.js");
require_once("/templates/js/lib/jquery/jquery.cookie.js");
require_once("/templates/js/lib/jquery/jquery.tooltip.js");

// for image gallery
require_once("/templates/js/lib/fancybox/jquery.fancybox-1.3.1.pack.js");

// for forms
require_once("/templates/js/lib/jquery/jquery.blockUI.js");
require_once("/templates/js/lib/jquery/jquery.validate.js");
require_once("/templates/js/lib/jquery/jquery.validate.methods.js");
require_once("/templates/js/lib/jquery/jquery.timeentry.js");
require_once("/templates/js/lib/jquery/jquery.ui.datepicker.js");
require_once("/templates/js/lib/jquery/jquery.form.js");

// module includes
require_once("/templates/js/glow_form.js");
require_once("/templates/js/glow_imagegallery.js");

var ie = $.browser.msie;
var ie6 = $.browser.msie && parseInt($.browser.version)==6;
var ie7 = $.browser.msie && parseInt($.browser.version)==7;
var ff2 = $.browser.mozilla && parseFloat($.browser.version)<=1.8;
var hasFlash = $.fn.flash.hasFlash('9');

/**
* jquery tooltip defaults
*/
$.extend($.tooltip.defaults, {
 	track: true,
    delay: 250,
    showURL: false,
    //showBody: " - ",
	bodyHandler : function(){ return this.tooltipText; },
	top: -15,
	left: 15,
    fade: 250
});

$(document).ready(function() {
	if(!hasFlash){		
		var altContent = $("<span>You need the <a href='http://get.adobe.com/flashplayer/' >Adobe Flash Player</a><br/>to view this content.</span>");
		$("object[data$=swf]").each(function(n,val){
			var w = $(val).attr('width');
			var h = $(val).attr('height');
			if(w.match(/[0-9]+/)) w = w + 'px';
			if(h.match(/[0-9]+/)) h = h + 'px';			
			var newDiv = $("<div></div>");
			$(newDiv).height(h)
					.width(w)					
					.addClass('noflash')
					.html(altContent);
			$(val).replaceWith(newDiv);
		});
	}
	
	// blockUI defaults (change default message)
	$.extend($.blockUI.defaults,{
		message: "<img hspace='5' src='/templates/images/widgets/throbber.gif' width='16' height='16' style='vertical-align:middle'/> <span class='glow-throbber'>processing...</span>"
		,
		css: { 
			padding:        0,
			margin:         0,
			width:          '30%', 
			top:            '40%', 
			left:           '35%', 
			textAlign:      'center', 
			color:          '#000', 
			border:         '',
			backgroundColor:'',
			cursor:         'wait'
		}
		,
		 // styles for the overlay
	    overlayCSS:  { 
	        backgroundColor:'#fff', 
	        opacity:        '0.8' 
	    }
	});
	
	glow_initStyles();
	
	//////////////////////////////////////////////////////////////
	// MODULE SPECIFIC INITS
	//////////////////////////////////////////////////////////////
	
	//////////////////////////////////////////////////////////////
	// Init other components
	glow_initImageGalleries();
	glow_initForms();		
});

/**
 * init visual stuff
 */
function glow_initStyles(){
	//////////////////////////////////////////////////////////////
	// MAIN NAV: apply padding to two columns (items 1 and 2) 
	$("li:eq(0),li:eq(1)", "#main-nav").css("padding-top","50px");	
	
	
	//////////////////////////////////////////////////////////////
	// flyout menus 	
	$('ul ul li:has(ul)', '#sub-nav').hover(
		function(){
			$(this).data('is-ancestor',$(this).is('.ancestor'))
			$(this).addClass('ancestor');
			$("ul",this).show();		
		},
		function(){
			if(!$(this).data('is-ancestor')) $(this).removeClass('ancestor');
			$("ul",this).hide();
		}
	);
	
	// IE6 fix (patch for menu visibility)
	if(ie6) {
		$("#sub-nav > UL > LI.current  > UL,#sub-nav > UL > LI.ancestor > UL").css('display','block');
		$("UL UL LI.ancestor > A,UL UL LI.current  > A,UL UL A:hover","#sub-nav ").css({
			'background-image' : 'url(../images/left_nav_bg_selected.gif)',
			'color'			   : '#000'
		})
		$("UL UL UL", "#sub-nav").css("left","170px");
	}
	
	if(ie6 || ie7) {$("#footer-border").hide() }
	
	//////////////////////////////////////////////////////////////
	// summary list (icons landing page) remove margin on
	// every 4th item
	$(".summarylist-item:nth-child(4n)","#summarylist").css("margin-right","0");
			
	
	//////////////////////////////////////////////////////////////
	// SEARCH FIELD clear on focus	
	$(":input[type='text']:filled","#searchform").addClass("occupied");
	$(":input","#searchform").focus(function (){ $(this).addClass('occupied')})
							 .blur(function () { if($.trim($(this).attr('value')).length==0) $(this).removeClass('occupied') });
	
	///////////////////////////////////////////////////////////////
	// banded tables (for non-css3 browsers)
	$(".table-banded > TR:even, .table-banded > TBODY > TR:even").addClass("table-row-shaded");
	
	///////////////////////////////////////////////////////////////
	// table wrapper 
	$(".table-banded,.table-standard").wrap("<div class='table-wrapper'></div>");
	$(".table-wrapper").each(function(){
		var tbl		= $('table:first', this)
		var w 		= $(tbl).attr('width') ? $(tbl).attr('width') : $(tbl).css('width');
		var align	= $(tbl).attr('align') ? $(tbl).attr('align') : $(tbl).css('float');		
		$(this).attr('width', w).css('align', align);
		$(tbl).attr('width','100%');
	});
	
	
	//////////////////////////////////////////////////////////////
	// TOOLTIPS (summary list)
	$('a[title!=""]','.summarylist-icon').tooltip();
	
	//////////////////////////////////////////////////////////////
	// captioned images
	//////////////////////////////////////////////////////////////
	// Captioned images (convert to table with caption)
	/*
	$("img:not(.processed):not(.nocaption)").filter(function(index){
		var caption = $(this).attr('title') ? $(this).attr('title') : $(this).attr('alt');
		var hasCaption = caption.search(/^caption\s*:/i) != -1;
		if(hasCaption) $(this).data('caption',caption.replace(/^caption\s*:\s*(.*?)/i, '$1'));
		return hasCaption;
	}).each(function(n, val){
		// add a 'processed' flag to make sure we only do this once
		$(this).addClass('processed');	
		
		// get caption from data
		var caption = $(this).data('caption');
		
		// remove caption: tag from alt and title
		$(this).attr('title', $(this).attr('title').replace(/^caption\s*:\s*(.*?)/i, '$1'));
		$(this).attr('alt', $(this).attr('alt').replace(/^caption\s*:\s*(.*?)/i, '$1'));
		
		// determine alignment
		var align = $(this).attr('align') ? $(this).attr('align') : $(this).css('float');		

		// build table and insert a clone of me (or my parent link if available) into it
		var tbl = $("<table class='img-caption'><tr><td></td></tr></table>");			
		var me = $(this).parent().is("a") ? $(this).parent() : $(this);
		
		// set table alignment
		if(align == "right") tbl.addClass('align-right');
		if(align == "left")  tbl.addClass('align-left');
		tbl.attr('align', align);		
		tbl.width( $(this).width() );
		
		// wrap in table
		$(me).wrap(tbl);
		
		// remove my own alignment properties
		$(this).removeClass('align-right')
				.removeClass('align-left')
				.attr('align','').css('float','none');
		
		// apply caption
		$("tr:last td", $(this).parents("table")).append( $("<div class='caption'>" + caption + "</caption>") );
		
	});*/
}

