﻿Event.observe(window, "load", OnDocumentLoad);

var GH;

var isMobile = false;
isMobile = isMobile || navigator.userAgent.match(/iPad/i) != null;
isMobile = isMobile || navigator.userAgent.match(/iPhone/i) != null;
isMobile = isMobile || navigator.userAgent.match(/iPod/i) != null;
isMobile = isMobile || navigator.userAgent.match(/series60/i) != null;
isMobile = isMobile || navigator.userAgent.match(/symbian/i) != null;
isMobile = isMobile || navigator.userAgent.match(/android/i) != null;

var DynamicScrollEnabled = !isMobile;

function OnDocumentLoad(evObj)
{
}
	
function OnHtmlLoad ()
{
	Event.observe(window, "resize", OnWindowResize);
	
	GH = new GalleryExtender();	

	if ( $("Navigation") )
		new NavigationExtender($("Navigation"));
		
	if ( $("PictureChange") && $("Pictures").getStyle("display") == "none" )
	{
		var pictures = new Array();
		$$("#Pictures a").each(function(item)
		{
			pictures.push(item);
		}, this);
			
		new ShootingNewsExtender($("PictureChange"),pictures);
	}
	
	if ( $("Publications") )
	{
		new PublicationImageExtender($("Publications"), $("Publications").down(".imageContainer"), $("Publications").down(".imageDescription"));
		
	}
	
	CorrectLayout();
}


function OnWindowResize(evObj)
{
	CorrectLayout();
	
}

function CorrectLayout ()
{
	var vp = new ViewportProperties();
	var scrollContent = $("ScrollContainer");
	var content = $("Content");
	
	if ( DynamicScrollEnabled )
		scrollContent.setStyle({
			height: (vp.WindowHeight-scrollContent.cumulativeOffset()[1]-30) + "px", 
			width: ((vp.WindowWidth-scrollContent.cumulativeOffset()[0] < 820) ? 820 : vp.WindowWidth-scrollContent.cumulativeOffset()[0]) + "px"
			});
			
			
	$("PageTop").setStyle({display: (scrollContent.getHeight() > content.getHeight()) ? "none" : "inline"});

	if ( $("LanguageSwitch") )
	{
		var left = Math.round((vp.WindowWidth-800)/2);
		var top = Math.round((vp.WindowHeight-97-$("LanguageSwitch").getHeight()-50)/2);

		if ( left < 0 )
			left = 0;
			
		if ( top < 0 )
			top = 0;
		
		$("Logo").setStyle({left: left  + "px", top: top + "px", padding: "0px 0px 0px 0px"});
		$("LanguageSwitch").setStyle({left: left + "px", top: (top + 97+50) +  "px"});
		
		$("ScrollContainer").hide();

	}
}

var PublicationImageExtender = Class.create(
{
	initialize: function( linkContainer, targetContainer, imageDescription )
	{
		this.NavigationClick = this.OnNavigationClick.bindAsEventListener(this);
		this.PictureLoad = this.OnPictureLoad.bindAsEventListener(this);

		this.LinkContainer = $(linkContainer);
		this.TargetContainer = $(targetContainer);
		this.ImageDescription = $(imageDescription);
		
		this.LinkContainer.select("a").each(function(item) {
			if ( item.href.endsWith(".jpg") )
			{
				item.PictureUrl = item.href;
				item.href = "javascript: void(0);";
				Event.observe(item, "click", this.NavigationClick);
			}
		}, this);
	},
	
	OnNavigationClick: function( evObj)
	{
		this.NextPicture = null;

		var targetObj = $(evObj.target);

		if ( targetObj.up("a") )
			targetObj = targetObj.up("a");

		
		this.TargetContainer.select("img").each(function(item) {
				if ( item.src == targetObj.PictureUrl )
				{
					this.NextPicture = item;
				}
				else
				{
					item.removeClassName("current");
				}
		}, this);
		
		this.LinkContainer.select("a").each(function(item) {
				item.removeClassName("selected");
		}, this);
		
		if ( !this.NextPicture)
		{
			this.NextPicture= new Element("img");
			this.NextPicture.src = targetObj.PictureUrl;
			this.NextPicture.Title = targetObj.title;
			this.TargetContainer.appendChild(this.NextPicture);
		}
		 
		targetObj.addClassName("selected");
		this.NextPicture.addClassName("current");
		

		new PeriodicalExecuter(this.PictureLoad, 0.1);
	},
	
	OnPictureLoad : function (evObj)
	{
        if (this.NextPicture.complete) {
            evObj.stop();
			this.ImageDescription.innerText = this.NextPicture.Title;
			this.TargetContainer.setStyle({height: this.NextPicture.getHeight() + "px"});
        }
	},

});

