//** Animated Collapsible DIV v2.0- (c) Dynamic Drive DHTML code library: http://www.dynamicdrive.com.
//** May 24th, 08'- Script rewritten and updated to 2.0.
//** June 4th, 08'- Version 2.01: Bug fix to work with jquery 1.2.6 (which changed the way attr() behaves).

var animatedcollapse={
divholders: {}, //structure: {div.id, div.attrs, div.$divref}
divgroups: {}, //structure: {groupname.count, groupname.lastactivedivid}
lastactiveingroup: {}, //structure: {lastactivediv.id}

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
	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
	}
},

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
	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')) : 500)
		return false
	}
},

generatemap:function(){
	var map={}
	for (var i=0; i<arguments.length; i++){
		if (arguments[i][1]!=null){
			map[arguments[i][0]]=arguments[i][1]
		}
	}
	return map
},

init:function(){
	var ac=this
	jQuery(document).ready(function($){
		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){
				var cssdisplay=(jQuery.inArray(this.id, persistopenids)!=-1)? 'block' : 'none'
			}
			else{
				var cssdisplay=this.getAttr('hide')? 'none' : null
			}
			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 (!targetgroup.lastactivedivid && this.$divref.css('display')!='none' || cssdisplay=="block") //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)
				ac.divholders[this.lastactivedivid].$divref.show() //and show last "active" DIV within each group (one that should be expanded)
		})
		var $allcontrols=$('*[rel]').filter('[@rel^="collapse-"], [@rel^="expand-"], [@rel^="toggle-"]') //get all elements on page with rel="collapse-", "expand-" and "toggle-"
		var controlidentifiers=/(collapse-)|(expand-)|(toggle-)/
		$allcontrols.each(function(){
			$(this).click(function(){
				var relattr=this.getAttribute('rel')
				var divid=relattr.replace(controlidentifiers, '')
				var doaction=(relattr.indexOf("collapse-")!=-1)? "hide" : (relattr.indexOf("expand-")!=-1)? "show" : "toggle"
				return ac.showhide(divid, doaction)
			}) //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=/"
}

}

var arr1 = new Array();
var arr2 = new Array();
var arr3 = new Array();
var arr4 = new Array();

arr1[0] = 'bio';
arr1[1] = 'showreview';
arr1[2] = 'discography';
arr1[3] = 'photogallery';
arr1[4] = 'tourdates';
arr1[5] = 'fans';
arr1[6] = 'links';

arr2[0] = 'miscVideoInfo';
arr2[1] = 'concertinfo';
arr2[2] = 'newestConcerts';
arr2[3] = 'musicvideos';
arr2[4] = 'interviews';
arr2[5] = 'blog';
arr2[6] = 'albumreview';
arr2[7] = 'features';
arr2[8] = 'pressbuzz';
arr2[9] = 'photos';
arr2[10] = 'newestGuestApt';

arr3[0] = 'videoViews';
arr3[1] = 'myFriends';
arr3[2] = 'myPhotos';
arr3[3] = 'myInfo';
arr3[4] = 'purchasedvideos';

arr4[0] = 'newestConcerts2';
arr4[1] = 'musicvideos2';
arr4[2] = 'interviews2';
function buildDivs(){
// implementation

	var i,j,k;
	for (i in arr1){
		animatedcollapse.addDiv(arr1[i], 'fade=1,height=220px');
	}
	for (j in arr2){
		animatedcollapse.addDiv(arr2[j], 'fade=1,height=220px');
	}
	for (k in arr3){
		animatedcollapse.addDiv(arr3[k], 'fade=1,height=220px');
	}
	for (k in arr4){
		animatedcollapse.addDiv(arr4[k], 'fade=1,height=220px');
	}
}

buildDivs();
animatedcollapse.init();

var menu_visible = '';
var grupo1 = 0;
var grupo2 = 0;
var grupo3 = 0;
var grupo4 = 0;
var quegrupo = 0;
var menuact1 = '';
var menuact2 = '';
var menuact3 = '';
var menuact4 = 'newestConcerts2';


