/* Copyright (c) 2009 Onlyweb Studio | http://www.onlyweb.ru/ */
/*

Author: Mike Zimin (mihazimin@mail.ru)

From Russia with love!

*/


var Likbez = newClass();
Likbez.prototype = {
	
	init: function(p) {
		var t = this;
		
		t.predRef = p.predRef;
		t.nextRef = p.nextRef;
		t.count = p.count;
		
		t.pageList = $('#page_list');
		t.pageNumber = $('#page_number');
		t.backPage = $('#back_page');
		t.nextPage = $('#next_page');
		t.postPeople = $('#post_people');
		
		t.duration = 1000;
		
		t.contentText = [];
		$('.ContentText').each(function() {
			var el = $(this);
			var id = Number(el.attr('id').replace('content_text_', ''));
			t.contentText[id] = el;
		});
		
		t.contentQuote = [];
		$('.ContentQuote').each(function() {
			var el = $(this);
			var id = Number(el.attr('id').replace('content_quote_', ''));
			t.contentQuote[id] = el;
		});
		
		t.contentRef = [];
		$('.ContentRef').each(function() {
			var el = $(this);
			var id = Number(el.attr('id').replace('content_ref_', ''));
			t.contentRef[id] = el;
		});
		
		t.likbezImage = [];
		$('.LikbezImage').each(function() {
			var el = $(this);
			var id = Number(el.attr('id').replace('likbez_image_', ''));
			t.likbezImage[id] = el;
		});
		
		
		// treat hash if it's existed
		var hash = location.hash.replace('#', '');
		var scene = Number(hash);
		if (scene >= 2 && scene <= t.count) {
			t.currentScene = scene;
			t.setSceneFirstTime(scene);
		} else {
			t.currentScene = 1;
			t.setSceneFirstTime(1);
		}
		
		resizer.addCustomFunction(function(w) {
			t.moveImage(w);
		});
		
	},
	
	moveImage: function(w) {
		var t = this;
		
		var likbezImageLeft = Math.round((resizer.getDocumentWidth() - t.likbezImage[t.currentScene].find('img').attr('width')) / 2) + 80;
		
		t.likbezImage[t.currentScene].css('left', likbezImageLeft + 'px');
	},
	
	// movement to new scene
	move: function(newScene, duration) {
		var t = this;
		
		var oldScene = t.currentScene;
		
		if (newScene == oldScene) {
			return;
		} else if (newScene < oldScene) {
			var k = 1;
		} else {
			var k = -1;
		}
		
		// console.log(t.contentText);
		
		// Set styles for new scene
		t.contentText[newScene].css('left', -(k * 2000) + 'px').css('display', 'block');
		t.contentQuote[newScene].css('right', (k * 2000) + 'px').css('display', 'block');
		t.likbezImage[newScene].css('left', -(k * 2000) + 'px').css('display', 'block');
		if (typeof(t.contentRef[newScene]) != 'undefined') {
			t.contentRef[newScene].css('right', (k * 2500) + 'px').css('display', 'block');
		}
		
		var likbezImageLeft = Math.round((resizer.getDocumentWidth() - t.likbezImage[newScene].find('img').attr('width')) / 2) + 80;
		
		// Animate new scene
		t.contentText[newScene].animate({left: "27px"}, duration);
		t.contentQuote[newScene].animate({right: "0"}, duration);
		t.likbezImage[newScene].animate({left: likbezImageLeft + 'px'}, duration);
		if (typeof(t.contentRef[newScene]) != 'undefined') {
			t.contentRef[newScene].animate({right: "0"}, duration);
		}
		
		// Animate old scene
		t.contentText[oldScene].animate({left: (k * 2000) + 'px'}, duration);
		t.contentQuote[oldScene].animate({right: -(k * 2000) + 'px'}, duration);
		t.likbezImage[oldScene].animate({left: (k * 2000) + 'px'}, duration);
		if (typeof(t.contentRef[oldScene]) != 'undefined') {
			t.contentRef[oldScene].animate({right: -(k * 2500) + 'px'}, duration);
		}
		
		// When animation will be finished we should set display: none
		setTimeout(function() {
			t.contentText[oldScene].css('display', 'none');
			t.contentQuote[oldScene].css('display', 'none');
			t.likbezImage[oldScene].css('display', 'none');
			if (typeof(t.contentRef[oldScene]) != 'undefined') {
				t.contentRef[oldScene].css('display', 'none');
			}
		}, duration);
		
		t.currentScene = newScene;
	},
	
	updatePageList: function() {
		var t = this;
		t.pageList.find('.Page').removeClass('SelectPage');
		$('#page_' + t.currentScene).addClass('SelectPage');
	},
	
	updatePageNumber: function() {
		var t = this;
		t.pageNumber.html('шаг ' + t.currentScene + ' из ' + t.count);
	},
	
	updatePageIterator: function() {
		var t = this;
		if (t.currentScene == 1) {
			t.backPage.html(t.predRef);
		} else {
			t.backPage.html('<a onclick="likbez.pred();">сюда</a>');
		}
		if (t.currentScene == t.count) {
			t.nextPage.html(t.nextRef);
		} else {
			t.nextPage.html('<a onclick="likbez.next();">туда</a>');
		}
	},
	
	setSceneFirstTime: function(newScene) {
		var t = this;
		t.contentText[newScene].css('left', '27px').css('display', 'block');
		t.contentQuote[newScene].css('right', '0').css('display', 'block');
		t.likbezImage[newScene].css('left', '450px').css('display', 'block');
		if (typeof(t.contentRef[newScene]) != 'undefined') {
			t.contentRef[newScene].css('right', '0').css('display', 'block');
		}
		t.updatePageList();
		t.updatePageNumber();
		t.updatePageIterator();
	},
	
	changeScene: function(newScene) {
		var t = this;
		t.move(newScene, t.duration);
		t.updatePageList();
		t.updatePageNumber();
		t.updatePageIterator();
		// Устанавливаем hash
		location.hash = newScene;
	},
	
	next: function() {
		var t = this;
		t.changeScene(t.currentScene + 1);
	},
	
	pred: function() {
		var t = this;
		t.changeScene(t.currentScene - 1);
	},
	
	changePostBackground: function() {
		var t = this;
		var bit = [];
		for (var i = 0; i < 3; i++) {
			bit[i] = Math.floor((Math.random() * 10000) % 50) + 90;
		}
		var f = Math.floor((Math.random() * 10000) % 3);
		// console.log(f);
		bit[f] = 255;
		var color = '#' + bit[0].toString(16) + bit[1].toString(16) + bit[2].toString(16);
		// console.log(color);
		t.postPeople.css('background', color);
	}
	
}

