﻿/* Tools Function Write by Sander 2007-04-20
Dependendies:
<script type="text/javascript" src="/js/ui/yahoo/yahoo-min.js"></script>
<script type="text/javascript" src="/js/ui/event/event-min.js"></script>
<script type="text/javascript" src="/js/ui/dom/dom-min.js"></script>
<script type="text/javascript" src="/js/ui/connection/connection-min.js"></script>
*/
////////////////////////////////
var _photoControl;
var _loading = false;
var _groupId;
var _target;
var __tags = [];
var __count = 0;
var __maxCount = 60;
/////////////////////////////////
window.onerror = function(e){
	return true;
};

function getObject(elementId) {
	if (document.getElementById) {
		return document.getElementById(elementId);
	} else if (document.all) {
		return document.all[elementId];
	} else if (document.layers) {
		return document.layers[elementId];
	} 
};

function isNull(value){
	try{
		if(typeof value == "undefined" || value == null){
			return true;
	   }
	}catch(e){
		return true;
	}
	return false;
};

function urlDecode(str)
{
	if("undefined" == typeof decodeURIComponent)
	{
		return unescape(str).replace(/\+/g, ' ').replace(/%2B/g,'+');
  	} else {
  		return unescape(str).replace(/\+/g, ' ').replace(/%2B/g,'+');
  }
};

function createDelegate(oThis, pFunc){
	return function() {return pFunc.apply(oThis, arguments);};
};

var Callback = {
  success:Success,
  failure:Failure
};

function Success(o){
	_loading = false;
	var s = o.responseText;
	if(s.indexOf('|')>0){
	    var p = s.split("|");
	    for(var i = 0; i < p.length; i++){
	       eval(p[i] + "\(" + p[++i] + "\)");
	    }
	}
}

function Failure(o){
	_loading = false;
};

function request(action, groupId, template){
	if(isNull(template))
		template = "";
	_loading = true;
	var postData = "";
	var url = "/PhotoHandler.aspx?action=" + action + "&groupid=" + groupId + "&template=" + template + "&time=" + (new Date()).getTime();
	
	var request = YAHOO.util.Connect.asyncRequest('GET', url, Callback, postData);
};

function getGroupId(s){
	var r = new RegExp("_([0-9]+)_", "i");
	r.exec(s);
	return RegExp.$1;
};
///////////////////////////////////////////////////////////--

start("camptonImageGroup");

function start(cssClass){
	var list = YAHOO.util.Dom.getElementsByClassName(cssClass, "div");
	
	for(var i = 0; i < list.length; i++){
		var s = list[i].id;
		YAHOO.util.Dom.setStyle(list[i], 'background', '');
		_groupId = getGroupId(s);
		__tags.push({groupId:_groupId, obj:list[i]});
		
		_loading = false;
		beginRequest(i);
	}
}

function getTag(groupId){
	if(groupId >= 0){
		for(var i = 0; i < __tags.length; i++){
			if(__tags[i].groupId == groupId){
				return __tags[i].obj;
			}
		}
	}
	return null;
};

function beginRequest(index){
	if(index >= __tags.length)
		return;
	var s = __tags[index].obj.id;
	if(_loading == false){
		_groupid = getGroupId(s);
		request('get', _groupid, '');
	}
};

function photoItemClick() {
	var photoContent = YAHOO.util.Dom.getElementsByClassName("photocontent", "div", this.container)[0];
	var o = document.createElement("img");
	var url = this.img.src;
	var bigUrl = url.toLowerCase().replace("small/", "");
	o.src = bigUrl;
	var width = YAHOO.util.Dom.getStyle(photoContent, 'width');
	var height = YAHOO.util.Dom.getStyle(photoContent, 'height');
	if(!isNull(width) && width.indexOf("px") > 0){
		width = width.replace("px", "") - 6;
	}else{
		width = 500;
	}
	if(!isNull(height) && height.indexOf("px") > 0){
		height = height.replace("px", "") - 6;
	}else{
		height = 342;
	}
	var w = setImageSize(width, height, o.width, o.height);
	
	photoContent.innerHTML = "<img src=\"" + bigUrl + "\" style=\"width: " + w.width + "px; height: " + w.height + "px;\" />";
};

