/**
 * Copyright (c) 2008 Andreas Timm | symmetrics gmbh (http://symmetrics.de)
 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
 *
 * $CreateDate: 2008-10-04 16:00:00 +0001 (Sat, 04 Okt 2008) $
 *
 * Version: 0.1
 *
 * @author Andreas Timm <at@symmetrics.de>
 *
 * Requires:
 **/
var References = function()
{
	this.click_time = 0

	//hidde|to_hidde|to_show|show
	this.scroller_show_flag = "hide";

	this.thumbnails_cur = 0 ;
	this.thumbnails_links = [] ;

	this.tf_buf = [] ;
	this.tf_init = null ;
	this.tf_check = false ;



	/**
	 * init
	 **/
	this.init = function ()
	{
		this.domModification();
		
		var etxtscroll_flag = false ;
		
		if ( $("#references_slider li").length > 4 )
		{
			$("#references_slider").css( "position", "absolute" );
			$("#references_box").css( "height", 429 ) ;
	
			$("#references_box").etxtscroll
			({
				cont: "#references_slider",
				scroller: "#reference_scroll",
				cursor_up: "url(fileadmin/images/references/link_select.cur), pointer",
				cursor_down: "url(fileadmin/images/references/link_select_close.cur), pointer",
				cont_down: function () {
					References.click_time = (new Date()).getTime();
				},
				cont_up: function ( v ) {
					if
					(
						( v.delta < 25 && v.delta > -25 ) ||
						(new Date()).getTime() - References.click_time < 200
					)
						References.item_click();
					References.click_time = 0;
				}
			});
	
			etxtscroll_flag = true ;
		}
		else
			$("#references_slider li").click(function(){
				References.item_click();
			});

		//thumbnail fage In/Out
		$("#references_slider li").each( function ( id )
		{
			$(this).hover(
				function()
				{
					if ( ! etxtscroll_flag || ! $.etxtscroll.references_box.field_down )
					{
						References.thumbnails_cur = id;
						References.thumbnailFade({id:id,action:"to_hide"});
					}
				},
				function()
				{
					if ( ! etxtscroll_flag || ! $.etxtscroll.references_box.field_down )
					{
						References.thumbnails_cur = -1;
						References.thumbnailFade({id:id,action:"to_show"});
					}
				}
			);

		});


	};


	/**
	 * DOM modification
	 **/
	this.domModification = function ()
	{
		var tag_a ;
		var img = "" ;

		$("#references_slider li").each( function ( id )
		{
			tag_a = $(this).children("a") ;
			References.thumbnails_links[id] = tag_a.attr("href");
			img = tag_a.children("img").attr("src");
			tag_a.remove();
			$(this).append
			(
				"<div class=\"thumbnail\" style=\"background:url('"+img+"')\">" +
				"</div>"
			);
		});

	} ;




	/**
	 *
	 *
	 * @param	Object		v:
	 *
	 *
	 * @return
	 **/
	this.scrollerShow = function ( flag )
	{
		if ( flag == "to_show" )
		{
			if ( References.scroller_show_flag == "to_hide" )
				References.scroller_show_flag = "to_show" ;
			else if ( References.scroller_show_flag == "hide" )
			{
				References.scroller_show_flag = "to_show" ;
				$("#reference_scroll").fadeIn(1000,function(){
					if ( References.scroller_show_flag == "to_hide" )
					{
						References.scroller_show_flag = "show";
						References.scrollerShow("to_hide");
					}
					else
						References.scroller_show_flag = "show";
				});
			}
		}
		else
		{
			if ( References.scroller_show_flag == "to_show" )
				References.scroller_show_flag = "to_hide";
			else if ( References.scroller_show_flag == "show" )
			{
				References.scroller_show_flag = "to_hide";
				$("#reference_scroll").fadeOut(1000,function(){
					if ( References.scroller_show_flag == "to_show" )
					{
						References.scroller_show_flag = "hide";
						References.scrollerShow("to_show");
					}
					else
						References.scroller_show_flag = "hide";
				});
			}
		}
	};



	/**
	 * check hide out
	 *
	 * @param	Object		event:
	 **/
	this.checkHideOut = function ( event )
	{
		if
		(
			event.pageX < 913 || event.pageX > 1084 ||
			event.pageY < 413 || event.pageY > 840
		)
			References.scrollerShow("to_hide");
	};



	/**
	 * thumbnail fade
	 *
	 * @param	Object		id:
	 * 				Number		id:
	 * 				String		action: to_hide|to_show
	 * 				String		type: do|set
	 **/
	this.thumbnailFade = function ( v )
	{
		if ( this.tf_init != null )
		{
			clearTimeout ( this.tf_init ) ;
			this.tf_init = null ;
			References.thumbnailFade({action:"check"});
		}

		var len = this.tf_buf.length ;
		switch ( v.action )
		{
			case "to_hide":
				if ( len > 0 )
				{
					if ( this.tf_buf[len-1].action == "set_hide" )
						this.tf_buf[len-1].id = v.id ;
					else
					{
						if ( this.tf_buf[len-1].id == v.id )
							this.tf_buf.pop();
						else
							this.tf_buf[len] = {id:v.id,action:"set_hide"} ;
					}
				}
				else
					this.tf_buf[0] = {id:v.id,action:"set_hide"} ;
			break;
			case "to_show":
				if ( len > 0 )
				{
					if ( this.tf_buf[len-1].id == v.id )
					{
						if ( this.tf_buf[len-1].action == "set_hide" )
							this.tf_buf.pop();
					}
					else
						this.tf_buf[len] = {id:v.id,action:"set_show"} ;
				}
				else
					this.tf_buf[0] = {id:v.id,action:"set_show"} ;
			break;
			case "set_hide":
				if ( v.timeout == undefined )
				{
					this.tf_init = setTimeout ( function (){
						References.tf_init = null ;
						References.thumbnailFade({id:v.id,action:"set_hide",timeout:true});
					}, 200 );
				}
				else
				{
					var obj = $("#references_slider li:eq("+v.id+") .thumbnail");
					this.tf_hiding = v.id ;
					obj.animate({opacity: 0.2},350,function(){
						References.tf_hiding = -1 ;
						References.thumbnailFade({action:"check"});
					});
				}
			break;
			case "set_show":
				var obj = $("#references_slider li:eq("+v.id+") .thumbnail");
				obj.animate({opacity: 1},350,function(){
					References.thumbnailFade({action:"check"});
				});
			break;
			case "check":
				if ( len > 0 )
				{
					if ( this.tf_init != null )
						clearTimeout ( this.tf_init ) ;

					this.thumbnailFade(this.tf_buf.shift());
				}
				else
				{
					this.tf_check = false ;
					return true ;
				}
			break;
		}

		if ( ! this.tf_check )
		{
			this.tf_check = true ;
			this.thumbnailFade({action:"check"});
		}

		return true ;
	}



	/**
	 *
	 *
	 * @param	Object		v:
	 *
	 *
	 * @return
	 **/
	this.item_click = function ( v )
	{
		if ( this.thumbnails_cur != -1 )
		{
			document.location = this.thumbnails_links[this.thumbnails_cur] ;
		}
	};

} ;

References = new References ;