function ViewportOffset (w, h) {
	window.resizeBy(w, h);
}

var domloaded = false;
var dataloaded = false;
var data;
var lookup;
var loader;
var index = -1;
var photo_objects = new Array();

if (!redrocketcms.classes.FlashSite.isLoaded()) {
	loader = new redrocketcms.classes.AjaxConnection();
	loader.onLoad = onDataLoad;
	loader.load(rootpath + "setup.txt");
	swfobject.addDomLoadEvent(onDomLoad);
	swfobject.createCSS("#container", "display: none");
}

function onDomLoad () {
	domloaded = true;
	if (dataloaded) setTimeout("DHTMLSite()", 500);
}

function onDataLoad () {
	dataloaded = true;
	parseData(loader.getText());
	if (domloaded) setTimeout("DHTMLSite()", 500);
}

function DHTMLSite () {
	var container = document.getElementById("container");
	container.style.display = "block";
	checkForVideo();
	fixHiddenNavItems();
	resizeViewport();
	enableAlbum();
	window.onresize = doLayout;
	doLayout();
}

function showVideo () {
	var isIPhone = navigator.userAgent.indexOf('iPhone')!=-1;
	if (isIPhone) {
		document.location.replace(lookup[0].mp4);
		return;
	}
	var image = document.getElementById("image");
	var embed = '<object classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" width="100%" height="100%" codebase="http://www.apple.com/qtactivex/qtplugin.cab">';
	embed += '<param name="src" value="' + lookup[0].mp4 + '">';
	embed += '<param name="autoplay" value="false">';
	embed += '<embed src="' + lookup[0].m4v + '" width="100%" height="100%" type="video/quicktime" autoplay="false" pluginspage="http://www.apple.com/quicktime/download/">';
	embed += '</embed></object>';
	image.innerHTML = embed;
	var videoPlay = document.getElementById("video-play");
	videoPlay.style.display = "none";
}

function checkForVideo () {
	lookup = new Array();
	var container = document.getElementById("container");
	var splitviews = getElementsByClassName("Splitview", "div", container);
	for (var i=0; i<splitviews.length; ++i) {
		var splitview = splitviews[i];
		var obj = new Object();
		var data = getElementsByClassName("Data", "div", splitview)[0];
		var lines = data.innerHTML.split("\n");
		for (var j=0; j<lines.length; ++j) {
			var bits = lines[j].split("=");
			var name = bits.shift();
			var value = bits.join("=");
			obj[name] = value;
		}
		if (obj.flv||obj.m4v) {
			var videoPlay = document.getElementById("video-play");
			videoPlay.style.display = "block";
			videoPlay.onclick = function() {
				showVideo();
			}
		}
		lookup.push(obj);
	}
}

function fixHiddenNavItems () {
	if (navigator.userAgent.indexOf('iPhone')==-1) return;
	var navigation = document.getElementById("navigation");
	var lis = navigation.getElementsByTagName("li");
	for (var j=0; j<lis.length; ++j) {
		var li = lis[j];
		if (li.className.indexOf("NoHref")!=-1) {
			var anchor = li.getElementsByTagName("a")[0];
			anchor.setAttribute("href", "javascript:void(0);");
			li.style.cursor = "pointer";
			li.onclick = function() {
				if (this.className.indexOf("sfhover")!=-1) this.className = this.className.replace(new RegExp(" sfhover\\b"), "");
				else this.className += " sfhover";
			}
		}
	}
}

function resizeViewport () {
	var dims = redrocketcms.utilities.getBrowserDimensions();
	var height = Math.max(dims[1], data.site_minimum_height);
	var width = Math.max(dims[0], data.site_minimum_width);
	var body_height = height - data.top_bar_height - data.bottom_bar_height;
	var body_width = Math.round((body_height*data.site_aspect_ratio[0])/data.site_aspect_ratio[1]);
	ViewportOffset(body_width-dims[0], 0);
	var thumbnailWrapper = document.getElementById("thumbnail-wrapper");
	if (!thumbnailWrapper) {
		var display = document.getElementById("display");
		display.style.position = "absolute";
		display.style.left = (width/3) + "px";
	}
}