/**** GALLERY EXTENDER ****/
var GalleryExtender = Class.create(
{
    initialize: function () {
		this.PictureLoad = this.OnPictureLoad.bindAsEventListener(this);
		this.PictureChange = this.OnPictureChange.bindAsEventListener(this);
		this.NavigationClick = this.OnNavigationClick.bindAsEventListener(this);
		this.NextClick = this.OnNextClick.bindAsEventListener(this);
		this.PreviousClick = this.OnPreviousClick.bindAsEventListener(this);
		this.CloseClick = this.OnCloseClick.bindAsEventListener(this);
		this.Periodical = this.OnPeriodical.bindAsEventListener(this);
		
		this.Timeout = 4;
		this.AutoLoadActivated = false;

		this.NextPicture = null;
		this.CurrentPicture = null;
		this.CurrentIndex = -1;
		
		this.Gallery = $("Gallery");
		this.Container = $("Gallery").down(".container");
		this.Navigation = $("Content");
		this.Next = this.Gallery.down(".next");
		this.Previous = this.Gallery.down(".previous");
		this.Num = this.Gallery.down(".num");
		this.Count = this.Gallery.down(".count");
		this.Close = this.Gallery.down(".close");
		
		this.Pictures = new Array();
		
		this.Navigation.select("a.thumb").each(function(item) {
			item.href = "javascript: void(0);";
			Event.observe(item, "click", this.NavigationClick);
		}, this);
		
		this.Gallery.select("a").each(function(item) {
			if ( item.href.endsWith(".jpg") && this.Pictures.indexOf(item.href) == -1 )
			{
				this.Pictures.push(item.href);
			}
		}, this);

	},
	
	LoadPicture : function(item)
	{
		if ( this.NextPicture == null && this.Pictures.indexOf(item) != this.CurrentIndex  )
		{
			Event.stopObserving(this.Next, "click", this.NextClick);
			Event.stopObserving(this.Container, "click", this.NextClick);
			Event.stopObserving(this.Previous, "click", this.PreviousClick);
			Event.stopObserving(this.Close, "click", this.CloseClick);

			this.CurrentIndex = this.Pictures.indexOf(item);
			
			this.NextPicture = new Image();
			this.NextPicture.src = item;
			
			this.Num.innerHTML = this.CurrentIndex+1;
			this.Count.innerHTML = this.Pictures.length;

			if ( this.Pictures.length > 1 )
				new PeriodicalExecuter(this.PictureLoad, 0.1);
			else
				this.AutoLoadActivated = false;
		}
	},
	
	OnPeriodical: function(evObj)
	{
		evObj.stop();
		this.LoadPicture((this.CurrentIndex < this.Pictures.length-1)?this.Pictures[this.CurrentIndex+1]:this.Pictures[0]);
	},
	
	OnPictureLoad : function (evObj)
	{
        if (this.NextPicture.complete) {
            evObj.stop();

            this.Container.appendChild(this.NextPicture);
            
			this.NextPicture.hide();

			if ( this.CurrentPicture )
			{
				Effect.Fade(this.CurrentPicture, { duration: 0.0  });

				// todo animation resize
				this.Container.setStyle({height: this.NextPicture.getHeight() + "px", width: this.NextPicture.getWidth() + "px"});
			}
			else
			{
	            this.Container.setStyle({height: this.NextPicture.getHeight() + "px", width: this.NextPicture.getWidth() + "px"});
			}

			Effect.Appear(this.NextPicture, { duration: 0.0, afterFinish: this.PictureChange});
        }
	},
	
	OnPictureChange: function(evObj)
	{
		if ( this.CurrentPicture )
			this.Container.removeChild(this.CurrentPicture);
	
		this.CurrentPicture = this.NextPicture;
		this.NextPicture = null;
		
		Event.observe(this.Next, "click", this.NextClick);
		Event.observe(this.Container, "click", this.NextClick);
		Event.observe(this.Previous, "click", this.PreviousClick);
		Event.observe(this.Close, "click", this.CloseClick);

		if ( this.AutoLoadActivated )
			new PeriodicalExecuter(this.Periodical, this.Timeout);
	},
	
	OnNavigationClick: function(evObj)
	{
		this.AutoLoadActivated = false;
		this.Gallery.setStyle({display: "block"});
		this.LoadPicture(this.Pictures[0]);
	},
	
	OnNextClick: function(evObj)
	{
		this.AutoLoadActivated = false;
		
		this.LoadPicture((this.CurrentIndex < this.Pictures.length-1)?this.Pictures[this.CurrentIndex+1]:this.Pictures[0]);
	},
	
	OnPreviousClick: function(evObj)
	{
		this.AutoLoadActivated = false;
		
		this.LoadPicture((this.CurrentIndex == 0)?this.Pictures[this.Pictures.length-1]:this.Pictures[this.CurrentIndex-1]);

	},
	
	OnCloseClick: function(evObj)
	{
		this.AutoLoadActivated = false;
		
		this.Gallery.setStyle({display: "none"});
	}

});
/**** GALLERY EXTENDER ****/

