var tabber = { tab_id: 'home-page-tab-1', img_loading: null, jscal_loaded: null};
var x = null;

tabber.go = function(tab){
	tabber.pre_tab_id = tabber.tab_id;
	tabber.tab_id = tab.id;
	
	document.cookie = "home_page_tab=" + tabber.tab_id + "; path=/";
	
	if(store.tabs[tab.id]){
		tabber.applyCached(store.tabs[tab.id]);
		return;
	}
	
	x = sajax_init_object();
	if(!x){
		alert('Failed to load this tab dynamically. Will continue with ordinary reload.');
		document.location.href = '/?tab='+tab.substr(14);	
	}
	
	document.getElementById('home-page-content').innerHTML = '<div class="loading"><img src="'+tabber.img_loading.src+'"/></div>';
  tabber.get(tabber.tab_id, tabber.apply);
}

tabber.get = function(){
	sajax_do_call("content_tabs", tabber.get.arguments);
}
	
tabber.apply = function(content){
	eval(content);
	document.getElementById('home-page-content').innerHTML = home_page_content;

	if(home_page_javascript){
		eval(home_page_javascript);
	}
	
	//cache generated tab content
	store.tabs[tabber.tab_id] = document.getElementById('home-page-content').innerHTML;
	
	delete home_page_content;
	delete home_page_javascript;
	
	document.getElementById(tabber.tab_id).className = 'active';
	if(tabber.pre_tab_id != tabber.tab_id)
		document.getElementById(tabber.pre_tab_id).className = 'common';

	
	//jscalendar for photoops tab
	if(tabber.tab_id == 'home-page-tab-4' && !tabber.jscal_loaded){
		jscalendar_load();
		jscal_loaded = 1;
	}
}

tabber.applyCached = function(content){
	document.getElementById('home-page-content').innerHTML = content;
	
	document.getElementById(tabber.tab_id).className = 'active';
	if(tabber.pre_tab_id != tabber.tab_id)
		document.getElementById(tabber.pre_tab_id).className = 'common';
		
	//jscalendar for photoops tab
	if(tabber.tab_id == 'home-page-tab-4' && !tabber.jscal_loaded){
		jscalendar_load();		
		tabber.jscal_loaded = 1;
	}
}


/////////////////////////////////////////////
// remote scripting library
// (c) copyright 2005 modernmethod, inc
	var sajax_debug_mode = false;
	var sajax_request_type = "POST";
	
	function sajax_debug(text) {
		if (sajax_debug_mode)
			alert("RSD: " + text)
	}
	
	function sajax_init_object() {
		sajax_debug("sajax_init_object() called..")
			
		var A;
		try {
			A=new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				A=new ActiveXObject("Microsoft.XMLHTTP");
			} catch (oc) {
				A=null;
			}
		}
		if(!A && typeof XMLHttpRequest != "undefined")
			A = new XMLHttpRequest();
		if (!A)
			sajax_debug("Could not create connection object.");
		return A;
	}
	
	function sajax_do_call(func_name, args) {
		var i, n; // x - made global 
		var uri;
		var post_data;
		
		uri = "?q=hinsdale_home_page/get";
		if (sajax_request_type == "GET") {
			if (uri.indexOf("?") == -1) 
				uri = uri + "?rs=" + escape(func_name);
			else
				uri = uri + "&rs=" + escape(func_name);
			for (i = 0; i < args.length-1; i++) 
				uri = uri + "&rsargs[]=" + escape(args[i]);
			uri = uri + "&rsrnd=" + new Date().getTime();
			post_data = null;
		} else {
			post_data = "rs=" + escape(func_name);
			for (i = 0; i < args.length-1; i++) 
				post_data = post_data + "&rsargs[]=" + escape(args[i]);
		}
		
		//x = sajax_init_object();
		x.open(sajax_request_type, uri, true);
		if (sajax_request_type == "POST") {
			x.setRequestHeader("Method", "POST " + uri + " HTTP/1.1");
			x.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		}
		x.onreadystatechange = function() {
			if (x.readyState != 4) 
				return;
			sajax_debug("received " + x.responseText);
			
			var status;
			var data;
			status = x.responseText.charAt(0);
			data = x.responseText.substring(2);
			if (status == "-") 
				alert("Error: " + data);
			else  
				args[args.length-1](data);
		}
		x.send(post_data);
		sajax_debug(func_name + " uri = " + uri + "/post = " + post_data);
		sajax_debug(func_name + " waiting..");
		delete x;
	}