function doLayout () {
	var dims = redrocketcms.utilities.getBrowserDimensions();
	var header = document.getElementById("header");
	var navigation = document.getElementById("navigation");
	var content = document.getElementById("cms-content");
	var height = Math.max(dims[1], data.site_minimum_height);
	var width = Math.max(dims[0], data.site_minimum_width);
	content.style.height = (height - data.top_bar_height - data.bottom_bar_height) + "px";
	var thumbnailWrapper = document.getElementById("thumbnail-wrapper");
	var thumbnails = document.getElementById("thumbnails");
	var nav_width = navigation.getElementsByTagName("ul")[0].offsetWidth;
	navigation.style.width = nav_width + "px";
	var display = document.getElementById("display");
	display.style.position = "absolute";
	if (thumbnailWrapper) {
		display.style.left = "0px";
		var button_width = Math.floor(data.thumbnail_width/2);
		thumbnailWrapper.style.width = (width-nav_width-1) + "px";
		thumbnailWrapper.style.left = (nav_width+1) + "px";
		thumbnailWrapper.style.top = (height - data.bottom_bar_height) + "px";
		thumbnails.style.width = (width-nav_width-1-(button_width*2)) + "px";
		thumbnails.style.left = button_width + "px";
		thumbnails.style.top = "0px";
		var prevImage = document.getElementById("prev-image");
		var nextImage = document.getElementById("next-image");
		nextImage.style.top = "0px";
		nextImage.style.left = (width-nav_width-1-button_width) + "px";
		var image = document.getElementById("image");
		image.style.width = width + "px";
		image.style.height = (height - data.top_bar_height - data.bottom_bar_height) + "px";
		var img = image.getElementsByTagName("img")[0];
		if (img) {
			img.setAttribute("width", Math.floor(width));
			img.setAttribute("height", Math.floor(height - data.top_bar_height - data.bottom_bar_height));
		}
		var progress = document.getElementById("progress");
		progress.style.width = width + "px";
		progress.style.height = (height - data.top_bar_height - data.bottom_bar_height) + "px";
	} else {
		display.style.left = (width/3) + "px";
		var image = document.getElementById("image");
		image.style.width = ((width/3)*2) + "px";
		image.style.height = (height - data.top_bar_height - data.bottom_bar_height) + "px";
		var img = image.getElementsByTagName("img")[0];
		if (img) {
			img.setAttribute("width", Math.floor((width/3)*2));
			img.setAttribute("height", Math.floor(height - data.top_bar_height - data.bottom_bar_height));
		}
		var progress = document.getElementById("progress");
		progress.style.width = ((width/3)*2) + "px";
		progress.style.height = (height - data.top_bar_height - data.bottom_bar_height) + "px";
		var videoPlay = document.getElementById("video-play");
		videoPlay.style.width = ((width/3)*2) + "px";
		videoPlay.style.height = (height - data.top_bar_height - data.bottom_bar_height) + "px";
		var desc = document.getElementById("desc");
		desc.style.height = (height - data.top_bar_height - data.bottom_bar_height - desc.offsetTop - 20) + "px";
	}
}

function showPrevImage () {
	var i = index-1;
	if (i==-1) i = photo_objects.length-1;
	showIndex(i);
}

function showNextImage () {
	var i = index+1;
	if (i==photo_objects.length) i = 0;
	showIndex(i);
}

function enableAlbum () {
	var container = document.getElementById("container");
	var thumbnailWrapper = document.getElementById("thumbnail-wrapper");
	var thumbnails = document.getElementById("thumbnails");
	showIndex(0);
	if (!thumbnailWrapper) return;
	var prevImage = document.getElementById("prev-image");
	var nextImage = document.getElementById("next-image");
	prevImage.style.display = "block";
	prevImage.onclick = showPrevImage;
	nextImage.style.display = "block";
	nextImage.onclick = showNextImage;
	thumbnailWrapper.style.position = "absolute";
	var anchors = thumbnails.getElementsByTagName("a");
	for (var j=0; j<anchors.length; ++j) {
		var anchor = anchors[j];
		anchor.href = 'javascript:showIndex(' + j + ');';
	}
	var ul = thumbnails.getElementsByTagName("ul")[0];
	ul.style.position = "absolute";
	ul.style.width = (photo_objects.length*data.thumbnail_width) + "px";
	ul.style.top = "0px";
	ul.style.left = "0px";
	thumbnails.style.float = "none";
	thumbnails.onmouseover = scrollThumbnailView;
	thumbnails.onmouseover = function () {
		if (!this.onmousemove) this.onmousemove = scrollThumbnailView;
	}
	thumbnails.onmouseout = function () {
		this.onmousemove = undefined;
	}
	container.appendChild(thumbnailWrapper);
}