/********** NavigationExtender  **********/
var NavigationExtender = Class.create();
NavigationExtender.prototype =
{
    initialize: function(SliderBox) {
    	this.WindowResize = this.OnWindowResize.bindAsEventListener(this);
        this.ItemOver = this.OnItemOver.bindAsEventListener(this);
        this.MouseMove = this.OnMouseMove.bindAsEventListener(this);
    	
        this.SliderBox = $(SliderBox);
        this.InnerBox = this.SliderBox.down("ul");

        Event.observe(this.SliderBox, "mouseover", this.ItemOver);

        this.Movement = null;

        this.OverAreaSize = null;



    	$("Navigation").select("a").each(function(a)
    	{
     		if ( a.href == document.location.href || document.location.href.startsWith( a.href.replace(".html", "-")) )
    		{
    			a.up("li").addClassName("selected");
    			if ( a.up("li").up("li") )
    			{
					a.up("li").up("li").addClassName("selected");
	    			if ( a.up("li").up("li").up("li") )
						a.up("li").up("li").up("li").addClassName("selected");
				}

    		}
    	}, this);    

    	this.Update();
    	
    	Event.observe(window, "resize", this.WindowResize);
    },
    
	Update: function()
	{
		var vp = new ViewportProperties();
		
		if ( vp.WindowHeight < $("Navigation").positionedOffset()[1] + $("Navigation").down("ul").getHeight() && DynamicScrollEnabled)
		{
			$("Navigation").addClassName("scroll");
			$("Navigation").setStyle({height: (vp.WindowHeight - $("Navigation").positionedOffset()[1]) + "px"});
		}
		else
		{
			$("Navigation").removeClassName("scroll");
			$("Navigation").setStyle({height: "auto"});
			$("Navigation").down("ul").setStyle({top: "0px"});
		}

        this.OverAreaSize = Math.round(this.SliderBox.getHeight()/4);
        this.LeftOverArea = this.OverAreaSize;
        this.RightOverArea = this.SliderBox.getHeight() - this.OverAreaSize;
	},
	
	OnWindowResize: function(evObj)
	{
		this.Update();
	},
	
	OnItemOver: function(evObj) {
        Event.observe(document, "mousemove", this.MouseMove);
    },

    OnMouseMove: function(evObj) {
        var x = Event.pointerX(evObj) - Element.cumulativeOffset(this.SliderBox)[0];
        var y = Event.pointerY(evObj) - Element.cumulativeOffset(this.SliderBox)[1];

        this.PerformEvent(x, y);
    },

    PerformEvent: function(x, y) {
        var rightMissing = this.InnerBox.getHeight() - this.SliderBox.getHeight() + Element.positionedOffset(this.InnerBox)[1];
        var leftMissing = -Element.positionedOffset(this.InnerBox)[1];

        if ((y <= 0 || y >= this.SliderBox.getHeight() || x <= 0 || x >= this.SliderBox.getWidth())) {
            if (this.Movement != null) {
                this.Movement.cancel();
                this.Movement = null;
            }

            Event.stopObserving(document, "mousemove", this.MouseMove);
        }
        else {

            if (y > this.LeftOverArea && y < this.RightOverArea && this.Movement != null) {
                this.Movement.cancel();
                this.Movement = null;
            }

            if (y > this.RightOverArea && rightMissing > 0) {
                var dur = rightMissing / 100 * 0.4;
                if (this.Movement != null && this.Movement.options.y > 0) {
                    this.Movement.cancel();
                    this.Movement = new Effect.Move(this.InnerBox, { y: -rightMissing, x: 0, mode: "relative", duration: dur });
                }
                else if (this.Movement == null)
                    this.Movement = new Effect.Move(this.InnerBox, { y: -rightMissing, x: 0, mode: "relative", duration: dur });
            }

            if (y < this.LeftOverArea && leftMissing > 0) {
                var dur = leftMissing / 100 * 0.4;
                if (this.Movement != null && this.Movement.options.y < 0) {
                    this.Movement.cancel();
                    this.Movement = new Effect.Move(this.InnerBox, { y: leftMissing, x: 0, mode: "relative", duration: dur });
                }
                else if (this.Movement == null)
                    this.Movement = new Effect.Move(this.InnerBox, { y: leftMissing, x: 0, mode: "relative", duration: dur });
            }
        }

    }
};
/********** NavigationExtender  **********/

