var AL = {};

AL.components = {};
AL.componentsCount = 0;

AL.init = function() {
	this.loadComponents();
	this.initComponents();
};

AL.loadComponents = function() {
	var tags = $('.component');
	jQuery.each(tags, function() {
		AL.loadSingleComponent(this);
	});
};


AL.loadSingleComponent = function(tag) {
	var c = {};
	var id = 'comp' + AL.componentsCount;
	tag.id = id;
	if (tag.onclick) {
		c.types = tag.onclick().types || false;
		c.params = tag.onclick().params || false;
		c.element = tag;
		AL.components[id] = c;
		AL.componentsCount++;
		return id;
	} else {
		return false;
	}
};

AL.initComponents = function() {
	for (var i in this.components) {
		var c = this.components[i];
		AL.initSingleComponent(c);
	}
};

AL.initSingleComponent = function(obj) {
	var t = obj.types;
	for (var i = 0; i < t.length; i++) {
		var componentType = t[i];
		var componentName = t[i]+'Component';
		if (AL[componentName]) {
			obj.element.removeAttribute('onclick');
			var component = AL[componentName];
			new component().init(obj.element, obj.params);
		} else {
			alert('No '+componentType+' component!');
		}
	}
};

AL.albumHoverComponent = function() {
	var self = this;
	this.init = function(element,params) {
/*
		this.element = element;
		this.items = $('.album-item',this.element);
		this.items.map(function(i,el){
			$(el).hover(self.hoverOn,self.hoverOut);
		});
*/
	};
	this.hoverOn = function() {
		self.items.not(this).stop().animate({opacity:0.5});
	};
	this.hoverOut = function() {
		self.items.not(this).stop().animate({opacity:1});
	};
};


AL.tabsComponent = function() {

	var self = this;

	this.init = function(element,params) {

		this.element = element;
		this.holder = $('.tab-holder',this.element);
		this.tabs = $('.tab',this.element);
		this.selectedTab = $('.tab-selected',this.holder);
		this.labels = $('.tab-label',this.element);
		this.selectedLabel = $('.tab-label-selected',this.element);
		this.labels.map(function(i,el){
			$(el).click(self.click);
		});
	};

	this.click = function(event) {
		var label = $(this);
		if (label == self.selectedLabel) {
			// nothing
		} else {
			for (var i = 0, l = self.labels.length; i < l; i++) {
				if (self.labels[i] == this)
					var tab = $(self.tabs[i]);
			};
			self.selectedLabel.removeClass('tab-label-selected');
			label.addClass('tab-label-selected');
			self.selectedLabel = label;
			self.selectedTab.removeClass('tab-selected');
			self.selectedTab.fadeOut('slow', function(){$(this).addClass('hidden')});
			tab.fadeIn('slow', function(){$(this).addClass('tab-selected').removeClass('hidden');});
			self.selectedTab = tab;
		}
	};

};

