//** Animated Collapsible DIV v2.0- (c) Dynamic Drive DHTML code library: http://www.dynamicdrive.com.

var animatedcollapse={
divholders: {}, //structure: {div.id, div.attrs, div.$divref, div.$togglerimage}
divgroups: {}, //structure: {groupname.count, groupname.lastactivedivid}
lastactiveingroup: {}, //structure: {lastactivediv.id}
preloadimages: [],

show:function(divids){ //public method
	if (typeof divids=="object"){
		for (var i=0; i<divids.length; i++)
			this.showhide(divids[i], "show")
	}
	else
		this.showhide(divids, "show")
},

hide:function(divids){ //public method
	if (typeof divids=="object"){
		for (var i=0; i<divids.length; i++)
			this.showhide(divids[i], "hide")
	}
	else
		this.showhide(divids, "hide")
},

toggle:function(divid){ //public method
	if (typeof divid=="object")
		divid=divid[0]
	this.showhide(divid, "toggle")
},

addDiv:function(divid, attrstring){ //public function
	this.divholders[divid]=({id: divid, $divref: null, attrs: attrstring})
	this.divholders[divid].getAttr=function(name){ //assign getAttr() function to each divholder object
		var attr=new RegExp(name+"=([^,]+)", "i") //get name/value config pair (ie: width=400px,)
		return (attr.test(this.attrs) && parseInt(RegExp.$1)!=0)? RegExp.$1 : null //return value portion (string), or 0 (false) if none found
	}
	this.currentid=divid //keep track of current div object being manipulated (in the event of chaining)
	return this
},

showhide:function(divid, action){
	var $divref=this.divholders[divid].$divref //reference collapsible DIV
	if (this.divholders[divid] && $divref.length==1){ //if DIV exists
		var targetgroup=this.divgroups[$divref.attr('groupname')] //find out which group DIV belongs to (if any)
		if ($divref.attr('groupname') && targetgroup.count>1 && (action=="show" || action=="toggle" && $divref.css('display')=='none')){ //If current DIV belongs to a group
			if (targetgroup.lastactivedivid && targetgroup.lastactivedivid!=divid) //if last active DIV is set
				this.slideengine(targetgroup.lastactivedivid, 'hide') //hide last active DIV within group first
				this.slideengine(divid, 'show')
			targetgroup.lastactivedivid=divid //remember last active DIV
		}
		else{
			this.slideengine(divid, action)
		}
	}
},

slideengine:function(divid, action){
	var $divref=this.divholders[divid].$divref
	var $togglerimage=this.divholders[divid].$togglerimage
	if (this.divholders[divid] && $divref.length==1){ //if this DIV exists
		var animateSetting={height: action}
		if ($divref.attr('fade'))
			animateSetting.opacity=action
		$divref.animate(animateSetting, $divref.attr('speed')? parseInt($divref.attr('speed')) : 900, function(){
			if ($togglerimage){
				$togglerimage.attr('src', ($divref.css('display')=="none")? $togglerimage.data('srcs').closed : $togglerimage.data('srcs').open)
			}
			if (animatedcollapse.ontoggle){
				try{
					animatedcollapse.ontoggle(jQuery, $divref.get(0), $divref.css('display'))
				}
				catch(e){
					alert("An error exists inside your \"ontoggle\" function:\n\n"+e+"\n\nAborting execution of function.")
				}
			}
		})
		return false
	}
},

generatemap:function(){
	var map={}
	for (var i=0; i<arguments.length; i++){
		if (arguments[i][1]!=null){ //do not generate name/value pair if value is null
			map[arguments[i][0]]=arguments[i][1]
		}
	}
	return map
},

init:function(){
	var ac=this
	jQuery(document).ready(function($){
		animatedcollapse.ontoggle=animatedcollapse.ontoggle || null
		var urlparamopenids=animatedcollapse.urlparamselect() //Get div ids that should be expanded based on the url (['div1','div2',etc])
		var persistopenids=ac.getCookie('acopendivids') //Get list of div ids that should be expanded due to persistence ('div1,div2,etc')
		var groupswithpersist=ac.getCookie('acgroupswithpersist') //Get list of group names that have 1 or more divs with "persist" attribute defined
		if (persistopenids!=null) //if cookie isn't null (is null if first time page loads, and cookie hasnt been set yet)
			persistopenids=(persistopenids=='nada')? [] : persistopenids.split(',') //if no divs are persisted, set to empty array, else, array of div ids
		groupswithpersist=(groupswithpersist==null || groupswithpersist=='nada')? [] : groupswithpersist.split(',') //Get list of groups with divs that are persisted
		jQuery.each(ac.divholders, function(){ //loop through each collapsible DIV object
			this.$divref=$('#'+this.id)
			if ((this.getAttr('persist') || jQuery.inArray(this.getAttr('group'), groupswithpersist)!=-1) && persistopenids!=null){ //if this div carries a user "persist" setting, or belong to a group with at least one div that does
				var cssdisplay=(jQuery.inArray(this.id, persistopenids)!=-1)? 'block' : 'none'
			}
			else{
				var cssdisplay=this.getAttr('hide')? 'none' : null
			}
			if (urlparamopenids[0]=="all" || jQuery.inArray(this.id, urlparamopenids)!=-1){ //if url parameter string contains the single array element "all", or this div's ID
				cssdisplay='block' //set div to "block", overriding any other setting
			}
			else if (urlparamopenids[0]=="none"){
				cssdisplay='none' //set div to "none", overriding any other setting
			}
			this.$divref.css(ac.generatemap(['height', this.getAttr('height')], ['display', cssdisplay]))
			this.$divref.attr(ac.generatemap(['groupname', this.getAttr('group')], ['fade', this.getAttr('fade')], ['speed', this.getAttr('speed')]))
			if (this.getAttr('group')){ //if this DIV has the "group" attr defined
				var targetgroup=ac.divgroups[this.getAttr('group')] || (ac.divgroups[this.getAttr('group')]={}) //Get settings for this group, or if it no settings exist yet, create blank object to store them in
				targetgroup.count=(targetgroup.count||0)+1 //count # of DIVs within this group
				if (jQuery.inArray(this.id, urlparamopenids)!=-1){ //if url parameter string contains this div's ID
					targetgroup.lastactivedivid=this.id //remember this DIV as the last "active" DIV (this DIV will be expanded). Overrides other settings
					targetgroup.overridepersist=1 //Indicate to override persisted div that would have been expanded
				}
				if (!targetgroup.lastactivedivid && this.$divref.css('display')!='none' || cssdisplay=="block" && typeof targetgroup.overridepersist=="undefined") //if this DIV was open by default or should be open due to persistence								
					targetgroup.lastactivedivid=this.id //remember this DIV as the last "active" DIV (this DIV will be expanded)
				this.$divref.css({display:'none'}) //hide any DIV that's part of said group for now
			}
		}) //end divholders.each
		jQuery.each(ac.divgroups, function(){ //loop through each group
			if (this.lastactivedivid && urlparamopenids[0]!="none") //show last "active" DIV within each group (one that should be expanded), unless url param="none"
				ac.divholders[this.lastactivedivid].$divref.show()
		})
		if (animatedcollapse.ontoggle){
			jQuery.each(ac.divholders, function(){ //loop through each collapsible DIV object and fire ontoggle event
				animatedcollapse.ontoggle(jQuery, this.$divref.get(0), this.$divref.css('display'))
			})
		}
 		//Parse page for links containing rel attribute
		var $allcontrols=$('a[rel]').filter('[rel^="collapse["], [rel^="expand["], [rel^="toggle["]') //get all elements on page with rel="collapse[]", "expand[]" and "toggle[]"
		$allcontrols.each(function(){ //loop though each control link
			this._divids=this.getAttribute('rel').replace(/(^\w+)|(\s+)/g, "").replace(/[\[\]']/g, "") //cache value 'div1,div2,etc' within identifier[div1,div2,etc]
			if (this.getElementsByTagName('img').length==1 && ac.divholders[this._divids]){ //if control is an image link that toggles a single DIV (must be one to one to update status image)
				animatedcollapse.preloadimage(this.getAttribute('data-openimage'), this.getAttribute('data-closedimage')) //preload control images (if defined)
				$togglerimage=$(this).find('img').eq(0).data('srcs', {open:this.getAttribute('data-openimage'), closed:this.getAttribute('data-closedimage')}) //remember open and closed images' paths
				ac.divholders[this._divids].$togglerimage=$(this).find('img').eq(0) //save reference to toggler image (to be updated inside slideengine()
				ac.divholders[this._divids].$togglerimage.attr('src', (ac.divholders[this._divids].$divref.css('display')=="none")? $togglerimage.data('srcs').closed : $togglerimage.data('srcs').open)
			}
			$(this).click(function(){ //assign click behavior to each control link
				var relattr=this.getAttribute('rel')
				var divids=(this._divids=="")? [] : this._divids.split(',') //convert 'div1,div2,etc' to array 
				if (divids.length>0){
					animatedcollapse[/expand/i.test(relattr)? 'show' : /collapse/i.test(relattr)? 'hide' : 'toggle'](divids) //call corresponding public function
					return false
				}
			}) //end control.click
		})// end control.each

		$(window).bind('unload', function(){
			ac.uninit()
		})
	}) //end doc.ready()
},

uninit:function(){
	var opendivids='', groupswithpersist=''
	jQuery.each(this.divholders, function(){
		if (this.$divref.css('display')!='none'){
			opendivids+=this.id+',' //store ids of DIVs that are expanded when page unloads: 'div1,div2,etc'
		}
		if (this.getAttr('group') && this.getAttr('persist'))
			groupswithpersist+=this.getAttr('group')+',' //store groups with which at least one DIV has persistance enabled: 'group1,group2,etc'
	})
	opendivids=(opendivids=='')? 'nada' : opendivids.replace(/,$/, '')
	groupswithpersist=(groupswithpersist=='')? 'nada' : groupswithpersist.replace(/,$/, '')
	this.setCookie('acopendivids', opendivids)
	this.setCookie('acgroupswithpersist', groupswithpersist)
},

getCookie:function(Name){ 
	var re=new RegExp(Name+"=[^;]*", "i"); //construct RE to search for target name/value pair
	if (document.cookie.match(re)) //if cookie found
		return document.cookie.match(re)[0].split("=")[1] //return its value
	return null
},

setCookie:function(name, value, days){
	if (typeof days!="undefined"){ //if set persistent cookie
		var expireDate = new Date()
		expireDate.setDate(expireDate.getDate()+days)
		document.cookie = name+"="+value+"; path=/; expires="+expireDate.toGMTString()
	}
	else //else if this is a session only cookie
		document.cookie = name+"="+value+"; path=/"
},

urlparamselect:function(){
	window.location.search.match(/expanddiv=([\w\-_,]+)/i) //search for expanddiv=divid or divid1,divid2,etc
	return (RegExp.$1!="")? RegExp.$1.split(",") : []
},

preloadimage:function(){
	var preloadimages=this.preloadimages
	for (var i=0; i<arguments.length; i++){
		if (arguments[i] && arguments[i].length>0){
			preloadimages[preloadimages.length]=new Image()
			preloadimages[preloadimages.length-1].src=arguments[i]
		}
	}
}

}

//animateddivs

animatedcollapse.addDiv('aboutus', 'fade=1,height=auto,group=lcol')
animatedcollapse.addDiv('news', 'fade=1,height=auto,group=mcol')
animatedcollapse.addDiv('portfolio', 'fade=1,height=auto,group=rcol')
animatedcollapse.addDiv('contact', 'fade=1,height=auto,group=lcol,hide=1')
animatedcollapse.addDiv('welcome', 'fade=1,height=auto,group=mcol')
animatedcollapse.addDiv('client', 'fade=1,height=auto,group=rcol')

animatedcollapse.ontoggle=function($, divobj, state){ //fires each time a DIV is expanded/contracted
	//$: Access to jQuery
	//divobj: DOM reference to DIV being expanded/ collapsed. Use "divobj.id" to get its ID
	//state: "block" or "none", depending on state
}

animatedcollapse.init()

//image swaps

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

var gArgs = new Array(),NNt;
function NN_td_start(obj,c1,c2,t1,t2,cF,sF,s){
	var isNet = navigator.appName == 'Netscape' ? true : false;
	var isFF = navigator.userAgent.indexOf('Firefox') != -1 ? true : false;
	var bVer = parseInt(navigator.appVersion);
	gArgs[0]=obj;gArgs[1]=c1;gArgs[2]=c2;gArgs[3]=t1;gArgs[4]=t2;gArgs[5]=s;
	if (isNet){if (isFF && bVer >= 5){NN_td_rollover();
	}else{obj.style.backgroundColor=c1;obj.style.color=c2;}
	}else{NN_td_rollover();}if(cF){obj.style.cursor='hand';}
}
function NN_td_exe(s,e){
	var cntr=0,sp=gArgs[5];
	var r = parseInt(e.substring(1,3),16),g = parseInt(e.substring(3,5),16), b = parseInt(e.substring(5,7),16);
	if (navigator.userAgent.indexOf('Firefox') == -1){
	var rr = parseInt(s.substring(1,3),16),gg = parseInt(s.substring(3,5),16),bb = parseInt(s.substring(5,7),16);}else{
	var rslt = s.match(/rgb\((\d{1,3}), (\d{1,3}), (\d{1,3})\)/);
	var rr = parseInt(rslt[1]),gg = parseInt(rslt[2]),bb = parseInt(rslt[3]);}
		if (r!=rr){if(rr<r){rr=rr+sp>r?r:rr+sp;}else{rr=rr-sp<r?r:rr-sp;}}else{cntr++;}
		if (g!=gg){if(gg<g){gg=gg+sp>g?g:gg+sp;}else{gg=gg-sp<g?g:gg-sp;}}else{cntr++;}
		if (b!=bb){if(bb<b){bb=bb+sp>b?b:bb+sp;}else{bb=bb-sp<b?b:bb-sp;}}else{cntr++;}
	rr=rr.toString(16);gg=gg.toString(16);bb=bb.toString(16);
	rr=rr.length==1?'0'+rr:rr;gg=gg.length==1?'0'+gg:gg;bb=bb.length==1?'0'+bb:bb;
	if (cntr!=3){return '#'+rr.toUpperCase()+gg.toUpperCase()+bb.toUpperCase();}
}
function NN_td_rollover(){
	switch(gArgs[3]){
	case 1:gArgs[0].style.backgroundColor=gArgs[1];break;
	case 2:gArgs[0].style.backgroundColor=NN_td_exe(gArgs[0].style.backgroundColor,gArgs[1]);}
	switch(gArgs[4]){
	case 1:gArgs[0].style.color=gArgs[2];break;
	case 2:gArgs[0].style.color=NN_td_exe(gArgs[0].style.color,gArgs[2]);}
	NNt=setTimeout('NN_td_rollover()',1);
}
function NN_td_reset(obj,hex,tHex){
	obj.style.backgroundColor=hex;obj.style.color=tHex;clearTimeout(NNt);
}
function NN_target_frames(t,l){
	if(t.length==0){document.location.href=l;}
	else if(top.frames[t]!=undefined){top.frames[t].location=l;}else{window.open(l);}
}

//these are the unobtrusive events, hopefully

document.observe("dom:loaded", function() {
	
	MM_preloadImages('images/windo/windo_ab.png','images/windo/windo_ne.png','images/windo/windo_po.png','images/windo/windo_co.png','images/windo/windo_cl.png','images/windo/windo_we.png');

	//ABOUT
  	$("aboutbut").observe('mouseover', function(){MM_swapImage('window','','images/windo/windo_ab.png',1);});
  	$("aboutbut").observe('click', function(){animatedcollapse.toggle('aboutus');animatedcollapse.hide('welcome');});
	$("aboutbut").observe('mouseout', function(){MM_preloadImages('images/windo/winda_id.jpg','images/windo/winda_cr.jpg','images/windo/winda_ph.jpg','images/windo/winda_il.jpg','images/windo/winda_we.jpg','images/windo/winda_pr.jpg');});
	
	//NEWS
	$("newsbut").observe('mouseover', function(){MM_swapImage('window','','images/windo/windo_ne.png',1);});
  	$("newsbut").observe('click', function(){animatedcollapse.toggle('news');});

	//PORTFOLIO
	$("portbut").observe('mouseover', function(){MM_swapImage('window','','images/windo/windo_po.png',1);});
  	$("portbut").observe('click', function(){animatedcollapse.toggle('portfolio');animatedcollapse.hide('welcome');});
	$("portbut").observe('mouseout', function(){MM_preloadImages('images/portos/porto_exped.jpg','images/portos/porto_vana.jpg','images/portos/porto_orange.jpg','images/portos/porto_dcoded.jpg','images/portos/porto_rencol.jpg','images/portos/porto_seven.jpg','images/portos/porto_compusys.jpg','images/portos/porto_barns.jpg','images/portos/porto_hrowenstand_03.jpg','images/portos/porto_holyt.jpg');});
	
	//CONTACT
	$("contactbut").observe('mouseover', function(){MM_swapImage('window','','images/windo/windo_co.png',1);});
  	$("contactbut").observe('click', function(){animatedcollapse.toggle('contact');animatedcollapse.hide('welcome');});
	
	//WELCOME
	$("welcbut").observe('mouseover', function(){MM_swapImage('window','','images/windo/windo_we.png',1);});
  	$("welcbut").observe('click', function(){animatedcollapse.toggle('welcome');});
	$("welcomeroll").observe('mouseover', function(){MM_swapImage('window','','images/windo/windn_nav.jpg',1);});
	$("welcomeroll").observe('click', function(){animatedcollapse.hide('welcome');});
	
	//CLIENT
	$("clientbut").observe('mouseover', function(){MM_swapImage('window','','images/windo/windo_cl.png',1);});
  	$("clientbut").observe('click', function(){animatedcollapse.toggle('client');animatedcollapse.hide('welcome');});
	$("clientbut").observe('mouseout', function(){MM_preloadImages('images/login_sprite.jpg');});
	
	//ABOUT US IMGSWAPS
	$("abo_ident").observe('mouseover', function(){MM_swapImage('window','','images/windo/winda_id.jpg',1);});
	$("abo_creat").observe('mouseover', function(){MM_swapImage('window','','images/windo/winda_cr.png',1);});
	$("abo_phot").observe('mouseover', function(){MM_swapImage('window','','images/windo/winda_ph.jpg',1);});
	$("abo_illus").observe('mouseover', function(){MM_swapImage('window','','images/windo/winda_il.png',1);});
	$("abo_web").observe('mouseover', function(){MM_swapImage('window','','images/windo/winda_we.jpg',1);});
	$("abo_print").observe('mouseover', function(){MM_swapImage('window','','images/windo/winda_pr.jpg',1);});
	$("abo_exhib").observe('mouseover', function(){MM_swapImage('window','','images/windo/winda_ex.png',1);});
	$("abo_strat").observe('mouseover', function(){MM_swapImage('window','','images/windo/winda_st.jpg',1);});
	
	//PORTFOLIO IMGSWAPS
	
	//NEWS ITEMS
	$("news_rsvp").observe('click', function(){Lightview.show('invnews');});
	$("news_rsvp").observe('mouseout', function(){this.style.backgroundColor='transparent';});
	$("news_rsvp").observe('mouseover', function(){this.style.backgroundColor='#FC0';MM_swapImage('window','','images/windo/windn_invite.jpg',1);});
	
	$("news_sssg").observe('click', function(){Lightview.show('sssnews');});
	$("news_sssg").observe('mouseout', function(){this.style.backgroundColor='transparent';});
	$("news_sssg").observe('mouseover', function(){this.style.backgroundColor='#FC0';MM_swapImage('window','','images/news/news_sssg_ro.jpg',1);});
	
	//FOOTER
	
	//the welcome surprise
	animatedcollapse.show('welcome');
});