/********** SHOOTINGNEWS **********/
var ShootingNewsExtender = Class.create();
ShootingNewsExtender.prototype =
{
    initialize: function(holder, pics) {
        this.ImageLoaded = this.OnImageLoaded.bindAsEventListener(this);
        this.ImageChanged = this.OnImageChanged.bindAsEventListener(this);

        this.Holder = $(holder);
        this.Pics = pics;
        this.Timeout = 0.1;
        this.PicIndex = -1;

        this.CurrentImage = null;

        this.NextImage = new Image();
        var nextPicLink =  this.NextPicture();
        this.NextImage.src = nextPicLink.href;
        this.LinkText = nextPicLink.innerText;

        if (this.Holder.down("img") == undefined)
            new PeriodicalExecuter(this.ImageLoaded, this.Timeout);

        this.Timeout = 3;
    },

    NextPicture: function() {
        if (this.PicIndex == -1)
            this.PicIndex = Math.floor(Math.random() * this.Pics.length);
        else if (this.PicIndex < (this.Pics.length - 1))
            this.PicIndex++;
        else
            this.PicIndex = 0;

        return this.Pics[this.PicIndex];
    },

    OnImageLoaded: function(evObj) {
        if (this.NextImage.complete) {
            evObj.stop();

            this.Holder.appendChild(this.NextImage);

            if (this.CurrentImage == null) {
                this.OnImageChanged(null);
            }
            else {
                Effect.Fade($(this.CurrentImage), { duration: 2 });
                Effect.Appear($(this.NextImage), { duration: 2, afterFinish: this.ImageChanged });
            }
        }
    },

    OnImageChanged: function(evObj) {
        if (this.CurrentImage != null)
            this.Holder.removeChild(this.CurrentImage);

        this.CurrentImage = this.NextImage;
        
		this.Holder.setStyle({height: this.CurrentImage.getHeight() + "px"});

        if ( this.Holder.down(".description") )
	        this.Holder.down(".description").innerText = this.LinkText;

        $(this.CurrentImage).addClassName("visible");

        this.NextImage = new Image();
        var nextPicLink =  this.NextPicture();
        this.NextImage.src = nextPicLink.href;
        this.LinkText = nextPicLink.innerText;

        new PeriodicalExecuter(this.ImageLoaded, this.Timeout);
    }
}
/********** SHOOTING NEWS **********/


/********** VIEWPORT PROPERTIES **********/
var ViewportProperties = Class.create();
ViewportProperties.prototype =
{
    initialize: function() {
        this.WindowWidth = 0;
        this.WindowHeight = 0;
        this.ScrollLeft = 0;
        this.ScrollTop = 0;
        this.DocWidth = 0;
        this.DocHeight = 0;

        this.Calculate();
    },

    Calculate: function() {
        if (window.innerWidth) {
            this.WindowWidth = window.innerWidth-18;
            this.WindowHeight = window.innerHeight;
        }
        else if (document.documentElement && document.documentElement.clientWidth) {
            this.WindowWidth = document.documentElement.clientWidth;
            this.WindowHeight = document.documentElement.clientHeight;
        }
        else if (document.body) {
            this.WindowWidth = document.body.clientWidth;
            this.WindowHeight = document.body.clientHeight;
        }

        if (window.pageYOffset) {
            this.ScrollLeft = window.pageXOffset;
            this.ScrollTop = window.pageYOffset;
        }
        else if (document.documentElement && document.documentElement.scrollTop) {
            this.ScrollLeft = document.documentElement.scrollLeft;
            this.ScrollTop = document.documentElement.scrollTop;
        }
        else if (document.body) {
            this.ScrollLeft = document.body.scrollLeft;
            this.ScrollTop = document.body.scrollTop;
        }

        if (document.body.scrollHeight > document.body.offsetHeight) {
            this.DocWidth = document.body.scrollWidth;
            this.DocHeight = document.body.scrollHeight;
        }
        else {
            this.DocWidth = document.body.offsetWidth;
            this.DocHeight = document.body.offsetHeight;
        }
    }

};
/********** VIEWPORT PROPERTIES **********/
