jQuery.extend({
    //dims the screen
    dimScreen: function(speed, opacity, callback) {
        if(jQuery('#__dimScreen').size() > 0) return;
        
        if(typeof speed == 'function') {
            callback = speed;
            speed = null;
        }

        if(typeof opacity == 'function') {
            callback = opacity;
            opacity = null;
        }

        if(speed < 1) {
            var placeholder = opacity;
            opacity = speed;
            speed = placeholder;
        }
        
        if(opacity >= 1) {
            var placeholder = speed;
            speed = opacity;
            opacity = placeholder;
        }
		
		height = ($(document).height() < $(window).height()) ? $(window).height() : $(document).height();
		height = (height < 500) ? 500 : height;
		width = ($(document).width() < $(window).width()) ? $(window).width() : $(document).width();
		width = (width < 680) ? 680 : width;
        speed = (speed > 0) ? speed : 500;
        opacity = (opacity > 0) ? opacity : 0.5;
        return jQuery('<div></div>').attr({
                id: '__dimScreen'
                ,fade_opacity: opacity
                ,speed: speed
            }).css({
            background: '#000'
            ,height: height + 'px'
            ,left: '0px'
            ,opacity: 0
            ,position: 'absolute'
            ,top: '0px'
            ,width: width + 'px'
            ,zIndex: 999
        }).appendTo(document.body).fadeTo(speed, opacity, callback);
    },
    
    //stops current dimming of the screen
    dimScreenStop: function(callback) {
        var x = jQuery('#__dimScreen');
        var opacity = x.attr('fade_opacity');
        var speed = x.attr('speed');
        x.fadeOut(speed, function() {
            x.remove();
            if(typeof callback == 'function') callback();
        });
    }
});

var adCurtainHeight = 700;
var adCurtainWidth = 700;
var adCurtainFile = '../curtainads/greatdeals_WMGV.swf';


function insertadCurtain() {
	var adCurtainPosition = ($(document).width()/2)-340;
	var sba = readCookie('sba');
	if ((sba != null) && (sba != "")) {
		startAdCurtain = 0;
	} else {		
		startAdCurtain = 1;
		createCookie('sba',startAdCurtain, 0.5);
		$.dimScreen(500, 0.8, function() {
			$("object").toggle();			   
			$(window).resize(function () {
				resizeDimmer();
			});
			$("body").append("<div id=\"adCurtain\" style=\"display: none;\"></div>");
			$("#adCurtain").css({ height:(adCurtainHeight+"px"), width:(adCurtainWidth+"px"), position:"absolute", top:"0", left:(adCurtainPosition+"px"), zIndex:"1050", display:"block" });	
			var adCurtain = new SWFObject(adCurtainFile, "the_adCurtain", adCurtainWidth, adCurtainHeight, "8");
			adCurtain.addParam("quality", "high");
			adCurtain.addParam("wmode", "transparent");
			adCurtain.write("adCurtain");
		});
	}
}

function resizeDimmer() {
	height = ($(document).height() < $(window).height()) ? $(window).height() : $(document).height();
	height = (height < 500) ? 500 : height;
	width = ($(document).width() < $(window).width()) ? $(window).width() : $(document).width();
	width = (width < 680) ? 680 : width;
	adCurtainPosition = ($(document).width()/2)-340;
	$("#adCurtain").css({left: (adCurtainPosition+"px") });
	$("#__dimScreen").css({height:(height+"px"), width:(width+"px") });
}

function removeadCurtain() {
	$("#adCurtain").remove();
	$(window).unbind('resize',resizeDimmer);
	$.dimScreenStop();
	$("object").toggle();
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);

	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

$(document).ready(function(){

	window.onload = insertadCurtain();

});