//
// Copyright Michael Markie 2009
// 

function Effects() {}
function SetStyles() {}
function Lightbox() {}
var lightbox = "mquery_lightbox";

function mQuery(){}

var IE6 = navigator.userAgent.toLowerCase().indexOf('msie 6') != -1;
var FF = navigator.userAgent.toLowerCase().indexOf('firefox') != -1;

function $(id){
	return document.getElementById(id);
}
function ComputedStyle(id,value){
	if($(id).currentStyle){
		return $(id).currentStyle[value];
	}else if(window.getComputedStyle){
		return document.defaultView.getComputedStyle($(id), "").getPropertyValue(value);
	}
}
/*function show(id){
	$(id).style.display="block";
}
function hide(id){
	$(id).style.display="none";
}*/
/*Snippet from prototype*/
if (!document.getElementsByClassName) {
	document.getElementsByClassName = function(className) {
		var children = document.getElementsByTagName('*') || document.all;
		var elements = new Array();
	  
		for (var i = 0; i < children.length; i++) {
			var child = children[i];
			var classNames = child.className.split(' ');
			for (var j = 0; j < classNames.length; j++) {
				if (classNames[j] == className) {
					elements.push(child);
					break;
				}
			}
		}
		return elements;
	}
}

SetStyles.prototype.setopacity = function(id,level,test){
	if(test == 1){
		$(id).style.opacity=(level/100);
		$(id).style.filter="alpha(opacity: "+(level)+")";
	}else{
		$(id).style.opacity=level;
		$(id).style.filter="alpha(opacity: "+(level*100)+")";
	}
};
SetStyles.prototype.bgColor = function(id,color){
	if(color != ""){
		$(id).style.backgroundColor=color;
	}
};
Effects.prototype.fadein = function(id,duration,limit){
	SetStyle.setopacity(id,0);
	var i=0;
	while(i<limit){
		setTimeout("SetStyle.setopacity('"+id+"','"+i+"')",i * duration);
		i+=(1 / 16);
	}
};
Effects.prototype.FadeIn = function(id,duration,limit){
	SetStyle.setopacity(id,0);
	var i=0;
	while(i<limit){
		setTimeout("SetStyle.setopacity('"+id+"','"+i+"')",i * duration);
		i+=(1 / 16);
	}
};
Effects.prototype.FadeOut = function(id,duration){
	var opacity=$(id).style.opacity;
	var i=0;
	var t="";
	var step = 0.05;
	if(opacity >= 0.00){
		$(id).style.opacity=opacity - step;
		setTimeout("Effect.FadeOut('"+id+"','"+duration+"')",i * duration);
	}
};
Effects.prototype.fade = function(id,time1,time2){
	fadeout(id,time1,1);
	setTimeout("FadeIn("+id+","+time2+",1)",time1+10);
};
Effects.prototype.bgchanger = function(id,r,g,b){

	var color=$(id).style.backgroundColor.replace('rgb(','').replace(')','').split(',');
	var i=1;
	while(i<=255){
		if(color[0] < r && color[0] != r){ color[0]++; }else{ color[0]--; }
		if(color[1] < g && color[1] != g){ color[1]++; }else{ color[1]--; }
		if(color[2] < b && color[2] != b){ color[2]++; }else{ color[2]--; }
		setTimeout("SetStyle.bgColor('"+id+"','rgb("+color[0]+","+color[1]+","+color[2]+")')",i*5);
		i=i+1;
	}
};
Effects.prototype.SlideClose = function(id,duration){
	var step = 1;
	height1 = parseInt(ComputedStyle(id,'height').replace('px',''));
	if(height1 >= 0){
		$(id).style.height=height1 - step + "px";
		setTimeout("Effect.SlideClose('"+id+"','"+duration+"')",duration);
	}
};
Effects.prototype.SlideOpen = function(id,duration){
	var step=1, newheight=0, i=0;
	
	$(id).style.height="auto";
	height1 = parseInt(ComputedStyle(id,'height').replace('px',''));
	$(id).style.height="0px";
	alert(height1);
	while(i <= height1){
		t=setTimeout("$('"+id+"').style.height='"+i+"px'",i*duration);
		i=i+step;
		if(i>=height1){
			alert('cleartimeout');
			clearTimeout(t);
		}
	}
};


Effects.prototype.SlideIn = function(id,to,delay){
	var boxheight = parseInt(ComputedStyle(id,'height'));
	$(id).style.marginTop='-'+boxheight+'px';
	var from = parseInt(ComputedStyle(id,'marginTop').replace('px',''));
	var i=0;
	while(from <= to){
		setTimeout("$("+id+").style.marginTop="+from+"px",i * delay);
		from++;
		i++;
	}
};

