
var bkmarks = {
	
	init: function(pos) {
		
		$("ul.bkmarks li").bind("click", function() {
			bkmarks.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 tabs = {
	
	init: function() {
		
		$("ul.pager li").bind("click", function() {
			if (!$(this).hasClass("active")) {
				tabs.switchTab(this);
			}
			
			return false;
		});
		
		$("ul.pager li").bind("focus", function() {
			if(this.blur) this.blur();
		});
		
		tabs.switchTab($("ul.pager li:first")[0]);
	},
	
	switchTab: function(element) {
		
		$("ul.pager li").each(function(pos) {
			if (this == element) {
				$(this).addClass("active");
				
				$("div.pager-content").each(function(i) {
					if (pos == i) {
						$(this).show();
					} else {
						$(this).hide();
					}
				});
			} 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 = {
	
	zoom: 0,
	index: 0,
	image: false,
	full_image: false,
	cite: false,
	switcher: false,
	
	init: function() {
		
		imgNews.image		= $("div.pic-inset > div.image img");
		imgNews.full_image	= $("div.pic-inset-big > div.image img");
		imgNews.cite		= $("cite");
		imgNews.switcher	= $("div.pic-inset-wrapper div.switcher");
		
		$("a.prev").bind("click", function() {imgNews.prev(); return false;});
		$("a.next").bind("click", function() {imgNews.next(); return false;});
		
		$(".pic-inset-wrapper").bind("mouseover", function() {
			if (!imgNews.zoom) {
				$(imgNews.switcher).show();
			}
		});
		
		$(".pic-inset-wrapper").bind("mouseout", function() {
			$(imgNews.switcher).hide();
		});
		
		$("div.pic-inset > div.image").bind("click", function() {
			$(imgNews.switcher).hide();
			imgNews.zoomIn();
		});
		
		$("div.pic-inset-big > div.image").bind("click", function() {
			imgNews.zoomOut();
		});
		
		/** close modal window by escape */
		document.onkeyup = function(e){ 	
			if (e == null) { // ie
				keycode = event.keyCode;
			} else { // mozilla
				keycode = e.which;
			}
			if(keycode == 27) { // close
				imgNews.zoomOut();
			}	
		};
		
		$(document).bind("click", function(e) {
			var el = e.target || e.srcElement;
			var parentEls = $(el).parents();
				
			var flag = false;
			for(var i=0; i<parentEls.length; i++) {
				if(parentEls[i].className == "pic-inset" || parentEls[i].className == "pic-inset-big") {
					flag = true;
					break;
				}
			}
			
			if (!flag)
				imgNews.zoomOut();
		});
	},
	
	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() {
		
		if (imgNews.zoom) {
			var img = imgNews.full_image;
		} else {
			var img = imgNews.image;
		}
		
		images.show_preload(img[0].parentNode);
		
		$(img).bind("load", function() {
			$(this).unbind("load");
			images.hide_preload(this.parentNode);
		});
			
		$(imgNews.image).attr("src", "/_img/photo/news/"+photo_news[imgNews.index].prv);
		$(imgNews.full_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();
		}
	},
	
	zoomIn: function() {
		$(".pic-inset-big-wrapper").show();
		imgNews.zoom = 1;
	},
	
	zoomOut: function() {
		$(".pic-inset-big-wrapper").hide();
		imgNews.zoom = 0;
	}
}

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();
	}
}