/**
 * UNavigator (UniNvigator based on SWFObject and UniUploader)
 * 
 * Author and idea: Sitnikov Igor (sitnikov@kpmedia.ua)
 * Copyright (C) 2008, Dark Side (Bigmir)NET)
 */

function getURLParam( strHref, strParamName ) {
	var strHref = strHref || window.location.href;
	var strReturn = "";
	if( strHref.indexOf("?") > -1 ) {
		var strQueryString = strHref.substr(strHref.indexOf("?")).toLowerCase();
		var aQueryString = strQueryString.split("&");
		for( var iParam = 0; iParam < aQueryString.length; iParam++ ) {
			if(	aQueryString[iParam].indexOf(strParamName + "=") > -1  ) {
				var aParam = aQueryString[iParam].split("=");
				strReturn = aParam[1];
				break;
			}
		}
	}
	return strReturn;
}

function UNavigator ( config ) {
	var that = this;
	this.cache = { 
		next		: {}, 
		prev		: {},
		links		: {}
	}
	this.current	= config.current		||	0;		// текущий элемент массива content
	this.type		= config.type			||	null;	// тип навигатора (1 - Видео, 2 - Галерея)
	this.layer		= $(config.layer)		||	null;	// базовый элемент в который показываем что-нибудь ))
	this.content	= config.content		||	{};		// набор элементов
	this.nextLayer	= $(config.nextLayer)	||	null;	// элемент со ссылкой вперёд
	this.prevLayer	= $(config.prevLayer)	||	null;	// элемент со ссылкой назад
	this.tabsLayer	= $(config.tabsLayer)	||	null;	// элемент с табами
	this.descLayer	= $(config.descLayer)	||	null;	// элемент с описанием ко всему навигатору
	this.cDescLayer	= $(config.cDescLayer)	||	null;	// элемент с описанием к текущему элементу
	this.basePath	= config.basePath		||	null;
	this.onTab		= config.onTab			||	9;
	this.count		= config.count			||	1;
	
	this.isVideo 	= function() { return this.type === 1 ? true : false; };
	this.isPhoto 	= function() { return this.type === 2 ? true : false; };
	
	this.phrases	= {
		'next' : 'Следующая&nbsp;&rarr;',
		'prev' : '&larr;&nbsp;Предыдущая'
	}
	this.lr			= Math.floor( this.onTab / 2 );
	
	if( this.isVideo() ) {
		this.swf = config.swf || null;
		this.cache.swfLink = this.swf.attributes.swf;
	}
	
	if( this.isPhoto() ) {
		this.layer.style.background = 'url("http://k.img.com.ua/static/img/icon/cr/ajax-loader.gif") no-repeat 50% 50% ';
		var img = this.layer.getElementsByTagName('img');
		if( img[0] ) {
			this.swf = img[0];
			this.cache.swfLink = img[0].parentNode;
			this.cache.swfLink.onclick = function() {
				that.next();
				return false;
			}
			if( !this.cache[this.current] ) {
				if( this.swf.naturalWidth == 1 && this.swf.naturalHeight == 1 ) {
					alert('Изображение не найдено');
				} else {
					this.cache[this.current] = new Image();
					this.cache[this.current].src = this.swf.src;
				}
			}
		}
	}
		
	this.next = function () { return this.show( this.current + 1 ) };
	this.prev = function () { return this.show( this.current - 1 ) };
	this.checkCache = function() {
		if( this.cache[this.current].complete == true ) {
			if( this.isPhoto() ) {
				this.swf.src = this.cache[this.current].src;
				this.vToggle( this.swf, true );
				this.vToggle( this.nextLayer, true );
				this.vToggle( this.prevLayer, true );
				this.tabToggle( true );
				if( typeof this.content[this.current].description != "undefined" ) {
					var descr = $('decr');
					if( descr !== null ) {
						descr.innerHTML = this.content[this.current].description;
					}
				}
			}
			if( this.isVideo() ) {
				this.vToggle( this.swf, true );
				this.vToggle( this.nextLayer, true );
				this.vToggle( this.prevLayer, true );
				this.tabToggle( true );
				if( this.content[this.current].imageSrc && this.content[this.current].videoSrc ) {
					if( this.cache[this.current].width == 1 ) {
						this.swf.attributes.swf = 'http://www.krastv.ru/static/img/iptv-stream.swf';
						this.swf.params.scale = 'scale';
						this.swf.write(this.layer);	
					} else {
						this.swf.addVariable("img_url", this.cache[this.current].src);
						this.swf.addVariable("movie_url", this.content[this.current].videoSrc);
					}
					this.swf.write(this.layer);
				}	
			}
			return true;
		} else {
			setTimeout( function() {
				if( that.isPhoto() ) {
					that.vToggle( that.swf, false );
				}
				that.vToggle( that.nextLayer, false );
				that.vToggle( that.prevLayer, false );
				that.tabToggle( false );
				that.checkCache();
				return false;
			}, 100 );
		}
	}
	this.checkTab = function() {
		this.start = 0;
		this.end = this.count - 1;
		if( this.count > this.onTab ) {
			this.start	= ( this.current >= ( this.lr + 1 ) && ( this.current - this.lr ) >= 0 ) ? ( this.current - this.lr ) : 0;
			this.end = this.start + this.onTab - 1;
			if( this.start > 0 ) {
				this.start = ( ( this.end - this.onTab ) ) + 1;
				if( ( this.current + this.lr ) > this.count - 2  ) {
					this.start = ( ( this.count - 1 - this.onTab ) );	
				}
			}
		}
		for( var i = this.start; i <= this.end; i++ ) {
			if( typeof this.cache.links[i] == "object" ) {
				this.iToggle( this.cache.links[i], true );
				this.vToggle( this.cache.links[i], true );
			} else {
				if( typeof this.content[i] != "undefined" ) {
					try {
						this.cache.links[i] = this.make( 'link', i, this.tabsLayer );
					} catch( e ) { ; }
					var eLink = this.cache.links[i];
					if( this.current === i ) {
						eLink.className = 'current gray';
					} else {
						eLink.className = 'list';
					}
					Attach_EventListener( eLink, 'click', function( e ) {
						var src = isBrowsMSIE ? e.srcElement : this;
						return that.show( parseInt( src.getAttribute('id').replace( that.tabsLayer.id, '' ) ) );
					} );
					this.iToggle( eLink, true );
				}
			}
		}
		for( var i = 0; i < ( this.start ); i++ ) {
			if( typeof this.cache.links[i] != "undefined" )
				this.iToggle( this.cache.links[i], false );
		}
		for( var i = ( this.end + 1 ); i <= ( this.count - 1 ); i++ ) {
			if( typeof this.cache.links[i] != "undefined" )
				this.iToggle( this.cache.links[i], false );
		}
	}
	this.show = function ( id ) {
		if( this.content[id] ) {
			$(this.tabsLayer.id.toString() + this.current).className = 'list';
			this.current = id;
			$(this.tabsLayer.id.toString() + this.current).className = 'current gray';
			this.checkTab();
			if( typeof this.content[this.current + 1] == 'object' ) {
				this.toggle( this.cache.next, true );
			} else {
				this.toggle( this.cache.next, false );
			}
			if( typeof this.content[this.current - 1] == 'object' ) {
				this.toggle( this.cache.prev, true );
			} else {
				this.toggle( this.cache.prev, false );
			}	
			if( !this.cache[this.current] ) {					
				this.cache[this.current] = new Image();	
				if( this.isVideo() ) {
					this.cache[this.current].src = this.content[this.current].imageSrc;					
				}
				if( this.isPhoto() ) {	
					if( typeof this.content[this.current].src == 'undefined' ) {
						this.content[this.current].src = this.basePath.toString() + this.content[this.current].image;
					}
					this.cache[this.current].src = this.content[this.current].src.replace( '_tn', '' );
				}
			}
			this.checkCache();
/*!!!*/		if (typeof(banner_rotate) == "function") banner_rotate(); // explicitly rotate banners for every foto view
		}
		return false;	
	}
	
	this.iToggle = function( object, type ) {
		if( type === true ) {
			object.style.display = 'inline';
		} else if( type === false ) {
			object.style.display = 'none';
		}
	}
	
	this.toggle = function( object, type ) {
		if( type === true ) {
			object.link.style.display = 'block';
			object.span.style.display = 'none';
		} else if( type === false ) {
			object.link.style.display = 'none';
			object.span.style.display = 'block';				
		}
	}
	
	this.vToggle = function( object, type ) {
		try{
			if( type === true ) {
				object.style.visibility = 'visible';
			} else if( type === false ) {
				object.style.visibility = 'hidden';
			}
		} catch (e) { ; }
	}
	
	this.tabToggle = function( type ) {
		for( i in this.cache.links ) {
			if( i != this.current ) {
				this.vToggle( this.cache.links[i], type );
			}
		}
	}
	
	this.make = function( sType, sText, toLayer ) {
		if( sType === null ) return false;
		if( sText === null ) return false;
		if( typeof toLayer == 'undefined' ) return false;
		var tagName = ( sType == 'link' ) ? 'a' : 'span';
		var created = document.createElement( tagName );
		created.className = ( sType == 'link' ) ? '' : 'gray';
		created.style.display = 'none';
		if( typeof this.phrases[sText] != 'undefined' ) {
			created.id = ( toLayer.id.toString() + '_' + sType );
			created.innerHTML = this.phrases[sText];
			created.style.cursor = 'pointer';
			if( created.tagName == 'a' ) {
				created.href = '#';
				created.style.cursor = 'pointer';
				Attach_EventListener( created, 'click', function (){
					return that[sType]();
				}, true );
			}
		} else {
			created.href = '#';
			created.style.cursor = 'pointer';
			created.id = ( toLayer.id.toString() + sText );
			created.innerHTML = sText + 1;
		}
		toLayer.appendChild( created );
		return created;
	}
	
	for ( key in this.phrases ) {
		if( typeof this[key + 'Layer'] == 'object' ) {
			var layer = this[key + 'Layer'];
			if( layer !== null ) {
				this.cache[key] = {
					span		: $(layer.id + '_span') || this.make( 'span', key, layer ),
					link		: $(layer.id + '_link') || this.make( 'link', key, layer )
				}
				if( !this.cache[key].id )
					this.cache[key].link.onclick = function() {
						return that[key]();
					}
			}
		}
	}
	
	if( this.tabsLayer !== null ) {
		this.cache.links = this.tabsLayer.getElementsByTagName( 'a' );
		for( i in this.cache.links ) {
			this.cache.links[i].onclick = function() {
				return that.show( parseInt( getURLParam( this.href, ( that.isVideo() ? 'vid' : 'img' ) ) ) );
			}
		}
	}
	if( typeof this.cache.next.link != "undefined" )
		this.cache.next.link.onclick = function() {
			return that.next();
		}
	if( typeof this.cache.prev.link != "undefined" )
		this.cache.prev.link.onclick = function() {
			return that.prev();
		}
}