var section;
var Site = {
	start: function(){
		Site.moveStart();
		Site.fadeStart();
	},

	moveStart: function(){
		var sections = [$('section_members'),$('section_download'),$('section_upload'),$('section_aboutme'),$('section_details'),$('section_notify')];
		sections.each(function(el, i){
			if(el) {
				el.set('background-position', -30);
				Site.moveOver(el, i);
			}
		}, this);

	},
		
	fadeStart: function(){
		var genres = $$('#featured_genres li');
			genres.each(function(el, i){
				Site.fadeOver(el, i, '#721000', '#CF4B00');
		}, this);
		var mixes = $$('#featured_mixes li');
			mixes.each(function(el, i){
				Site.fadeOver(el, i, '#FFDD3F', '#E7BF00');
		}, this);
		var arist_mixes = $$('#artist_mixes li');
			arist_mixes.each(function(el, i){
				Site.fadeOver(el, i, '#CF4B00', '#FE9D00');
		}, this);

	},

	fadeOver: function(el, i, enterColor, leaveColor){
		var morph = new Fx.Morph(el);
		el.addEvent('mouseenter', function(e) {
			el.tween('background-color', enterColor);
		});
		el.addEvent('mouseleave', function(e) {
			el.tween('background-color', leaveColor);
		});
	},

	moveOver: function(el, i){
		el.addEvent('mouseenter', function(e) {
			el.tween('background-position', 0);
		});
		el.addEvent('mouseleave', function(e) {
			el.tween('background-position', -30);
		});
	},
		
	section: function(section_name,heading) {
		var title = $('page_heading');
		if(!heading) heading = '';
		if(!section) section = $('panel5');
		if(!section) section = $('panel1');
		title.innerHTML = heading;
		section.fade(0);
		$(section_name).fade(1);
		section = $(section_name);
	},
	
	avatar: function(path) {
		//write an image tag for the avatar
		if(!path || path == "") {
			document.write('<img src="/beatplexity/images/no_avatar.gif" alt=""/>');
		} else {
			var avatar = path.slice(15);
			document.write('<img src="/cgi-bin/beatplexity/SISite/data/images/users/'+avatar+'" width="60" width="60" alt=""/>');
		}
	},
	
	mixsearch: function(mixsearch_form) {
		var mixsearch_request = new ajaxObject('/beatplexity/mixsearch.php');
		mixsearch_request.callback = function(responseText, responseStatus, responseXML) {
			if (responseStatus==200) {
			} else {
				altert(responseStatus+ ': Error processing request. Please try again.');
			}
		}
		mixsearch_request.update(formDataQuery(mixsearch_form),'POST');
	}
	
};

//answser Form fields as query string
function formDataQuery(form) {
	var query = '';
	var sep = '';
	for(i=0; i<form.elements.length; i++) {
		if(form.elements[i].type != 'submit') {
			query+=sep+form.elements[i].name+'='+form.elements[i].value;
			sep = '&';
		}
	}
	return query;
}

//AJAX helper
function ajaxObject(url, callbackFunction) {
  var that=this;      
  this.updating = false;
  this.abort = function() {
    if (that.updating) {
      that.updating=false;
      that.AJAX.abort();
      that.AJAX=null;
    }
  }
  this.update = function(passData,postMethod) { 
    if (that.updating) { return false; }
    that.AJAX = null;                          
    if (window.XMLHttpRequest) {              
      that.AJAX=new XMLHttpRequest();              
    } else {                                  
      that.AJAX=new ActiveXObject("Microsoft.XMLHTTP");
    }                                             
    if (that.AJAX==null) {                             
      return false;                               
    } else {
      that.AJAX.onreadystatechange = function() {  
        if (that.AJAX.readyState==4) {             
          that.updating=false;                
          that.callback(that.AJAX.responseText,that.AJAX.status,that.AJAX.responseXML);        
          that.AJAX=null;                                         
        }                                                      
      }                                                        
      that.updating = new Date();                              
      if (/post/i.test(postMethod)) {
        var uri=urlCall+'?'+that.updating.getTime();
        that.AJAX.open("POST", uri, true);
        that.AJAX.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        that.AJAX.setRequestHeader("Content-Length", passData.length);
        that.AJAX.send(passData);
      } else {
        var uri=urlCall+'?'+passData+'&timestamp='+(that.updating.getTime()); 
        that.AJAX.open("GET", uri, true);                             
        that.AJAX.send(null);                                         
      }              
      return true;                                             
    }                                                                           
  }
  var urlCall = url;        
  this.callback = callbackFunction || function () { };
}

//Initialise the site
window.addEvent('load', Site.start);
