/*
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
code language:		javascript 1.8
folder name:            files/dclib_5.05/dcjs_5.0
class name:             dcjs_medialoader
class type:             dynamic
version:                1.0
date:                   20120108
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(){this.f_construct();}

///*    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 /////////////////////////////////////////////////////////////////////////////////////
	DCJS_mediaLoader.prototype.__dat_media = null;				///h : stores data for media loading
	DCJS_mediaLoader.prototype.__i_mediaID = 0;				///h : stores media id index
	DCJS_mediaLoader.prototype.__b_active = false;				///h : stores media loader active status

	DCJS_mediaLoader.prototype.__b_control_x = false;			///h : stores flag for checking x pos
	DCJS_mediaLoader.prototype.__b_control_y = true;			///h : stores flag for checking y pos
	DCJS_mediaLoader.prototype.__i_y_min = -210;				///h : stores y min value
	DCJS_mediaLoader.prototype.__i_uniqueIndex = 0;				///h : stores unique index
	DCJS_mediaLoader.prototype.__a_fade = [];				///h : stores array of elements to fade
	DCJS_mediaLoader.prototype.__b_loadingIcon = true;			///h : stores loading icon flag
	DCJS_mediaLoader.prototype.__b_background = true;			///h : stores background flag
	DCJS_mediaLoader.prototype.__i_media_elements_toLoad = 0;
	DCJS_mediaLoader.prototype.__a_media_elements_toFade = [];
	DCJS_mediaLoader.prototype.__i_media_elements_toShow = 0;
	DCJS_mediaLoader.prototype.__i_media_elements_toShow_max = 6;
	DCJS_mediaLoader.prototype.__i_media_elements_toLoad_max = 200;
	DCJS_mediaLoader.prototype.__b_showing = false;
	DCJS_mediaLoader.prototype.__i_showing = -1;

///*    class system init ////////////////////////////////////////////////////////////////////////////////////
	DCJS_mediaLoader.f_initSystem = function()
		{
		}

///*    class constructor ////////////////////////////////////////////////////////////////////////////////////
	DCJS_mediaLoader.prototype.f_construct = function()
		{
		///h : init data
		DCJS_mediaLoader.prototype.f_initData();
		}

///*    public static functions //////////////////////////////////////////////////////////////////////////////

///*    public 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
	 *
	*/
	DCJS_mediaLoader.prototype.f_start = function($jqo_base)
		{
		///h : properties
		var $o_base = this;

		///h : check if just active
		if(this.__b_active == true)
			{
			this.f_reset();
			return;
			}

		///h : update flag
		this.__b_active = true;

		///h : update media
		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();});
		this.f_updateMedia();

		setInterval(function(){$o_base.f_updateMedia();}, 1200);
		}

///*	------------------------------------------------------------------------------------------------------
	/**
	 * 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
	 *
	*/
	DCJS_mediaLoader.prototype.f_reset = function($b_stop)
		{
		///h : properties
		var $i_loop = -1;			///h  : stores loop index
		var $i_elements = -1;			///h  : stores loop index
		var $jqo_media;
		var $jqo_media_element;
		var $s_url;
		var $a_storage;
		var $storage_media = null;
		
		///h : check transmitted values
		$a_storage = this.__dat_media.f_getArray();
		$i_elements = $a_storage.length;
		this.__i_media_elements_toLoad = 0;
		this.__a_media_elements_toFade = [];
		this.__i_media_elements_toShow = 0;

		///h : parse in all media
		for($i_loop = 0; $i_loop < $i_elements; $i_loop ++)
			{
			///h : hide
			$storage_media = $a_storage[$i_loop];
			$jqo_media_element = $storage_media.jqo_media_element;
			$jqo_media_element.css({'display':'none'});
			$jqo_media_element.stop().fadeTo(0, 0);

			///h : update storage
			$storage_media.b_active = true;
			$storage_media.b_onScreen = false;		///h : stores if image is on screen
			$storage_media.b_show_start = false;
			$storage_media.b_show_complete = false;
			$storage_media.b_load_start = false;
			$storage_media.b_load_complete = false;
			$storage_media.b_init_complete = true;
			if($storage_media.jqo_media_element_image)
				{
				$storage_media.jqo_media_element_image.unbind('load');
				}
			}
		
		if($b_stop == true)
			{
			this.__b_active = false;
			}
		}

