dojo.require("dojox.xml.DomParser");

AddSafeLoaderCallback(Homeshow_Init);

var tmp_i = 0;
var homeshow_obj;
var hs_pre_image = new Image();
function Homeshow_Init()
{
	if(dojo.byId('homeshow_wrapper'))
	{
		homeshow_obj = new Homeshow(server_export_data_obj.homeshowxml, 'homeshow', dojo.byId('homeshow_wrapper'));
		
		// silly pre-pre-load hack
		hs_pre_image.onload = function() 
		{
			homeshow_obj.InitDisplay();
			homeshow_obj.StartOnSlideTransition(1);

		}
		
		hs_pre_image.src = 'homeshow/'+homeshow_obj.data_obj_array[0].image_filename;
		
	}
}

// =====================================================================================

// create homeshow obj
function Homeshow(xml_string, key_name, main_wrapper_node)
{
	this.SetupData(xml_string, key_name, main_wrapper_node);
}
Homeshow.prototype.SetupData = function(xml_string, key_name, main_wrapper_node)//slide_wrapper_node, control_wrapper_node)
{
	this.key_name = key_name;
	this.main_wrapper_node = main_wrapper_node;

	

	this.data_obj_array = new Array();
	this.config_obj = {};
	this.interval_id;
	
	this.preloaded_content_array = new Array;

	this.interval_animation = null;

	this.SetInternalState_SlideNum(1);

	var xdoc;
	var i, cur_node_name, cur_node_text_value;
	var tmp_node;
	
	xdoc=dojox.xml.DomParser.parse(xml_string);
	
	// *** load config data
	var config_node_array = xdoc.getElementsByTagName('config');
	var config_node = config_node_array[0];
	this.config_obj = {};
	for(i=0; i<config_node.childNodes.length; i++)
	{
		cur_node_name = '';
		cur_node_text_value = '';
		try
		{
			cur_node_name = config_node.childNodes[i].nodeName;
			cur_node_text_value = config_node.childNodes[i].childNodes[0].nodeValue;
		}
		catch(e) {}
		
		if(cur_node_name && cur_node_name != '#text')
		{
			this.config_obj[cur_node_name] = cur_node_text_value;
		}
	}
	
	// *** load slide data
	var slide_node_array = xdoc.getElementsByTagName('slide');
	var slide_node, list_i, tmp_obj;
	for(list_i=0; list_i<slide_node_array.length; list_i++)
	{
		// create preload location
		this.preloaded_content_array.push({});

		slide_node = slide_node_array[list_i];
		tmp_obj = {};
		for(i=0; i<slide_node.childNodes.length; i++)
		{
			cur_node_name = '';
			cur_node_text_value = '';
			try
			{
				cur_node_name = slide_node.childNodes[i].nodeName;
				cur_node_text_value = slide_node.childNodes[i].childNodes[0].nodeValue;
			}
			catch(e) {}
			
			if(cur_node_name != '#text')
				tmp_obj[cur_node_name] = cur_node_text_value;
		}

		this.data_obj_array.push(tmp_obj);
	}
	

	this.wrapper_node = document.createElement('div');
	this.wrapper_node.id = this.key_name+'_slide_wrapper';
	this.wrapper_node.className = this.key_name+'_slide_wrapper';
	this.main_wrapper_node.appendChild(this.wrapper_node);

	if(this.config_obj.is_show_control == 'true')
	{
		this.control_node = document.createElement('div');
		this.control_node.id = this.key_name+'_control_wrapper';
		this.control_node.className = this.key_name+'_control_wrapper';
		this.main_wrapper_node.appendChild(this.control_node);
	}
	if(this.config_obj.is_show_text_content == 'true')
	{
		this.text_content_node = document.createElement('div');
		this.text_content_node.id = this.key_name+'_text_content';
		this.text_content_node.className = this.key_name+'_text_content';
		this.main_wrapper_node.appendChild(this.text_content_node);
	}

	
	

	// *** create/setup slider obj
	var slider_config = new PaneSliderConfig();
	slider_config.is_wrapper_auto_height = parseInt(this.config_obj.height)?false:true;
	slider_config.is_wrapper_animate_height = parseInt(this.config_obj.height)?false:true;
	slider_config.height = parseInt(this.config_obj.height)?parseInt(this.config_obj.height):parseInt(this.config_obj.initial_height);
	slider_config.height_duration = parseInt(this.config_obj.transition_duration);
	slider_config.height_delay = parseInt(this.config_obj.transition_delay);
	slider_config.in_duration = parseInt(this.config_obj.transition_duration);
	slider_config.in_delay = parseInt(this.config_obj.transition_delay);
	slider_config.out_duration = parseInt(this.config_obj.transition_duration);
	slider_config.out_delay = parseInt(this.config_obj.transition_delay);
	slider_config.transition_gap = 10;
	this.slider_obj = new PaneSlider(this.wrapper_node, slider_config);

}