mQuery.prototype.DropDown = function(array){
	var amount = array['amount'];
	var classes = document.getElementsByClassName('drop');
	for (i=0;i<classes.length;i++) {
		classes[i].style.cursor="pointer";
		classes[i].onmouseover = function(){
		}
	}
};



Lightbox.prototype.Open = function(array){
	if($(lightbox).style.display != "block"){
		Lightbox.IE6(array);
		Lightbox.Location(array);
		show(lightbox);
		if(array['fade'] == true){
			Effect.FadeIn(lightbox,array['fadein_speed'],array['fadein_opacity']);
		}else if(array['fade'] == false && array['opacity'] != ""){
			SetStyle.setopacity(lightbox,array['opacity']);
		}
		show(array['content_box']);
	}
};
Lightbox.prototype.Close = function(array){
	if(array['fade'] == true){
		Effect.FadeOut(lightbox,array['fadeout_speed'],array['fadeout_opacity']);
		setTimeout("hide('mquery_lightbox')",array['fadeout_speed']);
	}else if(array['fade'] == false){
		hide(lightbox);
	}
	hide(array['content_box']);
	Lightbox.IE6(array);
};
Lightbox.prototype.Location = function(array){
	if(array['center_box_vertical'] == true || array['center_box_center'] == true){
		if(array['center_box_vertical'] == true){
			var height = ComputedStyle(array['content_box'],'height').replace('px','');
			$(array['content_box']).style.top="50%";
			
			if(IE6 == true){
				var sum = document.documentElement.scrollTop-height/2;
			}else{
				var sum = document.body.scrollTop-height/2;
			}
			$(array['content_box']).style.marginTop=sum+'px';
			if($(array['content_box']).style.position != ""){
				$(array['content_box']).style.position='absolute';
			}
		}
		if(array['center_box_center'] == true){
			var width = ComputedStyle(array['content_box'],'width').replace('px','');
			$(array['content_box']).style.left="50%";
			$(array['content_box']).style.marginLeft='-'+width/2+'px';
			if($(array['content_box']).style.position != ""){
				$(array['content_box']).style.position='absolute';
			}
		}
		if(array['offset_left'] != ""){
			$(array['content_box']).style.marginLeft=parseInt($(array['content_box']).style.marginLeft.replace('px','')) - array['offset_left'];
		}
		if(array['offset_top'] != ""){
			$(array['content_box']).style.marginTop=parseInt($(array['content_box']).style.marginTop.replace('px','')) - array['offset_top'];
		}
	}
};
Lightbox.prototype.Configure = function(array){
	if(typeof array['content_box'] == "undefined"){
		array['content_box'] = "mQuery_content_box";
	}
	var body = document.getElementsByTagName('body')[0];
	var lbox = '<div id="'+array['content_box']+'"></div><div id="mquery_lightbox" onclick="Lightbox.Close(lboxconf)"></div>';
	body.innerHTML=lbox + body.innerHTML;
	$('mquery_lightbox').style.height=body.scrollHeight+'px';
	if(FF == true){
		$('mquery_lightbox').style.position="fixed";
	}
	
	if($(array['close_button_id'])){
		$(array['close_button_id']).style.cursor="pointer";
		$(array['close_button_id']).onclick = function(){
			Lightbox.Close(lboxconf);
		}
	}
	var classes = document.getElementsByClassName('lightbox_open');
	for (i=0;i<classes.length;i++) {
		classes[i].style.cursor="pointer";
		classes[i].onclick = function(){
			Lightbox.Open(lboxconf);
		}
	}
};
Lightbox.prototype.IE6 = function(array){
	if(IE6 == true){
		if(typeof array['hide_objects'] != "undefined"){
			var splitted = array['hide_objects'].split(',');
			for(s=0;s< splitted.length;s++){
				if(typeof document.getElementsByTagName(splitted[s])[0] != "undefined"){
					var object = document.getElementsByTagName(splitted[s]);
					for(i=0;i< object.length;i++){
						if(object[i].style.visibility == "hidden"){
							object[i].style.visibility="visible";
						}else{
							object[i].style.visibility="hidden";
						}
					}
				}
			}
		}
	}
};

var Effect = new Effects();
var SetStyle = new SetStyles();
var Lightbox = new Lightbox();
var mQuery = new mQuery();