function initPhotoContentImages(groupId){
    __count++;
	var photoControl = getObject("photoControl" + groupId);
	var photoContent = YAHOO.util.Dom.getElementsByClassName("photocontent", "div", photoControl)[0];
	var img = photoContent.getElementsByTagName("img")[0];
	var url = img.src;
	var bigUrl = url.toLowerCase().replace("small/", "");
	img.src = bigUrl;
	if(!isNull(img)){
		var oNewImg = document.createElement("img");
		oNewImg.src = img.src;
		if(oNewImg.complete){
			var width = YAHOO.util.Dom.getStyle(photoContent, 'width');
			var height = YAHOO.util.Dom.getStyle(photoContent, 'height');
			if(!isNull(width) && width.indexOf("px") > 0){
				width = width.replace("px", "") - 6;
			}else{
				width = 500;
			}
			if(!isNull(height) && height.indexOf("px") > 0){
				height = height.replace("px", "") - 6;
			}else{
				height = 342;
			}
			var w = setImageSize(width, height, oNewImg.width, oNewImg.height);
			YAHOO.util.Dom.setStyle(img, 'width', w.width);
			YAHOO.util.Dom.setStyle(img, 'height', w.height);
		}else{
			if(__count < __maxCount) setTimeout("initPhotoContentImages(" + groupId + ")", 50);
		}
	}
};

function getPhotoContainers(photoControl){
	return YAHOO.util.Dom.getElementsByClassName('photoItemContainer', 'div', photoControl);
};

function getPhotoItemImages(photoControl){
	var elements = getPhotoContainers(photoControl);
	var list = [];
	for(var i = 0; i < elements.length; i++){
		var items = elements[i].getElementsByTagName('img');
		for(var i = 0; i < items.length; i++){
			list.push(items[i]);
		}
	}
	return list;
};

function addClickEvent2PhotoItems(photoControl, imgs){
	if(isNull(imgs)) {return;}
	for(var i = 0; i < imgs.length; i++){
		var _obj = {container: photoControl, img:imgs[i]};
		YAHOO.util.Event.addListener(imgs[i], "click", createDelegate(_obj, photoItemClick));
	}
};

function addBlackItem(element, n, length){
	var left = n - length;
	for(var j = 0; j < left; j++){
		var oNewNode = document.createElement("div");
		oNewNode.className = "photoItem";
		element.appendChild(oNewNode);
	}
}

var __complete=false;

function initPhotoItemImages(groupId, n){
    __count++;
	var photoControl = getObject("photoControl" + groupId);
	var element = getPhotoContainers(photoControl)[0];
	if(isNull(element)) return;
	var items = YAHOO.util.Dom.getElementsByClassName('photoItem', 'div', element);
	addBlackItem(element, n, items.length);
};

function setImageSize(x, y, w, h){
	if(w < x && h < y){
		return {width:w, height:h};
	}else{
		if(x/w < y/h){
			return {width:x, height:h*x/w};
		}else{
			return {width:w*y/h, height:y};
		}
	}
};

function init(obj){
	var d = urlDecode(obj.content);
	var target = getTag(obj.groupId);
	target.innerHTML = d;
	var _photoControl = getObject("photoControl" + obj.groupId);
	var width = YAHOO.util.Dom.getStyle(_photoControl, 'width');
	var height = YAHOO.util.Dom.getStyle(_photoControl, 'height');
	height = parseInt(height.replace('px','')) + 18 + 'px';
	YAHOO.util.Dom.setStyle(target, 'width', width);
	YAHOO.util.Dom.setStyle(target, 'height', height);
	var imgs = getPhotoItemImages(_photoControl);
	addClickEvent2PhotoItems(_photoControl, imgs);
	initPhotoContentImages(obj.groupId);
	initPhotoItemImages(obj.groupId, 10);
	
};

function initTemplate(obj){
    __count++;
	_photoControl = getObject("photoControl" + obj.groupId);
	var photoTemplate = YAHOO.util.Dom.getElementsByClassName("photoTemplate", "div", _photoControl)[0];
	if(isNull(photoTemplate)){
		if(__count < __maxCount) setTimeout("initTemplate(obj)", 100);
	}else{
		var d = urlDecode(obj.content);
		photoTemplate.innerHTML = d;
	}
};

function reloadTemplate(s, groupId){
	var _target = getTag(groupId);
	_target.style.background = "";
	_target.className="loading";
	_target.innerHTML = "";
	if(_loading == false){
		request('get', groupId, s);
	}
};
///////////////////////////////////////////////////////////--
//parentNode