Homeshow.prototype.InitDisplay = function()
{

	if(dojo.byId('homeshow_text_content'))
		dojo.byId('homeshow_text_content').innerHTML = this.config_obj.content;
	
	// *** create control area
	if(this.config_obj.is_show_control == 'true' && this.control_node)
	{
		this.control_node.style.display = '';
		this.control_node.innerHTML = '';
		
		for(i=0; i<this.GetSlideCount(); i++)
		{
			
			//tmp_node = document.createElement('span');
			//tmp_node.innerHTML = '<span id="homeshow_content_item_'+(i+1)+'" class="homeshow_counter_item_normal" onmouseover="homeshow_obj.StopOnSlide(\''+(i+1)+'\');" onmouseout="homeshow_obj.StartOnSlide(\''+(i+1)+'\');" onclick="window.location = \''+this.data_obj_array[i].link+'\'">' + (i+1) + '</span>';
			//this.control_node.appendChild(tmp_node);
			this.control_node.innerHTML += '<span id="homeshow_content_item_'+(i+1)+'" class="homeshow_counter_item_normal" onmouseover="homeshow_obj.StopOnSlide(\''+(i+1)+'\');" onmouseout="homeshow_obj.StartOnSlide(\''+(i+1)+'\');" onclick="window.location = \''+this.data_obj_array[i].link+'\'">' + (i+1) + '</span>';
		}
	}
	else if(this.control_node)
	{
		this.control_node.style.display = 'none';
	}

	if(this.config_obj.is_show_control_interval_pause == 'true' && this.control_node)
	{
		tmp_node = document.createElement('span');
		tmp_node.innerHTML = '<span id="homeshow_interval_pause" style="" class="homeshow_counter_pause" onclick="homeshow_obj.Stop();">'+this.config_obj.control_interval_pause_content+'</span>';
		tmp_node.innerHTML += '<span id="homeshow_interval_play" style="" class="homeshow_counter_play" onclick="homeshow_obj.Start();">'+this.config_obj.control_interval_play_content+'</span>';
		this.control_node.appendChild(tmp_node);
		this.ClearIntervalAnimation();
	}
	
	if(this.config_obj.is_show_control_interval_bar == 'true')
	{
		tmp_node = document.createElement('span');
		tmp_node.id = 'homeshow_interval_bar';
		tmp_node.className = 'homeshow_interval_bar';
		tmp_node.innerHTML = this.config_obj.control_interval_bar_innerHTML;
		this.main_wrapper_node.appendChild(tmp_node);
		this.ClearIntervalAnimation();
	}




}

Homeshow.prototype.GetInternalState_SlideNum = function()
{
	return parseInt(this.cur_state_slide_num);
}
Homeshow.prototype.SetInternalState_SlideNum = function(slide_num)
{
	this.cur_state_slide_num = parseInt(slide_num);
}

// instantly show slide
Homeshow.prototype.SetForegroundSlide = function(slide_num)
{
	this.SetInternalState_SlideNum(slide_num);
	this.DoControlHighlight(slide_num);
	this.slider_obj.SetForegroundContent(this.GetSlideContent(this.GetInternalState_SlideNum()));
}
// transition for current slide content to slide_num slide content
Homeshow.prototype.DoForegroundTransition = function(slide_num)
{
	this.DoControlHighlight(slide_num)
	this.slider_obj.SetBackgroundContent(this.GetSlideContent(slide_num));
	this.slider_obj.DoTransition(0);
}

// instantly highlight the specified control number (and un-highlight the rest)
Homeshow.prototype.DoControlHighlight = function(slide_num)
{
	var i=0;
	if(this.config_obj.is_show_control == 'true' && this.control_node)
	{	
		for(i=0; i<this.GetSlideCount(); i++)
		{
			dojo.byId('homeshow_content_item_'+(i+1)).className = 'homeshow_counter_item_normal';
		}
		dojo.byId('homeshow_content_item_'+slide_num).className = 'homeshow_counter_item_highlight';
	}
}

// transition the highlight of the from_num slide to the slide_num slide (and un-highlight the rest)
Homeshow.prototype.DoControlHighlightTransition = function(slide_num, from_num)
{
	// to-do: if needed
}

// start the slide loop
Homeshow.prototype.Start = function()
{
	this.StartIntervalAnimation();
	this.interval_id = setInterval(this.GoNext, parseInt(this.config_obj.slide_duration));
	if(this.config_obj.is_show_control_interval_pause == 'true' && dojo.byId('homeshow_interval_pause'))
	{
		dojo.byId('homeshow_interval_pause').style.display = '';
		dojo.byId('homeshow_interval_play').style.display = 'none';
	}
}