function setShowHide(whatmenushow) {
    // Veo a que grupo pertenece
    if ( (whatmenushow == 'miscVideoInfo' ) || (whatmenushow == 'bio' ) || (whatmenushow == 'showreview' ) || (whatmenushow == 'discography' ) || (whatmenushow == 'photogallery' ) || (whatmenushow == 'tourdates' ) || (whatmenushow == 'fans' ) || (whatmenushow == 'links' ) ) {
	quegrupo = 1;
    }
    if ( (whatmenushow == 'concertinfo' ) || (whatmenushow == 'newestConcerts' ) || (whatmenushow == 'newestGuestApt' ) || (whatmenushow == 'musicvideos' ) || (whatmenushow == 'interviews' ) || (whatmenushow == 'blog' ) || (whatmenushow == 'albumreview' ) || (whatmenushow == 'features' ) || (whatmenushow == 'pressbuzz' ) || (whatmenushow == 'photos' ) ) {
	quegrupo = 2;
    }
    if ( (whatmenushow == 'videoViews' ) || (whatmenushow == 'myFriends' ) || (whatmenushow == 'myPhotos' ) || (whatmenushow == 'myInfo' ) || (whatmenushow == 'purchasedvideos' ) ) {
	quegrupo = 3;
    }
    if ( (whatmenushow == 'newestConcerts2' ) || (whatmenushow == 'musicvideos2' ) || (whatmenushow == 'interviews2' ) ) {
	quegrupo = 4;
    }
    // fin veo a que grupo pertenece

    if ( ( ( menuact1 == whatmenushow ) && ( grupo1 == 1 ) )  ||  ( ( menuact2 == whatmenushow ) && ( grupo2 == 1 ) ) ||  ( ( menuact3 == whatmenushow ) && ( grupo3 == 1 ) ) || ( ( menuact4 == whatmenushow ) && ( grupo4 == 1 ) ) ) {
        animatedcollapse.hide(whatmenushow);

	if ( quegrupo == 1 ) { grupo1 = 0; menuact1 = ''; }
	if ( quegrupo == 2 ) { grupo2 = 0; menuact2 = ''; }
	if ( quegrupo == 3 ) { grupo3 = 0; menuact3 = ''; }
	if ( quegrupo == 4 ) { grupo4 = 0; menuact4 = ''; }

    } else if ( ( ( menuact1 != '' ) && ( quegrupo == 1 ) )  ||  ( ( menuact2 != '' ) && ( quegrupo == 2 ) ) ||  ( ( menuact3 != '' ) && ( quegrupo == 3 ) ) ||  ( ( menuact4 != '' ) && ( quegrupo == 4 ) ) ) {

	if ( quegrupo == 1 ) { animatedcollapse.hide(menuact1); }
	if ( quegrupo == 2 ) { animatedcollapse.hide(menuact2); }
	if ( quegrupo == 3 ) { animatedcollapse.hide(menuact3); }
	if ( quegrupo == 4 ) { animatedcollapse.hide(menuact4); }

        animatedcollapse.show(whatmenushow);

	if ( quegrupo == 1 ) { grupo1 = 1; menuact1 = whatmenushow; }
	if ( quegrupo == 2 ) { grupo2 = 1; menuact2 = whatmenushow; }
	if ( quegrupo == 3 ) { grupo3 = 1; menuact3 = whatmenushow; }
	if ( quegrupo == 4 ) { grupo4 = 1; menuact4 = whatmenushow; }

    } else {

        animatedcollapse.show(whatmenushow);

	if ( quegrupo == 1 ) { grupo1 = 1; menuact1 = whatmenushow; }
	if ( quegrupo == 2 ) { grupo2 = 1; menuact2 = whatmenushow; }
	if ( quegrupo == 3 ) { grupo3 = 1; menuact3 = whatmenushow; }
	if ( quegrupo == 4 ) { grupo4 = 1; menuact4 = whatmenushow; }
    }
}



/*
var menu_visible = '';

function setShowHide(whatmenushow) {
    if (menu_visible == whatmenushow) {
        if (document.getElementById("lastDiv").value.length > 0) {
            animatedcollapse.hide(document.getElementById("lastDiv").value);
        }
        else {
            animatedcollapse.hide(whatmenushow);
        }
        menu_visible = '';
        document.getElementById("lastDiv").value = whatmenushow;
    } else if (menu_visible != '') {
        if (document.getElementById("lastDiv").value.length > 0) {
            animatedcollapse.hide(document.getElementById("lastDiv").value);
        }
        else {
            animatedcollapse.hide(whatmenushow);
        }
        menu_visible = whatmenushow;
        animatedcollapse.show(whatmenushow);
        document.getElementById("lastDiv").value = whatmenushow;
    } else {
        animatedcollapse.show(whatmenushow);
        menu_visible = whatmenushow;
        document.getElementById("lastDiv").value = whatmenushow;
    }
}*/