AL.showcaseComponent = function() {

	var self = this;

	this.init = function(element,params) {
		this.element = element;
		this.display = $('.showcase-display',this.element);
		this.picture = $('.showcase-picture',this.element);
		this.picture_next = $('.showcase-picture-next',this.element);
		this.picture_prev = $('.showcase-picture-prev',this.element);
		this.picture_next.click(this.next);
		this.picture_prev.click(this.prev);
		this.thumbsBlock = $('.showcase-thumbs',this.element);
		this.selectedThumb = $('.showcase-thumb-selected',this.thumbsBlock);
		this.thumbs = $('.showcase-thumb',this.thumbsBlock);
		this.thumbs.map(function(i,el){
			$(el).click(self.click);
		});
		this.links = $('.showcase-thumb-link',self.element);
		this.preload();
	};
	this.click = function(event) {
		event.preventDefault();
		var thumb = $(this);
		if (thumb == self.selectedThumb) {
			// nothing
		} else {
			self.selectThumb(thumb);
			self.showPicture($('.showcase-thumb-link',thumb)[0].href);
		}
	};

	this.preload = function() {
		var alreadyPreloaded = $('.preloaded',self.element);
		if (alreadyPreloaded.length < self.links.length) {
			var next = alreadyPreloaded.length;
			var image = $('<img/>').addClass('hidden').addClass('preloaded').appendTo(self.element).load(self.preload);
			image.attr('src',self.links[next].href);
		}
	};

	this.selectThumb = function(thumb) {
		self.selectedThumb.removeClass('showcase-thumb-selected');
		thumb.addClass('showcase-thumb-selected');
		self.selectedThumb = thumb;
		if ( $('#interior_name').length )
			$('#interior_name').html( thumb.find('img').attr('alt') );
	};

	this.showPicture = function(url) {
		var newpicture = $(self.picture).clone().addClass('hidden');
		newpicture.css('background-image','url(' + url + ')');
		$(self.display).prepend(newpicture);
		this.picture_next = $('.showcase-picture-next',this.element);
		this.picture_prev = $('.showcase-picture-prev',this.element);
		this.picture_next.click(this.next);
		this.picture_prev.click(this.prev);
		if ($.browser.msie) {
			self.picture.addClass('before-fadeout');
		}
		self.picture.fadeOut('slow');
		newpicture.fadeIn('slow');
		self.picture = newpicture;
	};
	this.prev = function() {
		for (var i = 0, l = self.thumbs.length; i < l; i++) {
			if ($(self.thumbs[i]).hasClass('showcase-thumb-selected')) {
				break;
			}
		};
		var prevNumber = i - 1;
		if (i == 0) {
			prevNumber = self.thumbs.length - 1;
		}
		var thumb = $(self.thumbs[prevNumber]);
		self.selectThumb(thumb);
		self.showPicture($('.showcase-thumb-link',thumb)[0].href);
	};
	this.next = function() {
		for (var i = 0, l = self.thumbs.length; i < l; i++) {
			if ($(self.thumbs[i]).hasClass('showcase-thumb-selected')) {
				break;
			}
		};
		var nextNumber = i + 1;
		if (self.thumbs.length == nextNumber) {
			nextNumber = 0;
		}
		var thumb = $(self.thumbs[nextNumber]);
		self.selectThumb(thumb);
		self.showPicture($('.showcase-thumb-link',thumb)[0].href);
	};

};

AL.expandableComponent = function() {
	var self = this;
	this.init = function(element) {
		this.element = element;
		this.expandable = $('.expandable',element);
		this.switchers = $('.switcher',element);
		this.switchers.map(function(i,el){
			$(el).click(function(){
				$(self.element).toggleClass('expanded')
			});
		});
	};

};

