var cn = new Object();
window.requests = new Object();

cn.util = 
{
	request  : false,
	activeXML : false,
	
	addEvent : function( obj, type, fn )
	{
        if (obj.addEventListener)
			obj.addEventListener( type, fn, false );
        else if (obj.attachEvent)
        {
			obj["e"+type+fn] = fn;
			obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
			obj.attachEvent( "on"+type, obj[type+fn] );
        }
	},	
	
	makeRequest : function(url, id, callback, postData)
	{
		window.requests["request"+id] = this.createXMLHTTPObject();
		
		if (!window.requests["request"+id])
			return;
		
		var method = (postData) ? "POST" : "GET";
		
		window.requests["request"+id].open(method,url,true);
		window.requests["request"+id].setRequestHeader('User-Agent','XMLHTTP/1.0');
		
		if (postData)
			window.requests["request"+id].setRequestHeader('Content-type','application/x-www-form-urlencoded');
		
		window.requests["request"+id].onreadystatechange = function () {
			if (window.requests["request"+id].readyState != 4)
				return;
			
			if (window.requests["request"+id].status != 200 && window.requests["request"+id].status != 304)
				return;
			
			callback(window.requests["request"+id]);
		}
		
		if (window.requests["request"+id].readyState == 4)
			return;
		
		window.requests["request"+id].send(postData);
	},
	
	XMLHttpFactories : [
		function () {return new XMLHttpRequest()},
		function () {return new ActiveXObject("Msxml2.XMLHTTP")},
		function () {return new ActiveXObject("Msxml3.XMLHTTP")},
		function () {return new ActiveXObject("Microsoft.XMLHTTP")}
	],
	
	createXMLHTTPObject : function() {
		var xmlhttp = false;
		for (var i=0;i<this.XMLHttpFactories.length;i++) {
			try {
				xmlhttp = this.XMLHttpFactories[i]();			
			}catch (e) {
				continue;
			}
			break;
		}
		return xmlhttp;
	},
	
	handleError : function(message)
	{
		throw new Error(message);
	}
}

cn.browserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};

cn.dhtml = 
{
	createDomObject : function(tag,id,position,zindex,top,left,background,border,width,height,opacity)
	{
			var box = document.createElement(tag)
			box.id = id;
			
			if(position != 0)
				box.style.position = position;
			
			if(zindex != 0)
				box.style.zIndex = zindex;
			
			box.style.top = top + "px";
			box.style.left = left + "px";
			box.style.background = background;
			box.style.border = border;
			
			if(width != 0)
				box.style.width = width+"px";
			
			if(height != 0)
				box.style.height = height+"px";
			
			if(!document.all){
				box.style.opacity = opacity;
				box.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity="+(opacity*100)+")";
			}
			
			return box;
	},
	
	casesImg : function()
	{
	    if($('casesUl'))
    	    var elm = $('casesUl').getElementsByTagName('li');
	    
	    if(elm)
	    {
	        for(x = 1; x < (elm.length + 1); x++)
	        {
	            if(x % 3 == 0)
	                elm[(x -1)].style.marginRight = '0px';
	        }
	    }
	}
}
cn.slideShow =
{	
	showFirstPicture : function()
	{
	    if($('slideShowImage'))
    	    Effect.Appear('slideShowImage', {delay: 2.0 });
	},
	
	changePicture : function(img, btn)
	{
	    cn.slideShow.nxtImg = img;
	    
	    var controls = $('contentCase').getElementsByClassName('controls')[0].getElementsByTagName('IMG');
	    var clickedControl = $(btn).getElementsByTagName('IMG')[0];
	    
	    
	    for(var x in controls)
	    {
	        if(controls[x] == clickedControl)
	            controls[x].src = "/images/slideshow.controls.active.png";
	        else
	            controls[x].src = "/images/slideshow.controls.inactive.png";
	    }
	    if(cn.browserDetect.browser == 'Explorer' && cn.browserDetect.version < 7)
			$('slideShowImage').src = cn.slideShow.nxtImg;
		else
			Effect.Fade('slideShowImage', {afterFinish: cn.slideShow.makeChange});
	},
	
	makeChange : function()
	{
	    $('slideShowImage').src = cn.slideShow.nxtImg;
	    Effect.Appear('slideShowImage');
	}
}

var slideShowEngine = Class.create({
    zIndex: 0,
	initialize: function(target, triggerActiveClass, imageContainer, buttonContainer){
		this.t = target;
		this.tac = triggerActiveClass;
		this.ic = imageContainer;
		this.bc = buttonContainer;
		
		this.engine();
	},
	
	engine: function(){
		this.ico = this.getImageCollection();
		this.c = this.ico.length;
		this.n = 0;
		
		var _this = this;
		
		new Effect.Appear(this.t, {afterFinish: function(){
		    $(_this.t).style.filter = "alpha(opacity=100)";
		}});
		
		this.bootUp();
	},
	
	getImageCollection: function(){
		var _r = [];
		var htmlImageElements = $$('#'+this.ic+' img');
		htmlImageElements.each(function(imageElement){
			_r.push(imageElement.src);
		});
		return _r;
	},
	
	bootUp: function(){
	    var _this = this;
		this.i = setInterval(function(){
		    _this.nextImage();
		},5000);
	},
	
	nextImage: function(){
	    this.n++;
	    if(this.n == this.c) this.n = 0;
	    
	    this.changeActiveBtn(this.n);
	    
		this.createImageElement(this.ico[this.n]);
	},
	
	changeImage : function(index){
	    clearInterval(this.i);
	    this.changeActiveBtn(index);
	    this.createImageElement(this.ico[index]);
	},
	
	createImageElement : function(imageSrc){
	    this.zIndex++;
	    var img = Builder.node('img', {src: imageSrc, style: 'filter:alpha(opacity=0);opacity:0;left:0;top:0;position:absolute;z-index:'+this.zIndex+';' } );
	    $(this.t).parentNode.appendChild(img);
	    new Effect.Appear(img);
	},
	
	changeActiveBtn : function(btnIndex){
	    var _this = this;
	    $$('#'+this.bc+' img').each(function(item, index){
	        if(index == btnIndex){
	            item.src = "/images/slideshow.controls.active.png";
	        }else{
	            item.src = "/images/slideshow.controls.inactive.png";
	        }
	    });
	}
});

Event.observe(window, 'load', function(){
    if($('slideShowHolder'))
        window.slide = new slideShowEngine('slideShowImage', 'active', 'imageContainer', 'controls');
})




/** add events **/

cn.util.addEvent(window,'load', cn.dhtml.casesImg);
cn.browserDetect.init();
/*
if(cn.browserDetect.browser != 'Explorer' || (cn.browserDetect.browser == 'Explorer' && cn.browserDetect.version >= 7))
	cn.util.addEvent(window,'load', cn.slideShow.showFirstPicture);
*/

(function () {
    var ext = {
        init : function () {
           cn.util.addEvent(window, "load", ext.add);
        },
        add : function () {
            var a = document.getElementsByTagName("a"), i, re = new RegExp("(^|\\s)external(\\s|$)");
            for (i = 0; i < a.length; i += 1) {
                if (a[i].rel && (re).test(a[i].rel)) {
                    Event.observe(a[i], "click", ext.open);
                }
            }
        },
        open : function (evt) {
            window.open(this);
            Event.stop(evt)
        }
    };
    ext.init();
})();