///*	------------------------------------------------------------------------------------------------------
	/**
	 * 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
	 *
	*/
	DCJS_mediaLoader.prototype.f_addMedia = function($jqo_base)
		{
		///h : properties
		var $i_loop = -1;			///h  : stores loop index
		var $i_elements = -1;			///h  : stores loop index
		var $jqo_media;
		var $jqo_media_element;
		var $s_url;

		///h : check transmitted values
		$jqo_base = (($jqo_base == null) ? jQuery('html') : $jqo_base);
		$jqo_media = jQuery($jqo_base).find('.dcphp_mediaLoader');
		$i_elements = $jqo_media.length;

		///h : parse in all media
		for($i_loop = 0; $i_loop < $i_elements; $i_loop ++)
			{
			///h : get media id
			$jqo_media_element = jQuery($jqo_media[$i_loop]);
			$s_mediaID = jQuery($jqo_media_element).attr('id');
			$storage_media = this.__dat_media.f_setData({s_mediaID:$s_mediaID});
			if($storage_media.b_init_complete != true)
				{
				///h : get source from nosctipt
				$s_url = DCJS_html.f_getClassVar($jqo_media_element, 's_url');

				///h : init storage
				$storage_media.i_index = this.__i_uniqueIndex++;
				$storage_media.s_url = $s_url;
				$storage_media.jqo_media_element = $jqo_media_element;
				$storage_media.jqo_media_element_parent = $jqo_media_element.parent();

				///h : clear noscript html part
				$jqo_media_element.html('');
				}
			}

		///h : reset
		this.f_reset();
		}

///*    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
	 *
	*/
	DCJS_mediaLoader.prototype.f_loadMedia_complete = function($storage_media)
		{
		$storage_media.jqo_media_element.stop().fadeTo(0, 1);
		$storage_media.jqo_media_element_image.unbind('load');
		$storage_media.b_load_complete = true;
		this.__i_media_elements_toLoad --;
		}

///*	------------------------------------------------------------------------------------------------------
	/**
	 * 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
	 *
	*/
	DCJS_mediaLoader.prototype.f_loadMedia = function($storage_media)
		{
		///h : check load status
		if($storage_media.b_load_start == true)
			{
			return;
			}

		///h : load media
		this.__i_media_elements_toLoad ++;
		$storage_media.b_load_start = true;
		$storage_media.jqo_media_element.html('<img src="' + $storage_media.s_url + '" alt=" " />');
		$storage_media.jqo_media_element_image = jQuery($storage_media.jqo_media_element.find('img'));
		$storage_media.jqo_media_element_image.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);});
		}

///*	------------------------------------------------------------------------------------------------------
	/**
	 * 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
	 *
	*/
	DCJS_mediaLoader.prototype.f_updateMedia = function()
		{
		///h : properties
		var $i_loop = 0;					///h  : stores loop index
		var $a_storage;
		var $storage_media;
		var $i_x;
		var $i_height;
		var $i_screenWidth;
		var $i_screenHeight;
		var $jqo_media_element;
		var $jqo_media_element_parent;
		var $b_onScreen = false;

		///h : get data
		$i_screenWidth = (jQuery(window).width() + 100);
		$i_screenHeight = (jQuery(window).height() + 100);
		$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 < this.__dat_media.__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_load_start == false))
				{
				$jqo_media_element = $storage_media.jqo_media_element;
				$jqo_media_element_parent = $storage_media.jqo_media_element_parent;
				
				if(this.__b_control_x == true)
					{
					///h : get body y pos
					$i_base_x = $jqo_media_element_parent.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 = $jqo_media_element_parent.viewportOffset().top;

					if($i_base_y > this.__i_y_min)
						{
						if($i_base_y < $i_screenHeight)
							{
							$b_onScreen = true;
							}
						}
					}
					
				///h : check parent element
				if($jqo_media_element_parent.css('display') == 'none')
					{
					$b_onScreen = false;
					}

				if($b_onScreen == true)
					{
					///h : load image
					if(($storage_media.b_load_start == false) && (this.__i_media_elements_toLoad < this.__i_media_elements_toLoad_max))
						{
						$storage_media.b_onScreen = $b_onScreen;
						this.f_loadMedia($storage_media);
						}
					}
				}
			}
		}

///*	------------------------------------------------------------------------------------------------------
	/**
	 * 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
	 *
	*/
	DCJS_mediaLoader.prototype.f_initData = function()
		{
		///h : create data storage
		this.__dat_media = new DCJS_dataStorage({s_uniqueSearchKey:'s_mediaID'});
		}

///*    init class ///////////////////////////////////////////////////////////////////////////////////////////

/*
--------------------------------------------------------------------------------------------------------------
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:
--------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------
*/
