/*
@Author: Забродин Д.В.
@Name: Плавающие меню.
@Date: 18.02.2009
@Version: 1.03
*/

ScrollPhotoShow = Class.create();
ScrollPhotoShowN2 = Class.create();
PhotoItem = Class.create();
PhotoPreview = Class.create();

ScrollPhotoShow.prototype = {
	initialize: function(htmlPrefix)
	{
		this.HtmlPrefix = htmlPrefix;
		this.BlockHtml = this.HtmlPrefix + "-block";
		this.PreviewImg = new PhotoPreview(this);
		this.TickInterval = null;
		this.TickClock = 0;
		this.Step = 5;
		
		countPhoto = 0;
		$(this.BlockHtml).select('img').each(this.eachImgTag.bindAsEventListener(this));
		this.MaxLeftScroll = 700 - (121 * countPhoto);
		this.MaxLeftScroll = (this.MaxLeftScroll > 0) ? 0 : this.MaxLeftScroll;
		
		Event.observe($(this.HtmlPrefix + "-left"), 'mouseover', this.mouseoverRight.bindAsEventListener(this));
		Event.observe($(this.HtmlPrefix + "-left"), 'mouseout', this.mouseoutRight.bindAsEventListener(this));
		Event.observe($(this.HtmlPrefix + "-right"), 'mouseover', this.mouseoverLeft.bindAsEventListener(this));
		Event.observe($(this.HtmlPrefix + "-right"), 'mouseout', this.mouseoutLeft.bindAsEventListener(this));
		
		this.testCookie();
	},
	
	eachImgTag: function(elem)
	{
        new PhotoItem(elem, this);
        countPhoto++;
	},
	
	mouseoverLeft: function() 
	{
	    this.PreviewImg.hide();
	    this.TickInterval = setInterval(this.moveLeft.bindAsEventListener(this), 1);
	},
	
	mouseoutLeft: function() 
	{
	    clearInterval(this.TickInterval);
	    if (this.testCatalogPage()) {
	        this.setCookie();
	    }
	},
	
	mouseoverRight: function() 
	{
	    this.PreviewImg.hide();
	    this.TickInterval = setInterval(this.moveRight.bindAsEventListener(this), 1);
	},

	mouseoutRight: function() 
	{
	    clearInterval(this.TickInterval);
	    if (this.testCatalogPage()) {
	        this.setCookie();
	    }
	},
	
	moveLeft: function()
	{
	    var leftpx = $(this.BlockHtml).style.left;
	    leftpx = leftpx.gsub('px', '');
	    
	    if (this.MaxLeftScroll >= leftpx) {return}
	    
	    leftpx -= this.Step;
	    $(this.BlockHtml).style.left = leftpx + "px";
	},
	
	moveRight: function()
	{
	    var leftpx = $(this.BlockHtml).style.left;
	    leftpx = leftpx.gsub('px', '');
	    if ((leftpx * 1) >= 0) {clearInterval(this.TickInterval);return;}
	    leftpx = (leftpx * 1) + this.Step;
	    $(this.BlockHtml).style.left = leftpx + "px";
	},
	
	testCatalogPage: function()
	{
	    url = document.location.href;
	    url = url.toLowerCase();
   
	    if (url.indexOf("catalog") != -1 || url.indexOf("newsphoto") != -1 || url.indexOf("portfolio") != -1 || url.indexOf("hiddencam") != -1) {
	        return true;
	    }
	    return false;
	},
	
	getUrlTypeParam: function()
	{
	    url = document.location.href;
	    url = url.toLowerCase();
	    
	    if (url.indexOf("newsphoto") != -1 || url.indexOf("portfolio") != -1 || url.indexOf("hiddencam") != -1) {
	        return 1;
	    }
	    
	    params = url.split("?");
	    if (params.length > 1) {
	        qparams = params[1].toQueryParams();
	        type = qparams["type"] 
	    } else {
	        type = 1;
	    }
	    
	    return type;
	},
	
	setCookie: function()
	{
	    var leftpx = $(this.BlockHtml).style.left;
	    leftpx = leftpx.gsub('px', '');
	    
	    cookiename = this.HtmlPrefix + "-" + this.getUrlTypeParam();
	    cookievalue = leftpx;
	    document.cookie = cookiename + "=" + cookievalue + ";";
	},
	
	testCookie: function()
	{
	    if (this.testCatalogPage()) {	    
	        var cookies = document.cookie.split( ';' );
	        for (j = 0; j < cookies.length; j++) {
	            cookie = cookies[j].split('=');
	            cookiename = cookie[0];
	            cookievalue = cookie[1];
	            cookiename = cookiename.replace(/^\s+|\s+$/g, '');
    	                
	            if (cookiename == this.HtmlPrefix + "-" + this.getUrlTypeParam()) {
	                this.moveToPos(cookievalue);
	            }
	        }
        }
	},
	
	moveToPos: function(leftpx)
	{
	    if (this.MaxLeftScroll >= leftpx) {
	        $(this.BlockHtml).style.left = this.MaxLeftScroll + "px";   
	    } else {
	        $(this.BlockHtml).style.left = leftpx + "px";
	    }	    
	}
};

