/**
 * Google Ad manager inside of a window shade jquery plugin 
 * @usage:  jQuery("#ad-container").gamwindowshade([options]);
 *
 *	var options = {
 *     	'autostart': autostart,
 *	    'openHTML': '<img id="ad-open" src="/ads/ad-open-btn.png" alt="ad open button" />',
 *		'closeHTML': '<img id="ad-close" src="/ads/ad-close-btn.png" alt="ad close button" />',
 *	   	'innerContainer': "#gamMallsPlacement#",
 *	   	'paneWidth': 336,
 *      'paneHeight': 336, 
 *		'posx': '40%',
 *		'posy': '0px'
 *  };
 *  
 * note, use an ID or a specific selector, since you will only want this to work on one.
 * @author: mcn
 */
;(function(jQuery) {
	jQuery.fn.gamwindowshade = function(options){
		var elem = jQuery(this);
		var timers = [];
		if (typeof(options) == 'undefined') {
			var options = {};
		}
		var rollOutAndClose = function(){
			timers[0] = setTimeout(options.slideOpen,1000);
			timers[1] = setTimeout(options.slideClose,7000);
		};
		var iopen,iclose,icontainer,iitem;
		iopen       = '<img id="ad-open"  src="ad-open-btn.png"  alt="ad open button" />';
		iclose      = '<img id="ad-close" src="ad-close-btn.png" alt="ad close button" />';
		icontainer  = "#adSlider";	
		// initialize defaults
		//
		var defaultOptions = {
				openHTML: iopen,
				closeHTML: iclose,
				initBehavior: rollOutAndClose,
				innerContainer: icontainer,
				paneHeight: 600,
				paneWidth: 600,
				easingTime: 2500,
				autostart: false, 
				posx: '50%',
				posy: '0%'
		};
		// override defaults
		//
		for (var key in defaultOptions) {
			if (typeof options[key] == 'undefined') {
				options[key] = defaultOptions[key];
			}
		}
		var containerSelector = options.innerContainer;
		var paneSelector      = jQuery(containerSelector).attr('id')      || null;
		var openSelector      = jQuery(options.openHTML).attr('id')       || null;
		var closeSelector     = jQuery(options.closeHTML).attr('id')      || null;		
		// Extend options with additional features/behaviors utility functions
		//
		if (typeof options.showCloseButton == 'undefined') options.showCloseButton = function(){
			jQuery("#"+closeSelector).css("display", "block");
			jQuery("#"+openSelector).css("display", "none");  //fix for ie
		}
		if (typeof options.showOpenButton == 'undefined') options.showOpenButton = function(){
			jQuery("#"+openSelector).css("display", "block");
			jQuery("#"+closeSelector).css("display", "none");
		}
		if (typeof options.slideOpen == 'undefined') options.slideOpen = function(){	
			jQuery("#"+openSelector).css("display", "none");
			jQuery('#google_ads_div_'+options.innerContainer+'_ad_container').css('height',options.paneHeight).css('width',options.paneWidth);
			jQuery('#google_ads_div_'+options.innerContainer+'_ad_container').slideToggle(options.easingTime);
			setTimeout(options.showCloseButton,options.easingTime);
		}
		if (typeof options.slideClose == 'undefined') options.slideClose = function(){
			jQuery("#"+closeSelector).css("display", "none");
			jQuery("#google_ads_div_"+options.innerContainer+'_ad_container').slideToggle(options.easingTime);
			setTimeout(function(){
				jQuery('#google_ads_div_'+options.innerContainer+'_ad_container').css('height','0px').css('width','0px');	
			},options.easingTime);
			setTimeout(options.showOpenButton,options.easingTime);
		}
		/*
		 * Create window shade structure from options, ie: an open, close button, and the ad as google
		 * ad manager would jam it into the DOM, complete with callbacks and transparency (in iframe(s))
		 */
		
		//
		// Initial state of pane, open and close selectors
		//
		jQuery('#google_ads_div_'+options.innerContainer+'_ad_container').css("display", "none");
		jQuery('#google_ads_div_'+options.innerContainer+'_ad_container').css("width", options.paneWidth).css("height", options.paneHeight);
		jQuery("#"+options.innerContainer).css("width", '0px').css("height", '0px');
		jQuery("#"+options.innerContainer).css("top",options.posy).css("left",options.posx);
		jQuery("#"+options.innerContainer).prepend(options.openHTML);
		jQuery("#"+options.innerContainer).append(options.closeHTML);
		jQuery('#'+closeSelector).css("display", "none");
		jQuery('#'+openSelector).css("display", "block");
		//
		// Bind click events to open and close selectors
		//
		jQuery('#'+openSelector).click(function(){
			options.slideOpen();
			for (var i in timers){
				clearTimeout(timers[i]);
			}
		});
		jQuery('#'+closeSelector).click(function(){
			options.slideClose();
			for (var i in timers){
				clearTimeout(timers[i]);
			}
		});
		// Call initial behavior
		//
		if (options.autostart) {
			options.initBehavior();
		}	
	};
})(jQuery);
