var imgIndex = 1;
var malibu_count = 0;
var isAuthorized = false;

function malibu_add() {
	malibu_count += 1;
	var malibu_url = 'http://www.malibu-rum.com/islandize/images/';
	var count = 6;
	var imgFiles = new Array("coconut_drink_sm.gif", "malibu_stuff.gif", "palmtree_sm.gif", "pelican_sm.gif", "sunset_sm.gif", "wave_1_sm.gif");
	if(imgIndex == (count) ){
	   imgIndex=0;
	}
    imgIndex++;
	var filename = imgFiles[imgIndex-1];

	var heightRandom = Math.random()*.8;
	var windowWid = 1000;
	var windowHght = 760;
	var height = 0;
	var width = 0;
	var docelem = document.documentElement;
	if (typeof(window.innerHeight) == 'number') {
		windowHght = window.innerHeight;
		windowWid = window.innerWidth;
	} else {
		windowHght = docelem.clientHeight;
		windowWid = docelem.clientWidth;

		if (windowWid == 0) windowWid = 1000;
		if (windowHght == 0) windowHght = 600;
		
	}
	
	var img = document.createElement('img');
	img.style.position = 'absolute';
	img.style.zIndex = malibu_count + 99999;
	img.style.outline = 0;
	img.className= 'drag';
	img.style.left = Math.round( Math.floor((windowWid-100)*Math.random()) ) + "px";
	img.style.top = Math.round( windowHght*heightRandom + getScrollY()) + "px";

	img.setAttribute('src',malibu_url + filename);
	var dragable1 = DragHandler.attach(img);
	var body = document.getElementsByTagName('body')[0];
	body.appendChild(img);
		
}

function getScrollY() {
	var sy = 0;

	if (window.scrollY != undefined) {
		sy = window.scrollY;
	} else {
		sy = document.body.parentNode.scrollTop;
	}
	
	return sy;
}

function malibu_add_try() {
	if (!isAuthorized) {
		// Not authorized, ask for permission

		if (document.getElementById('__malibu_popup') != null) closeAgePopup();

		var tw = 1000;
		var th = 760;

		// Flash size
		var fw = 412;
		var fh = 275;

		if (typeof(window.innerHeight) == 'number') {
			tw = window.innerWidth;
			th = window.innerHeight;
		} else {
			var docelem = document.documentElement;
			tw = docelem.clientWidth;
			th = docelem.clientHeight;
			
			if (tw == 0) tw = 1000;
			if (th == 0) th = 600;
			
		}

		var el = document.createElement("div");
		el.id = "__malibu_popup";
		el.style.position = 'absolute';
		el.style.zIndex = 999999;
		//el.className= 'drag';
		el.style.left = Math.round((tw-fw)/2) + "px";
		el.style.top = Math.round((th-fh)/2 + getScrollY()) + "px";
		el.style.width = fw+"px";
		el.style.height = fh+"px";
		el.style.backgroundColor="#ffffff";

		var inEl = document.createElement("div");
		inEl.id = "__malibu_popup_inner";
		el.appendChild(inEl);

		var body = document.getElementsByTagName('body')[0];
		body.appendChild(el);

		var so = new SWFObject("http://www.malibu-rum.com/islandize/agecheck.swf", "index", fw+"px", fh+"px", "9", "#ffffff");
		//so.addParam("wmode", "transparent");
		so.addParam("allowFullScreen", "true");
		so.addParam("allowScriptAccess", "always");
		so.addParam("allowNetworking", "all");
		so.write("__malibu_popup_inner");

		/*
		// [
		el.appendChild(document.createTextNode("[ "));
		
		// Auth link
		var lnk = document.createElement("a");
		lnk.href = "javascript:ageAuthorize();";
		lnk.appendChild(document.createTextNode("Authorize"));
		el.appendChild(lnk);

		// |
		el.appendChild(document.createTextNode(" | "));

		// UnAuth link
		lnk = document.createElement("a");
		lnk.href = "javascript:ageUnAuthorize();";
		lnk.appendChild(document.createTextNode("UnAuthorize"));
		el.appendChild(lnk);

		// ]
		el.appendChild(document.createTextNode(" ]"));
		
		var dragHandleEl = DragHandler.attach(el);
		*/

	} else {
		// Already authorized, directly add a new image
		malibu_add();
	}
}

function ageAuthorize() {
	closeAgePopup();
	isAuthorized = true;
	malibu_add();
}

function ageUnAuthorize() {
	closeAgePopup();
}

function closeAgePopup() {
	document.getElementById('__malibu_popup_inner').id = "____xx";
	document.getElementById('__malibu_popup').style.zIndex = -document.getElementById('__malibu_popup').style.zIndex;
	document.getElementById('__malibu_popup').style.display = "none";
	document.getElementById('__malibu_popup').id = "____yy";
	//document.getElementById('__malibu_popup').removeChild(document.getElementById('__malibu_popup_inner'));
	//document.getElementsByTagName('body')[0].removeChild(document.getElementById('__malibu_popup'));
}

/**
*
*  Crossbrowser Drag Handler
*  http://www.webtoolkit.info/
*
**/
 
var DragHandler = {
 
 
	// private property.
	_oElem : null,
 
 
	// public method. Attach drag handler to an element.
	attach : function(oElem) {
		oElem.onmousedown = DragHandler._dragBegin;
 
		// callbacks
		oElem.dragBegin = new Function();
		oElem.drag = new Function();
		oElem.dragEnd = new Function();
 
		return oElem;
	},
 
 
	// private method. Begin drag process.
	_dragBegin : function(e) {
		var oElem = DragHandler._oElem = this;
 
		if (isNaN(parseInt(oElem.style.left))) { oElem.style.left = '0px'; }
		if (isNaN(parseInt(oElem.style.top))) { oElem.style.top = '0px'; }
 
		var x = parseInt(oElem.style.left);
		var y = parseInt(oElem.style.top);
 
		e = e ? e : window.event;
		oElem.mouseX = e.clientX;
		oElem.mouseY = e.clientY;
 
		oElem.dragBegin(oElem, x, y);
 
		document.onmousemove = DragHandler._drag;
		document.onmouseup = DragHandler._dragEnd;
		return false;
	},
 
 
	// private method. Drag (move) element.
	_drag : function(e) {
		var oElem = DragHandler._oElem;
 
		var x = parseInt(oElem.style.left);
		var y = parseInt(oElem.style.top);
 
		e = e ? e : window.event;
		oElem.style.left = x + (e.clientX - oElem.mouseX) + 'px';
		oElem.style.top = y + (e.clientY - oElem.mouseY) + 'px';
 
		oElem.mouseX = e.clientX;
		oElem.mouseY = e.clientY;
 
		oElem.drag(oElem, x, y);
 
		return false;
	},
 
 
	// private method. Stop drag process.
	_dragEnd : function() {
		var oElem = DragHandler._oElem;
 
		var x = parseInt(oElem.style.left);
		var y = parseInt(oElem.style.top);
 
		oElem.dragEnd(oElem, x, y);
 
		document.onmousemove = null;
		document.onmouseup = null;
		DragHandler._oElem = null;
	}
 
}