/////////////////////////////////////////////

/////////////////////////////////////////////
var featured = {nids: {}};

featured.go = function(nid){
	//check for cached version
	if(store.featured[nid]){
		featured.applyCached(store.featured[nid], nid);
		return;		
	}
	
	x = sajax_init_object();
	if(!x){
		alert('Failed to load this document dynamically. Will continue with ordinary reload.');
		document.location.href = '/?q=node/' + nid;	
	}
	
  featured.get_primary(nid,featured.apply);
}

featured.get_primary = function(nid){
	sajax_do_call("content_featured", featured.get_primary.arguments);
}

featured.apply = function(content){
	eval(content);
	
	document.getElementById('featured-primary').innerHTML = primary_content;
	
	//themize selected secondary item
	featured.mark_active_secondary(nid);
		
	//cache featured and recache all the tab
	store.featured[nid] = primary_content;
	store.tabs[tabber.tab_id] = document.getElementById('home-page-content').innerHTML;
	
	delete primary_content;
	delete nid;
}



featured.applyCached = function(content, nid){
	document.getElementById('featured-primary').innerHTML = content;

	//themize selected secondary item
	featured.mark_active_secondary(nid);	
		
	//recache all the tab
	store.tabs[tabber.tab_id] = document.getElementById('home-page-content').innerHTML;
}



//stylize active secondary element and put its id in featured.nid
featured.mark_active_secondary = function(nid){
	//alert('start: '+tabber.tab_id+'->'+featured.nids[tabber.tab_id]);
	if(featured.nids[tabber.tab_id]){
		if(nid == null){
			nid = featured.nids[tabber.tab_id];
		}
		else
			if(nid == featured.nids[tabber.tab_id]) return;
	
		//demark previous active secondary
		var elem = document.getElementById('featured-secondary-'+featured.nids[tabber.tab_id]);
		if(elem){
			elem.className = 'featured-secondary-' + tabber.tab_id.substr(14);
		}
	}
	
	//alert(2);
	var elem = document.getElementById('featured-secondary-'+nid);
	if(elem){
		elem.className = 'featured-secondary-' + tabber.tab_id.substr(14) + '-active';
	}
	
	//store active nid for current tab
	featured.nids[tabber.tab_id] = nid;	
	
	//alert('end: '+tabber.tab_id+'->'+featured.nids[tabber.tab_id]);
}


//store for caches
var store = {tabs:{}, featured:{}};


//jscalendar loader
jscalendar_load = function() {
  // Select all input elements
  inputs = document.getElementsByTagName('input');
  for (var i = 0; input = inputs[i]; ++i) {
    if (input && (input.getAttribute('type') == 'text') && hasClass(input, 'jscalendar')) {
      var form = input.form;
      var button = document.createElement('button');
      button.appendChild(document.createTextNode(' ... '));
      button.setAttribute('id', input.getAttribute('id') + '-button');
      addClass(button, 'jscalendar-icon');
      input.parentNode.insertBefore(button, input.nextSibling);
      addClass(input.parentNode, 'jscalendar');
      var settings = [];
      settings['ifFormat'] = form.elements['edit[jscalendar_ifFormat]'] ? form.elements['edit[jscalendar_ifFormat]'].value : '%Y-%m-%d %H:%M:%S';
      // We use eval() because the result is a boolean while our input is a string.
      settings['showsTime'] = form.elements['edit[jscalendar_showsTime]'] ? eval(form.elements['edit[jscalendar_showsTime]'].value) : true;
      settings['timeFormat'] = form.elements['edit[jscalendar_timeFormat]'] ? form.elements['edit[jscalendar_timeFormat]'].value : '12';
      Calendar.setup(
        {
          inputField  : input.id,
          ifFormat    : settings['ifFormat'],
          button      : input.getAttribute('id') + '-button',
          showsTime   : settings['showsTime'],
          timeFormat  : settings['timeFormat']
        }
      );
    }
  }
}