ScrollPhotoShowN2.prototype = {
	initialize: function(htmlPrefix)
	{
		n1 = new ScrollPhotoShow(htmlPrefix);
	
		countPhoto = 0;
		$(n1.BlockHtml).select('img').each(this.eachImgTag.bindAsEventListener(this));
		n1.MaxLeftScroll = 645 - (135 * countPhoto);
		n1.MaxLeftScroll = (n1.MaxLeftScroll > 0) ? 0 : n1.MaxLeftScroll;
	},
	
	eachImgTag: function(elem)
	{
        //new PhotoItem(elem, this);
        countPhoto++;
	}
};

PhotoItem.prototype = {
	initialize: function(htmlPhoto, parent)
	{
	    this.Parent = parent;
	    this.Status = 0;
		this.HtmlElement = htmlPhoto;
		this.LeftPos = this.HtmlElement.parentNode.parentNode.offsetLeft;
		this.BlockHtml = $(this.HtmlPrefix + "-block");
		var src = this.HtmlElement.src;
	    src = src.gsub('small', 'pre');
		this.PreviewImageObj = new Image();
		this.PreviewImageObj.src = src;
	    
	    Event.observe(this.HtmlElement, 'mouseover', this.mouseover.bindAsEventListener(this));
	    Event.observe(this.HtmlElement, 'mouseout', this.mouseout.bindAsEventListener(this));
	},
	
	mouseover: function()
	{
		return;
	    this.Parent.PreviewImg.setLeft(this.LeftPos);
	    this.Parent.PreviewImg.setImage(this.PreviewImageObj);
        this.Parent.PreviewImg.setHref(this.HtmlElement.parentNode.href);
        this.Parent.PreviewImg.display();
	},
	
	mouseout: function()
	{
	    this.Parent.PreviewImg.hide();
	}
};

PhotoPreview.prototype = {
	initialize: function(parent)
	{
	     this.Parent = parent;
	     this.Element = $(parent.HtmlPrefix + "-previewimg");
	     this.ParentHtmlBlock = $(parent.HtmlPrefix + "-preview");
	     this.BlockHtml = $(this.Parent.HtmlPrefix + "-block");
	     this.OrigLeft = 0;
	     this.Timer = null;
	     this.FadeTimer = null;
	     this.TickSize = 1;
	     this.TickClock = 0;
	     this.MaxTickCount = 50;
	     this.SameImage = false;
	     
	     this.Element.style.top = "-225px";
	     
	     Event.observe(this.Element, 'mouseover', this.mouseover.bindAsEventListener(this));
	     Event.observe(this.Element, 'mouseout', this.hide.bindAsEventListener(this));
	},
	
	mouseover: function()
	{
	    clearTimeout(this.FadeTimer);
	},
	
    hide: function()
	{
	    this.FadeTimer = setTimeout(this.fade.bindAsEventListener(this), this.TickSize);
	},
	
	fade: function()
	{
	    clearTimeout(this.FadeTimer);
	    this.ParentHtmlBlock.style.display = "none";
	    this.Element.src = "";
	},
	
	setLeft: function(left)
	{
	    this.OrigLeft = left;
	},
	
	setImage: function(image)
	{
	    this.SameImage = (this.Element.src == image.src) ? true : false;
	    
	    this.Element.src = image.src;
        scrollrel = this.BlockHtml.style.left;
	    scrollrel = scrollrel.gsub('px', '');
	    leftpx1 = this.OrigLeft + (scrollrel*1) + 97;
	    leftpx2 = this.OrigLeft + (scrollrel*1) + 97 - image.width;
	    leftpx3 = this.OrigLeft + (scrollrel*1) + 97 - (image.width / 2);
	    windowPadding = (document.body.offsetWidth - 780) / 2;
	    if ((leftpx1 + image.width) <= (740 + windowPadding)) {
	        leftpx = leftpx1;
	    } else if (leftpx2 >= (-1 * windowPadding)) {
	        leftpx = leftpx2;
	    } else {
	        leftpx = leftpx3;
	    }
	    this.Element.style.left = leftpx + "px";
	},
	
	setHref: function(href)
	{
	    this.Element.parentNode.href = href;
	},
	
	display: function()
	{
	    this.ParentHtmlBlock.style.display = "block";
	    clearInterval(this.Timer);
	    clearTimeout(this.FadeTimer);
	    if (this.SameImage) {
	        this.showImmediately();
	    } else {
	        this.TickClock = 0;
	        this.Timer = setInterval(this.show.bindAsEventListener(this), this.TickSize);
        }
	},
	
	show: function()
	{
	    //W3C
	    this.Element.style.opacity = (this.TickClock * (0.6/this.MaxTickCount)) + 0.4;
	    //IE
	    this.Element.style.filter = 'alpha(opacity = ' + ((this.TickClock * (60/this.MaxTickCount)) + 40) + ')';
	    this.TickClock++;
	    if (this.TickClock >= this.MaxTickCount) {
		    clearInterval(this.Timer);
			this.Element.style.opacity = "1";
			this.Element.style.filter = 'alpha(opacity = 100)';
		}
	},
	
	showImmediately: function()
	{
        this.Element.style.opacity = "1";
        this.Element.style.filter = 'alpha(opacity = 100)';
	}
};