AL.rainbowComponent = function() {

	self = this;

	this.init = function(element) {
		//this.element = element;
		//this.textGradient(element,['7C5B21','E5B65D']);
	};

	this.hexToDec = function (hex) {
		var i, hash, dec = [];
		hash = {
			'A' : 10, 'B' : 11, 'C' : 12, 'D' : 13, 'E' : 14, 'F' : 15
		}
		for (i = 0; i <= 9; i++) hash[''+i] = i;
		for (i = 0; i < hex.length; i++) {
			if (i % 2 == 0) dec[parseInt(i / 2)] = hash[hex.charAt(i)] * 16;
			else dec[parseInt(i / 2)] += hash[hex.charAt(i)];
		}
		return dec;
	}

	this.decToHex = function(decArray) {
		var hex = ['0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'];
		var out = "#";
		for (var i = 0; i < decArray.length; i++) {
			dec = parseInt(decArray[i]);
			out += hex[parseInt(dec / 16)] + hex[dec % 16];
		}
		return out;
	};

	this.nodeList = function(parentNode, list, level) {
		var i, node, count;
		if (typeof list == "undefined") list = new Array();
		if (typeof level == "undefined") level = 0;
		level++;
		for (i = 0; i < parentNode.childNodes.length; i++) {
			node = parentNode.childNodes[i];
			count = list.length;
			list[count] = new Array();
			list[count][0] = node;
			list[count][1] = level;
			self.nodeList(node, list, level);
		}
		return list;
	};

	this.textGradient = function(element, colors) {
		var i, j, k, count, rgb = [], tags, text, html = "", simb, childs, child, newchild;
		var red, green, blue;
		rgb[0] = self.hexToDec(colors[0]);
		rgb[1] = self.hexToDec(colors[1]);
		text = element.innerText ? element.innerText : element.textContent;
		childs = self.nodeList(element);
		count = 0;
		for (j = 0; j < childs.length; j++) {
			child = childs[j][0];
			if (child.nodeType != 3) continue;
			html = "";
			for (k = 0; k < child.nodeValue.length; k++) {
				simb = childs[j][0].nodeValue.charAt(k);
				if (simb == " ") {html += " "; continue;}
				red = parseInt(rgb[0][0] + (rgb[1][0] - rgb[0][0]) * (count / text.length));
				green = parseInt(rgb[0][1] + (rgb[1][1] - rgb[0][1]) * (count / text.length));
				blue = parseInt(rgb[0][2] + (rgb[1][2] - rgb[0][2]) * (count / text.length));
				html += "<span style=\"color:" + self.decToHex([red, green, blue]) + "\">" + simb + "<\/span>";
				count++;
			}
			newchild = document.createElement("span");
			newchild.innerHTML = html;
			child.parentNode.replaceChild(newchild, child);
		}
	};
};
AL.popupComponent = function() {
	var self = this;
	this.init = function(element) {
		this.element = element;
		this.targets = $('.popup-target',this.element);
		this.targets.map(function(i,el){
			$(el).click(self.click);
		});
		this.popup = false;
	};
	this.click = function(event) {
		event.preventDefault();
		var href = this.href;
		var desc = $('.mega-desc',this).html();
		desc += '<br/>' + $('.award-desc',this).html();
		if (!self.popup)
			self.createPopup();
		self.loadPopup(href,desc);
	};
	this.createPopup = function() {
		var p = $('<div class="popup hidden"><div class="popup-shadow"></div><div class="popup-center"><div class="popup-container"><span class="popup-closer switcher">Закрыть</span><img class="popup-image"/><p class="popup-desc"></p></div></div></div>');
		$(document.body).append(p);
		self.popup = p;
	};
	this.loadPopup = function(url,desc) {
		$('.popup-image',self.popup).attr('src',url);
		$('.popup-desc',self.popup).html(desc);
		$('.popup-closer',self.popup).click(self.closePopup);
		$('.popup-shadow',self.popup).click(self.closePopup);
		$('.popup-center',self.popup).css('padding-top', ($(window).scrollTop()+50) + 'px');
		self.popup.css('height', document.body.offsetHeight + 'px');
		self.popup.removeClass('hidden');
	};
	this.closePopup = function() {
		self.popup.addClass('hidden');
		$('.popup-image',self.popup).attr('src','');
		$('.popup-desc',self.popup).html('');
	};
};
AL.slideshowComponent = function() {
	var self = this;
	var delay = 5000; //7000;
	this.init = function(element) {
		this.element = element;
		this.pictures = $('.slideshow-picture',element);
		if( this.pictures.length <=1  ) return;
		setTimeout(function(){self.next()}, delay);
	};
	this.next = function() {
		var hidden = $('.hidden',self.element);
		var current = self.pictures.not(hidden);
		var lucky = $(hidden[parseInt(Math.random() * hidden.length)]);
		lucky.fadeIn(1000, function(){$(this).removeClass('hidden')});
		current.css('display','block').fadeOut(1000, function(){$(this).addClass('hidden')});
		setTimeout(function(){self.next()}, 7000);
	};
};

AL.graycolComponent = function() {
	var self = this;
	this.init = function(element) {
		this.element = element;
		this.height = $('.content').height() - 80;
		this.width = $(this.element.parentNode).width();
		$(this.element).css('height',this.height);
		$(this.element).css('width',this.width);
	};
};


$(document).ready(function () {
	AL.init();
});
