/**
 * DISNEY UI - Ad
 * @version 1.1
 * @author: Dwight Brown <dwightb at construct7.com>
 */
	 
;(function($)
{
	var classes =
		{
			"container":   "disney-ui-ad",
			"ad-banner":   "ad-banner",
			"ad-small":    "ad-small",
			"ad-medium":   "ad-medium",
			"ad-large":    "ad-large",
			"ad-vertical": "ad-vertical"		
		};

	var ads =
		{
			"banner":
			{
				"type":   "Super",
				"url":    "/segments/preschool/",
				"width":  "738",
				"height": "90"
			},
			"small":
			{
				"type":   "Promote_180x150",
				"url":    "/segments/preschool/",
				"width":  "180",
				"height": "160"
			},
			"medium":
			{
				"type":   "MediumRectangle",
				"url":    "/segments/preschool/",
				"width":  "300",
				"height": "260"
			},
			"large":
			{
				"type":   "Large",
				"url":    "/segments/preschool/",
				"width":  "400",
				"height": "310"
			},
			"vertical":
			{
				"type":   "Vertical",
				"url":    "/segments/preschool/",
				"width":  "160",
				"height": "610"
			}
		};

    var _baseURL  = "http://disney.go.com/Adserver?";
    var _params = { CallDown: "", adcategory: "coview/preschool/ftt" };
	var _timerId  = null;
	var _refreshInterval = 180;
	var _timerFunc = function()
		{
			$("." + classes.container + " iframe").each( function()
			{
				this.src = this.src;
			});
		};
	
	$.fn.Ad = function( adType, options )
	{
		var empty;
	
		// Set default options
		options = options || {};
        options.refreshInterval   = options.refreshInterval   || _refreshInterval;
        options.baseURL           = options.baseURL           || _baseURL;
        options.width             = options.width             || ads[adType].width;
        options.height            = options.height            || ads[adType].height;
        options.category          = options.category          || _params.adcategory;
        options.params            = options.params            || {};
        options.params.CallDown   = options.params.CallDown   || _params.CallDown;
        options.params.AdTypes    = options.params.AdTypes    || ads[adType].type;
        options.params.url        = options.params.url        || ads[adType].url;
        options.params.adcategory = options.params.adcategory || options.category;
        if ( options.params.adcategory == "null" ) {
        	options.params.adcategory = options.category;
        }
	//Always set the url value to be equal to the adcategory value, then get rid of
	//the adcategory value
	options.params.url = options.params.adcategory;
	delete options.params.adcategory;

        var _url = options.baseURL;
        
        for ( param in options.params )
        {
        	if ( options.params[param] != "" && options.params[param] != "undefined" )
        	{
            	_url += param + "=" + options.params[param] + "&";
            }
            else
            {
	            _url += param + "&"
            }
        }
    
		if ( options.refreshInterval && options.refreshInterval > 0 )
		{
			window.clearInterval( _timerId );
			_timerId = window.setInterval( _timerFunc, (options.refreshInterval * 1000) );
		}
		else if ( (options.refreshInterval == 0 || options.refreshInterval == false) && options.refreshInterval != "undefined" )
		{
			window.clearInterval( _timerId );
		}
	
		return this.each( function()
		{
			$(this).html( '<div class="' + classes.container + ' ' + classes['ad-'+adType] + '">' +
			              '<iframe src="' + _url + '"' +
			              '        width="' + options.width + '"' +
			              '        height="' + options.height + '"' +
			              '        marginwidth="0"' +
			              '        marginheight="0"' +
			              '        hspace="0"' +
			              '        vspace="0"' +
			              '        frameborder="0"' +
			              '        scrolling="no"' +
			              '        bordercolor="#000000"></iframe>' +
			              '</div>' );
		});
	};
	
	_timerId = window.setInterval( _timerFunc, (_refreshInterval * 1000) );
})(jQuery);

/* Avoid errors in browser that do not support console.log() */
if ( !window.console )
{
	window.console = {};
	console.log = function(){};
}