function scrollThumbnailView (event) {
	var ul = this.getElementsByTagName("ul")[0];
	var coords1 = getElementCoord(this);
	var coords2 = getCursorCoord(event);
	var xmouse = coords2[0]-coords1[0];
	var ymouse = coords2[1]-coords1[1];
	var viewable = this.offsetWidth;
	var actual = ul.offsetWidth;
	var scrollable = actual-viewable;
	var positionRatio = xmouse/viewable;
	var scrollOffset = scrollable*positionRatio;
	ul.style.left = "-" + Math.round(scrollOffset) + "px";
}

function hideLoader () {
	var progress = document.getElementById("progress");
	progress.style.display = "none";
}
function showIndex (num) {
	index = num;
	var thumbnails = document.getElementById("thumbnails");
	if (thumbnails) {
		var active = getElementsByClassName("Active", "li", thumbnails)[0];
		if (active) {
			var classes = active.className.split(" ");
			for (var i=0; i<classes.length; ++i) {
				if (classes[i]=="Active") {
					classes.splice(i, 1);
					break;
				}
			}
			active.className = classes.join(" ");
		}
		var active = thumbnails.getElementsByTagName("li")[num];
		if (active) active.className += " Active";
	}
	var progress = document.getElementById("progress");
	progress.style.display = "block";
	var dims = redrocketcms.utilities.getBrowserDimensions();
	var height = Math.max(dims[1], data.site_minimum_height);
	var img_width = thumbnails ? dims[0] : (dims[0]/3)*2;
	var img_height = height - data.top_bar_height - data.bottom_bar_height;
	var image = document.getElementById("image");
	var imgSrc = photo_objects[index];
	imgSrc = imgSrc.split("../").join("publish/");
	image.innerHTML = '<img width="' + Math.floor(img_width) + '" height="' + Math.floor(img_height) + '" onload="hideLoader();" src="' + rootpath + 'assets/php/resample.php?e=1&u=' + imgSrc + '&w=' + Math.floor(img_width) + '&h=' + Math.floor(img_height) + '" />';
}

function trim(str) {
	for(var i = 0; str.charCodeAt(i) < 33; i++);
	for(var j = str.length-1; str.charCodeAt(j) < 33; j--);
	return str.substring(i, j+1);
}

function parseData (str) {
	data = new Object();
	var lines = str.split("\n");
	for (var i=0; i<lines.length; ++i) {
		var line = lines[i];
		var bits = line.split(":");
		var name = bits.shift();
		var value = trim(bits.join(":"));
		data[name] = value;
		var bits = name.split("_");
		var type = bits.pop();
		if (type=="width"||type=="height"||type=="stroke") {
			value = parseFloat(value.split("px").join(""));
		} else if (type=="opacity") {
			value = parseFloat(value.split("%").join(""));
		} else if (type=="ratio") {
			value = value.split("x");
			value[0] = parseFloat(value[0]);
			value[0] = parseFloat(value[0]);
		}
		data[name] = value;
	}
}

function getElementsByClassName(className, tag, elm){
	var testClass = new RegExp("(^|\\s)" + className + "(\\s|$)");
	var tag = tag || "*";
	var elm = elm || document;
	var elements = (tag == "*" && elm.all)? elm.all : elm.getElementsByTagName(tag);
	var returnElements = [];
	var current;
	var length = elements.length;
	for(var i=0; i<length; i++){
		current = elements[i];
		if(testClass.test(current.className)){
			returnElements.push(current);
		}
	}
	return returnElements;
}

function getCursorCoord (e) {
	var posx = 0;
	var posy = 0;
	if (!e) var e = window.event;
	if (e.pageX || e.pageY) 	{
		posx = e.pageX;
		posy = e.pageY;
	}
	else if (e.clientX || e.clientY) 	{
		posx = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
		posy = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
	}
	return [posx, posy];
}

function getElementCoord (obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);
	}
	return [curleft,curtop];
}
