
var tabs = {
	
	init: function(pos) {
		
		$("ul.bkmarks li").bind("click", function() {
			tabs.switchTab(this);
		});
		
		$("ul.bkmarks li").bind("focus", function() {
			if(this.blur) this.blur();
		});
		
		$("ul.bkmarks").each(function() {
			$(this).find("li:first").addClass("active");
		});
	},
	
	switchTab: function(element) {
		
		$(element.parentNode).find("li").each(function() {
			if (this == element) {
				$(this).addClass("active");
			} else {
				$(this).removeClass("active");
			}
		});
	}
}

var imgTabs = {
	
	index:	1,
	stop:	false,
	
	init: function() {
		
		$(".featured-news li div.order").bind("click", function() {
			imgTabs.stop = true;
			imgTabs.change(findParent(this, "LI"));
			
			return false;
		});
		
		$(".featured-news a").bind("click", function() {return false;});
		
		$(".featured-news div.headline, .featured-news div.image").bind("click", function() {
			document.location.href = $(this.parentNode).attr("href");
		});
		
		$(".featured-news img").each(function(i) {
			
			$(this).bind("load", function() {
				$(this).unbind("load");
				
				$(this.parentNode.parentNode.parentNode).show();
				images.hide_preload(this.parentNode);
				
				if((i+1) == $(".featured-news img").length)
					setTimeout('imgTabs.set()', 6000);
			});
			
			$(this).attr("src", photo_news[i]);
			images.show_preload(this.parentNode);
		});
	},
	
	set: function() {
		
		if (imgTabs.stop)
			return;
		
		if (imgTabs.index == 4)
			imgTabs.index = 1;
		else
			imgTabs.index++;
			
		imgTabs.change($(".featured-news li[@id = 'box"+imgTabs.index+"']")[0]);
		
		setTimeout('imgTabs.set()', 6000);
	},

	change: function(element) {
		
		if ($(element).hasClass("active"))
			return;

		$(".featured-news li").each(function() {
			if (this == element) {
				$(this).addClass("active");
				$(this).find(".image").fadeIn(500);
			} else {
				$(this).removeClass("active");
				$(this).find(".image").hide();
			}
		});
	}
}

var imgNews = {
	
	index: 0,
	image: false,
	cite: false,
	
	init: function() {
		
		imgNews.image	= $("div.image img");
		imgNews.cite	= $("cite");
		
		$("a#prev").bind("click", function() {imgNews.prev(); return false;});
		$("a#next").bind("click", function() {imgNews.next(); return false;});
		
		$(".pic-inset-wrapper").bind("mouseover", function() {
			$(this).find(".switcher").show();
		});
		
		$(".pic-inset-wrapper").bind("mouseout", function() {
			$(this).find(".switcher").hide();
		});
	},
	
	prev: function() {
		
		if (imgNews.index == 0) {
			imgNews.index = photo_news.length-1;
		} else {
			imgNews.index = imgNews.index-1;
		}
		imgNews.set();
	},
	
	next: function() {
		
		if (imgNews.index == photo_news.length-1) {
			imgNews.index = 0;
		} else {
			imgNews.index = imgNews.index+1;
		}
		imgNews.set();
	},
	
	set: function() {
		
		images.show_preload(imgNews.image[0].parentNode);
		
		$(imgNews.image).bind("load", function() {
			$(this).unbind("load");
			images.hide_preload(this.parentNode);
		});
			
		$(imgNews.image).attr("src", "/_img/photo/news/"+photo_news[imgNews.index].src);
		
		if(photo_news[imgNews.index].text != '') {
			$(imgNews.cite).text(photo_news[imgNews.index].text);
			$(imgNews.cite).show();
		} else {
			$(imgNews.cite).hide();
		}
	}
}

var matchStack = {
	
	items: false,
	title: false,
	index: 0,
	
	init: function() {
		
		matchStack.items = $(".matches");
		
		matchStack.title = $(".switcher .name");
		matchStack.type();
		
		$("a#prev").bind("click", function() {matchStack.prev(); return false;});
		$("a#next").bind("click", function() {matchStack.next(); return false;});
	},
	
	prev: function() {
		
		if (matchStack.items.length <= 1)
			return;

		$(matchStack.items[matchStack.index]).hide();
		
		if (matchStack.index == 0) {
			matchStack.index = matchStack.items.length-1;
		} else {
			matchStack.index = matchStack.index-1;
		}
			
		matchStack.type();
		$(matchStack.items[matchStack.index]).fadeIn(300);
	},
	
	next: function() {
		
		if (matchStack.items.length <= 1)
			return;
		
		$(matchStack.items[matchStack.index]).hide();
		
		if (matchStack.index == matchStack.items.length-1) {
			matchStack.index = 0;
		} else {
			matchStack.index = matchStack.index+1;
		}
		
		matchStack.type();
		$(matchStack.items[matchStack.index]).fadeIn(300);
	},
	
	type: function() {
		$(matchStack.title).text($(matchStack.items[matchStack.index]).attr("type"));
	}
}

var images = {
	
	init: function() {
		
		$("div.image img").each(function(i) {
			
			$(this).bind("load", function() {
				$(this).unbind("load");
				images.hide_preload(this.parentNode);
			});
			
			images.show_preload(this.parentNode);
		});
	},
	
	show_preload: function(element) {
		
		var preload = document.createElement("div");
		$(preload).addClass("preloader index-png-fix");
		
		var img = document.createElement("img");
		$(img).attr("src", "/_img/template/preloader.gif");
		$(preload).append(img);
		
		$(element).append(preload);
	},
	
	hide_preload: function(element) {
		$(element).find(".preloader").remove();
	}
}
