/**
 * Includes js files and runs DOM ready code
 */

 LibMan = {
	// String : The path to libman.js
	path: null,

	/**
	 * Calculates and sets the relative path to libman.js
	 *
	 * @return void  
	 */
	calculatePath: function() {
		// condition : is object var already set?
		if (this.path == null) {
			// get path from LibraryManager javascript include string
			var libman = document.getElementById("libman");
			if (!libman) return false;
			
			// remove the filename before setting the path 
			this.path = libman.src.replace(/libman\.js(\?.*)?$/,'');
		}
	},


	/**
	 * Wrapper for requireFile, allows single filename or array
	 * of filenames to be passed in
	 * 
	 * @param mixed libFiles string or array of paths to files 
	 * to include, absolute or relative to libman.js
	 *
	 * @return void  
	 */
	requireFiles: function(libFiles) {
		if (typeof(libFiles) == "string") { // single file to include (called from php)
			LibMan.requireFile(libFiles);
		} else { // array of files to include (called from libman.js)
			for (var i=0; i < libFiles.length; i++) {
				LibMan.requireFile(libFiles[i]);
			};
		}
	},
	
	
	/**
	 * Includes a js file
	 * 
	 * @param string libFile path to file to include, 
	 * absolute or relative to libman.js
	 *
	 * @return void  
	 */
	requireFile: function(libFile){
		// match strings starting with 'http://'
		var pattern = /^http:\/\//;
		
		// if libFile matches pattern, path is absolute, otherwise it's relative
		libFile = (pattern.test(libFile)) ? libFile : this.path + libFile;
		document.write('<script type="text/javascript" src="'+ libFile +'"></script>');
	}
}

/*
 * site-specific file includes
 */
var requiredFiles = [
	,'tonic/swfobject/swfobject.js'
	,'tonic/livequery/jquery.livequery.min.js'
	,'tonic/hoverintent/jquery.hoverIntent.minified.js'
	 ];
	
LibMan.calculatePath();
LibMan.requireFiles(requiredFiles);

	
// allows css styles depending if js is enabled
$('html').addClass('hasJs');

// on DOM ready
$(function() {
	// product primer
	var $elementHeight = $("#productglossary").height(); 
	//console.log($elementHeight);
	$("#element").css({height: $elementHeight});
	
	
	$('#productexpander').click(function () {
		$('#productglossary').slideToggle(1500);

		if( $('#productexpander').is('.headerShown') ) {
		    $('#productexpander').removeClass('headerShown');
		} else {
		    $('#productexpander').addClass('headerShown');
		}
		
		return false;
	});
	
	// store value for product glossary
	var glossarybg = $('#productglossary .rollover').css('backgroundImage');
	var glossaryinfo = $('#productglossary #info').html();
	
	// rollovers in product primer
	$('#productglossary #links a').hover(
		function(){
			// get product id from link class
			var prodid = $(this).attr('class');
			
			// setup gateway 
			var methodgateway = LibMan.path+'../php/tonic/ajax/AJAXGateway.php?serv=CMS&func=getCgFormatData';
			var type = 'Cgformat';
			
			// get info for product
			$('#productglossary #ajaxreturn').load(methodgateway+'&id='+prodid+'&type='+type+'&path='+LibMan.path);
		},
		function(){
			$('#productglossary .rollover').css('backgroundImage', glossarybg);
			$('#productglossary #info').html(glossaryinfo);
		}
	);

	// home page
	if ($("body#home").length > 0) {
	
		// rollovers on home page
		$('#featcampaigns a').hover(
			function(){
				// fadeout any other feat campaigns
				$('#featcampaigns a span').each(function(){
					$(this).fadeOut('fast');
				});
				
				// highlight current feat campaign
				$(this).find('span:not(:animated)').fadeIn('fast');
				
				// get product id from link class
				var id = parseInt($(this).attr('class'));
				
				// setup gateway 
				var methodgateway = LibMan.path+'../php/tonic/ajax/AJAXGateway.php?serv=CMS&func=getFeaturedCampaignData';
				var type = 'Cgcampaign';
				
				// get ajax response and fade it into the correct part of the page
				$.getJSON(methodgateway+'&id='+id+'&type='+type+'&path='+LibMan.path, function(data){

					// replace image
					$('#maincampaign').find('img').fadeOut('normal', function(){
						$(this).attr('src', data.imgsrc).fadeIn('normal');
					});

					// replace text
					$('#maincampaign div > *').fadeOut('normal', function(){
						$(this).parent().html(data.textdata);
						$(this).fadeOut(1).fadeIn('normal');
					});
				});
			}, function(){
				// blank
			}
		);
	}
	
	// campaign gallery pages
	if ( $('body#campaigngallery').length > 0) {
		
		// campaign gallery, select boxes updated via ajax
		$('.updater').livequery('change', function () {
			
			// Horrible way of clearing the freesearch text box input
			$('#freesearch').val('Search for...');
		 	
		 	var methodgateway = LibMan.path+'../php/tonic/ajax/AJAXGateway.php?serv=CMS&func=getCgSelectData';
		 	
			// need to get other select values
			var params = "";
			$("#campaigngallerysearch select").each(function(){
				params += "&"+this.id+"=";
				params += this.value;
			});
//console.log(params);
			// lets still separate the one we clicked on for clarity
			var type = this.id; //console.log(type);
			var value = this.value; //console.log(value);
	
			// #dependentselects
			$('#dependentselects').load(methodgateway+'&value='+value+'&type='+type+params);
			
		});
		
		// remove automatically added search instructions on focus
		$('#freesearch').focus(function(){
			if (this.value == 'Search for...') {
				this.value = '';
			}
		});
		
		// image rollovers
		$('ul#galleryThumbs li').hover(function(){
			$(this).find('span').fadeIn("fast");
		}, function(){
			$(this).find('span').fadeOut("fast");		
		});
	}
	
	// contact page - load google map
	if ( $("body#contactus").length > 0 ) {googleMap();}
	
	// come play page - load swf
	if ( $("body#comeplay").length > 0 ) {
		// setup swfobject
		var flashvars, params, attributes = {};
		params = {allowscriptaccess: "always"};
		
		swfobject.embedSWF(LibMan.path+"../../comeplay/play.swf", "flashreplace", "800", "600", "9.0.0","expressInstall.swf", flashvars, params);
	}
	
	// meet the team page - load swf
	if ( $("body#meettheteam").length > 0 ) {
		// setup swfobject
		var flashvars, params, attributes = {};
		params = {allowscriptaccess: "always"};
		
		swfobject.embedSWF(LibMan.path+"../../aboutus/meettheteam/whos_who.swf", "flashreplace", "800", "600", "9.0.0","expressInstall.swf", flashvars, params);
	}	
});

// image preloading (this is not very DRY).  once everything else has loaded get ajax rollover images.
$(window).bind('load', function(){
	// home page
	if ($("body#home").length > 0) {
		$('#featcampaigns a').each(function(){
			// get product id from link class
			var id = parseInt($(this).attr('class'));
				
			// setup gateway 
			var methodgateway = LibMan.path+'../php/tonic/ajax/AJAXGateway.php?serv=CMS&func=getFeaturedCampaignData';
			var type = 'Cgcampaign';
			
			// create image from ajax response.  
			$.getJSON(methodgateway+'&id='+id+'&type='+type+'&path='+LibMan.path, function(data){
				var $img = $('<img>').attr('src', data.imgsrc);
				$img.css({display: 'none'});
				$('body').append($img);
			});
		});
	};
});