Homeshow.prototype.StartIntervalAnimation = function()
{
	if(this.config_obj.is_show_control_interval_bar == 'true')
	{
		if(this.interval_animation)
		{
			this.interval_animation.stop();
		}
	
		if(dojo.byId('homeshow_interval_bar'))
		{
			dojo.byId('homeshow_interval_bar').style.display = '';
			dojo.byId('homeshow_interval_bar').style.width = '0';
			this.interval_animation = dojo.animateProperty( 
			{
				node: dojo.byId('homeshow_interval_bar'),
				duration: parseInt(this.config_obj.slide_duration), // ms to run animation
				delay: 0, // ms to stall before playing
				properties:{width: {end: this.config_obj.control_interval_bar_size}}
			});
			this.interval_animation.play();
		}
	}
}

Homeshow.prototype.ClearIntervalAnimation = function()
{
	if(this.config_obj.is_show_control_interval_bar == 'true')
	{
		if(this.interval_animation)
		{
			this.interval_animation.stop();
		}
	
		if(dojo.byId('homeshow_interval_bar'))
		{
			dojo.byId('homeshow_interval_bar').style.width = '0';
			dojo.byId('homeshow_interval_bar').style.display = 'none';
		}
	}
}

// stop the slide loop
Homeshow.prototype.Stop = function()
{
	 clearInterval(this.interval_id);
	 this.ClearIntervalAnimation();
	 if(this.config_obj.is_show_control_interval_pause == 'true' && dojo.byId('homeshow_interval_play'))
	 {
	 	dojo.byId('homeshow_interval_play').style.display = '';
	 	dojo.byId('homeshow_interval_pause').style.display = 'none';
	 }
}

// instantly go to a specific slide and stop the loop
Homeshow.prototype.StopOnSlide = function(slide_num)
{
	this.SetForegroundSlide(slide_num);
	this.Stop();
}

// transition directly to a specific slide and stop the loop
Homeshow.prototype.StopOnSlideTransition = function(slide_num)
{
	this.SetInternalState_SlideNum(slide_num);
	
	this.DoForegroundTransition(slide_num);

	this.Stop();
}

// instantly go to a specific slide and start the loop
Homeshow.prototype.StartOnSlide = function(slide_num)
{
	this.Stop();
	
	this.SetInternalState_SlideNum(slide_num);
	this.SetForegroundSlide(slide_num);
	
	this.Start();
}

// transition directly to a specific slide and start the loop
Homeshow.prototype.StartOnSlideTransition = function(slide_num)
{
	this.Stop();
	
	this.SetInternalState_SlideNum(slide_num);
	this.DoForegroundTransition(slide_num);
	
	this.Start();
}


Homeshow.prototype.GetSlideContent = function(slide_num)
{
	this.PreloadSlideContent(slide_num);
	this.PreloadSlideContent(slide_num + 1);
	
	return '<span style="cursor:pointer;" onclick="window.location = \''+this.data_obj_array[slide_num - 1].link+'\';"><img src="'+this.preloaded_content_array[slide_num - 1].image_obj.src+'" /></span>';
}

Homeshow.prototype.PreloadSlideContent = function(slide_num)
{
	if(slide_num > 0 && slide_num <= this.GetSlideCount())
	{
		this.preloaded_content_array[slide_num - 1].image_obj = new Image();
		this.preloaded_content_array[slide_num - 1].image_obj.src = 'homeshow/'+this.data_obj_array[slide_num - 1].image_filename;
		//console.log('preloading '+(slide_num - 1)+': '+this.preloaded_content_array[slide_num - 1].image_obj.src);
	}
}

Homeshow.prototype.GetSlideCount = function()
{
	return this.data_obj_array.length;
}

// transition to the next slide in the loop
Homeshow.prototype.GoNext = function()
{
	var cur_scope_parent;
	if(this.slider_obj)
	{
		cur_scope_parent = this;
	}
	else
	{
		cur_scope_parent = homeshow_obj;
	}
	
	if(cur_scope_parent.GetInternalState_SlideNum() < cur_scope_parent.GetSlideCount())
	{
		cur_scope_parent.SetInternalState_SlideNum(cur_scope_parent.GetInternalState_SlideNum() + 1);
	}
	else
	{
		cur_scope_parent.SetInternalState_SlideNum(1);
	}
	
	cur_scope_parent.DoForegroundTransition(cur_scope_parent.GetInternalState_SlideNum());
	
	cur_scope_parent.StartIntervalAnimation();
}