var MoviePoster = $.inherit(
	{
		__constructor: function(oOptions) {
			oThis = this;

			this.jContainer = oOptions.jContainer;
			this.eContainer = this.jContainer.get(0);
			this.jjPosters = oOptions.jjPosters;
			this.jjTitles = oOptions.jjTitles;
			this.jjImages = $(".img", this.jjPosters);
			this.aImages = [];
			this.aCanvases = new Array(this.jjPosters.length);
			
			this.updateContainerHeight(
				this.eContainer.clientHeight,
				this.eContainer.clientWidth * 225 / 100
			);

			//this.externalBinds();

			this.init();
		},
		
		init: function() {
			this.jjImages.each(function(index){
				oThis.aImages.push(new Image());
				oThis.aImages[index].isLoaded = false;
				if (oThis.jjPosters.eq(index).is(":visible") || oThis.jjPosters.length == 1) oThis.selectedIndex = index;
				
				oThis.aImages[index].onload = function() {
					this.isLoaded = true;
					oThis.initPoster(index);
				};
				
				oThis.aImages[index].src = $(this).attr('src');
				
				this.className += " hidden";
			});
			
			$(window).resize(/*$.throttle(*/function(){
				oThis.updateImageSize(oThis.selectedIndex);
			}/*, 200)*/);
		},

		initPoster: function(index) {
			this.createCanvas(index);

			if(oThis.aImages.length == oThis.jjImages.length && !oThis.selectedIndex) { oThis.selectedIndex = 0; }

			if (index == oThis.selectedIndex) {
				this.updateImageSize(index);
			}
			this.jjPosters.eq(index).removeClass("not-loaded");
		},

		createCanvas: function(index) {
			$(this.self.canvasString).appendTo(this.jjPosters.eq(index));

			this.aCanvases[index] = $(this.self.canvasClass, this.jjPosters.eq(index));
		},
		
		updateContainerHeight: function(containerHeight, newHeight){
			var newContainerHeight = (window.innerHeight ? window.innerHeight : document.documentElement.clientHeight) - 80;
			
			if (newHeight < newContainerHeight)
				newContainerHeight = newHeight;
			
			newContainerHeight = newContainerHeight < 225 ? 225 : newContainerHeight;
			
			if (containerHeight != newContainerHeight) {
				oThis.jContainer.height(newContainerHeight);
				containerHeight = newContainerHeight;
			}
			
			return containerHeight;
		},

		updateImageSize: function(index) {
			var
				containerWidth = oThis.eContainer.clientWidth,
				containerHeight = oThis.jContainer.clientHeight,
				newContainerHeight,
				viewportHeight,
				newWidth,
				newHeight,
				options = {},
				jPoster = oThis.jjPosters.eq(index);

			options.marginTop = 0;

			//if (oThis.aImages[index].width / oThis.aImages[index].height < containerWidth / containerHeight) {
				newWidth  = containerWidth;
				newHeight = containerWidth * oThis.aImages[index].height / oThis.aImages[index].width;
				
				if (index == oThis.selectedIndex){
					containerHeight = oThis.updateContainerHeight(containerHeight, newHeight);
				}

				if (newHeight > containerHeight) {
					options.marginTop = -0.5 * (newHeight - containerHeight);
				}
			//} else {
				//newWidth  = containerHeight * oThis.aImages[index].width / oThis.aImages[index].height;
				//newHeight = containerHeight;
			//}

			options.width  = newWidth;
			options.height = newHeight
			oThis.updateCanvas(index, options)
		},

		updateCanvas: function(index, options) {
			var jCanvas = oThis.aCanvases[index];
			
			if (jCanvas) {

				jCanvas.attr({ height: options.height, width: options.width });
	
				var ctx = jCanvas.get(0).getContext('2d');
				ctx.drawImage(oThis.jjImages.get(index), 0, 0, options.width, options.height);

			}
		}
	},
	{
		canvasString: '<canvas class="canvas"></canvas>',
		canvasClass: '.canvas'
	}
);

var MoviePosterIE = $.inherit(
	MoviePoster,
	{
		updateCanvas: function(index, options) {
			var jCanvas = this.aCanvases[index];

			options.marginTop = 0;

			jCanvas &&
			jCanvas
				.css(options)
				.css(
					'filter',
					"progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + this.aImages[index].src + "', sizingMethod='scale')"
				);
		}
	},
	{
		canvasString: '<div class="canvas"></div>'
	}
);



//$(window).load(function() {
$(function(){
	var oMoviePostersOptions = {
		jContainer: $('#header'),
		jjPosters: $('#header .container:has(.img)'),
		jjTitles: $('#header .title-text:not(.text-column-position)')
	}
	if (oMoviePostersOptions.jjPosters.length){
		if ('undefined' != typeof(HTMLCanvasElement)) {
			var oMoviePosters = new MoviePoster(oMoviePostersOptions);
		} else {
			var oMoviePosters = new MoviePosterIE(oMoviePostersOptions);
		}
	}
	oMoviePosters.selectedIndex = 0;
	var aaa = setTimeout(rotatePoster, 5000);
	//console.log(oMoviePosters.selectedIndex);
	
	
	$("#changemainbanner").click(function(){
		clearTimeout(aaa);
		rotatePoster();
		return false;
	});
	
	
	
	
	
	
	function rotatePoster(){
		var _next = oMoviePosters.selectedIndex;
		oMoviePosters.jjPosters.eq(oMoviePosters.selectedIndex).addClass("hidden");
		_next++;
		if (_next >= oMoviePosters.jjPosters.length) _next = 0;
		oMoviePosters.selectedIndex = _next;
		oMoviePosters.updateImageSize(oMoviePosters.selectedIndex);
		oMoviePosters.jjPosters.eq(oMoviePosters.selectedIndex).removeClass("hidden");
		
		$("#our_projects_descs div.pr_descs").removeClass("on").eq(oMoviePosters.selectedIndex).addClass("on");
                $("#bannernames").stop().animate({
			scrollLeft: 304*oMoviePosters.selectedIndex
		}, 1000);
                //console.log(oMoviePosters.selectedIndex+" "+(304*oMoviePosters.selectedIndex+4)+" "+$("#our_projects_descs div.pr_descs.on").closest("td").offset().left);
		$("#containersozlerleft div.s").addClass("hidden").eq(oMoviePosters.selectedIndex).removeClass("hidden");
		$("#innercontainersozlerright div.s").addClass("hidden").eq(oMoviePosters.selectedIndex).removeClass("hidden");

		
		aaa = setTimeout(rotatePoster, 5000);
	}
	
});
