/**
 * Scroller implementation for "Work view" page 
 */
function Scroller_Work ()
{
	this.container = $('.work .container');
	this.content   = $('.content', this.container);
	this.elements  = $('div', this.content);
	this.element_cnt = 0;
	this.preloaded = 0;
	this.cur_element = 0;
	
	this.overlay = null;
	
	this.scroller  = new Scroller();
	this.preloadContent();
};
/**
 * Preloads images, which are not yet visible
 */
Scroller_Work.prototype.preloadContent = function () {
	var _self = this;
	var img = $('div img', this.content).load(Scroller_Work_preload_handler).error(Scroller_Work_preload_handler);
	var img_count = img.length;
	
	for(var i = 0; i < img_count; i++)
	{
		if (img[i].complete)
		{
			this.preloaded++;
			$(img[i]).unbind('load').unbind('error');
		} else {
			img[i].oncomplete = Scroller_Work_preload_handler;
		}
	}
	
	if (this.preloaded >= img_count)
	{
		$('div img', _self.content).unbind('load').unbind('error');
		_self._init();
	}
	
	function Scroller_Work_preload_handler() {
		_self.preloaded++;
		if (_self.preloaded == img_count)
		{
			$('div img', _self.content).unbind('load').unbind('error');
			_self._init();
		}
		$(this).unbind('load').unbind('error');
	}
};
/**
 * Checks if left and right navigatoin arrows should be visible and
 * shows/hides them 
 */
Scroller_Work.prototype._redrawArrows = function () {
		if (this.elements.eq(this.cur_element).hasClass('swf-hide-nav')) {
			$('.work .arrows .next').addClass('invisible');
			$('.work .arrows .prev').addClass('invisible');
			return;
		}
		
		if (this.cur_element === this.element_cnt - 1) {
			$('.work .arrows .next').addClass('invisible');
		}else{
			$('.work .arrows .next').removeClass('invisible');
		}
		
		if (this.cur_element === 0) {
			$('.work .arrows .prev').addClass('invisible');
		}else{
			$('.work .arrows .prev').removeClass('invisible');
		}
};
/**
 * Class constructor, executed automatically
 */
Scroller_Work.prototype._init = function () {
	var _self = this;

	//Adds empty element to the end of the list
	$("<div><img src=\"/img/px.gif\" width=\"630px\" height=\"1px\" alt=\"\" /></div>").appendTo(this.content);
	
	this.elements  = $('div', this.content);
	this.element_cnt = this.elements.length - 1;
	
	this.scroller.refresh(this.container, this.content, this.elements);
	
	//Images
	this.elements.each(function(i){
		if (i != _self.elements.length - 1)
		{
			this.scroller_index = i;
			$(this).click(function () {
				_self.scroller.scrollToElement(this.scroller_index);
				_self.cur_element = this.scroller_index;
				_self._onChange();
				_self._redrawArrows();
			});
		}
	});
	
	//Right navigation arrow
	$('.work .arrows .next').click(function() {
		_self.scrollNext();
		_self._redrawArrows();
		return false;
	});
	
	//Left navigation arrow
	$('.work .arrows .prev').click(function() {
		_self.scrollPrevious();
		_self._redrawArrows();
		return false;
	});
	
	if (this.element_cnt <= 1)
	{
		$('.work .arrows .next').addClass('invisible');
	}
	
	var h = $(this.elements[0]).height();
	var p = Math.floor(h * 0.4);
	var h2 = h - p;
	
	if (jQuery.browser.msie && jQuery.browser.version <= 6)
	{
		h = h - 3;
		h2 = h2 - 3;
	}
	
	/* Overlay */
	this.overlay = $('.work .content_overlay');
	this.overlay.css({width: $(this.elements[0]).width() - 1 + 'px', height: h2 + 'px', marginTop: - h + 'px', paddingTop: p + 'px', opacity: 0.01});
	
	this.overlay.mouseover(function () {
		if ($('a', this)[0])
		{
			if ($('a', this)[0].href) {
				$(this).stop();
				$(this).animate({opacity: 0.75}, 300);
			}
		}
	});
	this.overlay.mouseout(function () {
		$(this).stop();
		$(this).animate({opacity: 0.01}, 300);
	});
	
	this._onChange();
};
/**
 * Changes link (which is visible, when mouse hovers image) title and uri 
 * @access protected
 */
Scroller_Work.prototype._onChange = function () {
	//var info = $('img', this.elements[this.cur_element]).attr('alt') || '';
	//var info = info.split('|');
	//var a_tag = $('.work .content_overlay a');
	
	//a_tag.attr('href', info[0]);
	//a_tag.html(info[1]);
	
	if (this.elements.eq(this.cur_element).hasClass('swf-hide-overlay')) {
		this.overlay.hide();
	} else {
		this.overlay.show();
	}
};
/**
 * Scrolls content to next item
 */
Scroller_Work.prototype.scrollNext = function () {
	var new_el = this.cur_element + 1;
	
	if (new_el >= this.elements.length - 1)
	{
		new_el = this.elements.length - 2;
	}
	if (new_el < 0)
	{
		new_el = 0;
	}
	
	if (new_el != this.cur_element)
	{
		this.cur_element = new_el;
		this.scroller.scrollToElement(new_el);
		this._onChange();
		this._redrawArrows();
	}
};
/**
 * Scrolls content to previous item
 */
Scroller_Work.prototype.scrollPrevious = function () {
	var new_el = this.cur_element - 1;
	
	if (new_el < 0)
	{
		new_el = 0;
	}
	
	if (new_el != this.cur_element)
	{
		this.cur_element = new_el;
		this.scroller.scrollToElement(new_el);
		this._onChange();
		this._redrawArrows();
	}
};

/* Creates class instance */
var sw = null;
$(document).ready(function(){
	sw = new Scroller_Work();
});