/*
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
code language:		javascript 1.8
folder name:            files/dclib_5/dcjs_5.0
class name:             dcjs_medialoader
class type:             dynamic
version:                1.0
date:                   20110616
copyright:              massimo cardascia
url:                    www.plustic.de / www.dot-control.com
code style:		whitesmiths style variant - 8 spaces tab - http://en.wikipedia.org/wiki/indent_style
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
*/

///*	class definition /////////////////////////////////////////////////////////////////////////////////////
function DCJS_mediaLoader()
	{
///*    class versioning /////////////////////////////////////////////////////////////////////////////////////
	DCJS_mediaLoader.__s_classVersion = '5.00';		///h : dclib intern class version
	DCJS_mediaLoader.__s_className = 'DCJS_mediaLoader';	///h : dclib intern class name
	DCJS_mediaLoader.__s_classType = 'class';		///h : dclib intern class type

///*    class properties /////////////////////////////////////////////////////////////////////////////////////
	this.__dat_media = null;				///h : stores data for media loading
	this.__i_mediaID = 0;					///h : stores media id index
	this.__b_active = false;				///h : stores media loader active status
	this.__b_control_x = false;				///h : stores flag for checking x pos
	this.__b_control_y = true;				///h : stores flag for checking y pos
	this.__i_y_min = -210;					///h : stores y min value
	this.__i_media_toFade = 0;				///h : stores ammount of media to fade
	this.__i_uniqueIndex = 0;				///h : stores unique index
	this.__a_fade = [];					///h : stores array of elements to fade
	this.__b_loadingIcon = true;				///h : stores loading icon flag
	this.__b_background = true;				///h : stores background flag

///*    class system init ////////////////////////////////////////////////////////////////////////////////////
	DCJS_mediaLoader.f_initSystem = function()
		{
		}

///*    class constructor ////////////////////////////////////////////////////////////////////////////////////
	this.f_construct = function()
		{
		///h : init data
		this.f_initData();
		}

///*    public static functions //////////////////////////////////////////////////////////////////////////////

///*    public functions /////////////////////////////////////////////////////////////////////////////////////

///*	------------------------------------------------------------------------------------------------------
	/**
	 * reset all media
	 * all media elements are reseted or initiated if necessary.
	 *
	 * @access private
	 * @return void
	 *
	*/
	this.f_reset = function()
		{
		///h : properties
		var $i_loop = 0;					///h  : stores loop index
		var $i_elements = this.__dat_media.__i_elements;
		var $a_storage;
		var $storage_media;
		var $jqo_loading;
		var $jqo_background;
		var $s_width = '';
		var $s_height = '';
		var $jqo_media;

		///h : parse in all media
		$a_storage = this.__dat_media.f_getArray();

		///h : change source of all images
		for($i_loop = 0; $i_loop < $i_elements; $i_loop ++)
			{
			///h : load empty gif instead original source
			$storage_media = $a_storage[$i_loop];
			if($storage_media.b_init_complete == false)
				{
				///h : loading icon
				if(this.__b_loadingIcon == true)
					{
					$jqo_loading = jQuery('<div><img src="/files/dcLib_5/dcJS_5.0/DCJS_loading_w.gif" width="12px" height="12px"></div>');
					$jqo_loading.css({position:'absolute', top:'6px', left:'7px'});
					$jqo_loading.css('z-index', 5);
					$jqo_loading.appendTo(jQuery($storage_media.dom_media).parent());
					$storage_media.jqo_loading = $jqo_loading;
					}

				///h : init complete
				$storage_media.b_init_complete = true;
				}

			///h : reset
			this.f_showMedia($storage_media, false);
			$storage_media.b_onScreen = false;
			}

		///h : update media
		this.f_updateMedia();
		}

///*	------------------------------------------------------------------------------------------------------
	/**
	 * init all data later used in project. putting all data and easy to acces vars, arrays or
	 * data storages. interface to project backend if existing.
	 *
	 * @access private
	 * @return void
	 *
	*/
	this.f_addMedia = function($jqo_media)
		{
		///h : properties
		var $i_loop = 0;			///h  : stores loop index
		var $i_elements = $jqo_media.length;	///h  : stores loop index
		var $s_mediaID = '';
		var $dom_media;
		var $storage_media;
		var $s_url;

		///h : parse in all media
		for($i_loop = 0; $i_loop < $i_elements; $i_loop ++)
			{
			///h : get media id
			$dom_media = $jqo_media[$i_loop];
			$s_mediaID = jQuery($dom_media).attr('id');

			///h : get source from nosctipt
//			$s_url = jQuery($dom_media).parent().html();
			//$s_url = $s_url.substr($s_url.indexOf('_noscript') + 16);
			//$s_url = $s_url.substr(0, $s_url.indexOf('''));
			$s_url = DCJS_html.f_getClassVar(jQuery($dom_media), 's_url');

			///h : only add media with class var url prepared by dcphp_html
			if($s_url != null)
				{
				///h : assign unique media id if not assigned
				if($s_mediaID == '')
					{
					$s_mediaID = DCJS_html.f_getUniqueID();
					jQuery($dom_media).attr('id', $s_mediaID);
					}
				///h : create storage
				$storage_media = this.__dat_media.f_setData({s_mediaID:$s_mediaID});
				$storage_media.dom_media = $dom_media;
				$storage_media.s_url = $s_url;
				$storage_media.b_active = true;
				$storage_media.b_init_complete = false;
				$storage_media.b_visible = true;
				$storage_media.b_onScreen = false;
				$storage_media.b_load_start = false;
				$storage_media.b_load_complete = false;
				$storage_media.i_index = this.__i_uniqueIndex++;
				}
			}
		}

///*	------------------------------------------------------------------------------------------------------
	/**
	 * init all data later used in project. putting all data and easy to acces vars, arrays or
	 * data storages. interface to project backend if existing.
	 *
	 * @access private
	 * @return void
	 *
	*/
	this.f_start = function()
		{
		///h : properties
		var $i_loop = 0;					///h  : stores loop index
		var $i_elements = this.__dat_media.__i_elements;
		var $a_storage;
		var $storage_media;
		var $div_loading;
		var $o_base = this;

		///h : parse in all media
		$a_storage = this.__dat_media.f_getArray();

		///h : change source of all images
		this.f_reset();

		///h : update flag
		this.__b_active = true;

		///h : init engine
		jQuery(window).bind('resize', {o_base:this}, function($o_event){$o_event.data.o_base.f_updateMedia();});
		jQuery(window).bind('scroll', {o_base:this}, function($o_event){$o_event.data.o_base.f_updateMedia();});

		setInterval(function(){$o_base.f_updateMedia();}, 2000);
		this.f_updateMedia();
		}


///*    pivate static functions //////////////////////////////////////////////////////////////////////////////

///*    private functions ////////////////////////////////////////////////////////////////////////////////////

///*	------------------------------------------------------------------------------------------------------
	/**
	 * init all data later used in project. putting all data and easy to acces vars, arrays or
	 * data storages. interface to project backend if existing.
	 *
	 * @access private
	 * @return void
	 *
	*/
	this.f_showMedia_complete = function()
		{
		if(this.__i_media_toFade > 0)
			{
			this.__i_media_toFade --;
			}
		}

///*	------------------------------------------------------------------------------------------------------
	/**
	 * init all data later used in project. putting all data and easy to acces vars, arrays or
	 * data storages. interface to project backend if existing.
	 *
	 * @access private
	 * @return void
	 *
	*/
	this.f_showMedia = function($storage_media, $b_visible)
		{
		///h : properties
		var $o_base = this;

		//jQuery($storage_media.dom_media).
		if($storage_media.b_visible != $b_visible)
			{
			if($b_visible == true)
				{
				//this.__a_fade.push($storage_media.s_mediaID);
				jQuery($storage_media.dom_media).stop(true).delay(this.__i_media_toFade * 60).fadeTo(250, 1, function(){$o_base.f_showMedia_complete();});
				this.__i_media_toFade ++;
				}
			else
				{
				jQuery($storage_media.dom_media).stop(true).fadeTo(0, 0);
				this.f_showMedia_complete();

				}
			$storage_media.b_visible = $b_visible;
			}
		}

///*	------------------------------------------------------------------------------------------------------
	/**
	 * init all data later used in project. putting all data and easy to acces vars, arrays or
	 * data storages. interface to project backend if existing.
	 *
	 * @access private
	 * @return void
	 *
	*/
	this.f_loadMedia_complete = function($storage_media)
		{
		//
		//$storage_media.b_load_complete = true;
		//this.__i_mediaToLoad--;

		}

///*	------------------------------------------------------------------------------------------------------
	/**
	 * init all data later used in project. putting all data and easy to acces vars, arrays or
	 * data storages. interface to project backend if existing.
	 *
	 * @access private
	 * @return void
	 *
	*/
	this.f_loadMedia = function($storage_media)
		{
		///h : check load status
		if($storage_media.b_load_start == true)
			{
			return;
			}

		///h : load media
		$storage_media.b_load_start = true;
		jQuery($storage_media.dom_media).bind('load', {o_base:this, storage_media:$storage_media}, function($o_event){$o_event.data.o_base.f_loadMedia_complete($o_event.data.storage_media);});
		jQuery($storage_media.dom_media).attr('src', $storage_media.s_url);
		}


///*	------------------------------------------------------------------------------------------------------
	/**
	 * init all data later used in project. putting all data and easy to acces vars, arrays or
	 * data storages. interface to project backend if existing.
	 *
	 * @access private
	 * @return void
	 *
	*/
	this.f_updateMedia = function()
		{
		///h : properties
		var $i_loop = 0;					///h  : stores loop index
		var $i_elements = this.__dat_media.__i_elements;
		var $a_storage;
		var $storage_media;
		var $i_x;
		var $i_height;
		var $i_screenWidth;
		var $i_screenHeight;
		var $jqo_media;
		var $b_onScreen = false;

		///h : get data
		$i_screenWidth = jQuery(window).width();
		$i_screenHeight = jQuery(window).height();
		$i_body_x = jQuery('body').viewportOffset().left;
		$i_body_y = jQuery('body').viewportOffset().top;

		///h : check if media loader still active
		if(this.__b_active == false)
			{
			return;
			}

		///h : parse in all media
		$a_storage = this.__dat_media.f_getArray();

		///h : change source of all images
		for($i_loop = 0; $i_loop < $i_elements; $i_loop ++)
			{
			///h : get releavant media storage
			$storage_media = $a_storage[$i_loop];
			$b_onScreen = false;

			///h : check if relevant media active and on screen
			if(($storage_media.b_active == true) && ($storage_media.b_init_complete == true))
				{
				$ojq_base = jQuery($storage_media.dom_media);
				if(this.__b_control_x == true)
					{
					///h : get body y pos
					$i_base_y = $ojq_base.viewportOffset().left;
					if($i_base_x > this.__i_x_min)
						{
						if($i_base_x < $i_screenWidth)
							{
							$b_onScreen = true;
							}
						}
					}
				if(this.__b_control_y == true)
					{
					///h : get body y pos
					$i_base_y = $ojq_base.viewportOffset().top;
					if($i_base_y > this.__i_y_min)
						{
						if($i_base_y < $i_screenHeight)
							{
							$b_onScreen = true;
							}
						}
					}
				if($ojq_base.css('visibility') == 'hidden')
					{
					$b_onScreen = false;
					}

				if($b_onScreen != $storage_media.b_onScreen)
					{
					$storage_media.b_onScreen = $b_onScreen;
					if($b_onScreen == true)
						{
						if($storage_media.b_load_start == false)
							{
							this.f_loadMedia($storage_media);
							}
						this.f_showMedia($storage_media, true);
						}
					}
				}
			}
		}
		

///*	------------------------------------------------------------------------------------------------------
	/**
	 * init all data later used in project. putting all data and easy to acces vars, arrays or
	 * data storages. interface to project backend if existing.
	 *
	 * @access private
	 * @return void
	 *
	*/
	this.f_initData = function()
		{
		///h : create data storage
		this.__dat_media = new DCJS_dataStorage({s_uniqueSearchKey:'s_mediaID'});
		}

	///h : comstruct
	this.f_construct();
        }

///*    direct execution /////////////////////////////////////////////////////////////////////////////////////
//DCJS_mediaLoader();
//DCJS_mediaLoader.f_initSystem();

/*
--------------------------------------------------------------------------------------------------------------
description:
--------------------------------------------------------------------------------------------------------------
project is the main class of the dclib php framework.
project store all the main functions and is used as global wrappert to hold important functions and vars.
--------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------
events:
--------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------
getter properties:
--------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------
usage:
--------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------
*/
