﻿$ = jQuery.noConflict();
$(document).ready(function(){
	
	if($("#page.homepage").length > 0){
		$("#featured_contents .news_item").fadingThrough({
			"navigation": $("#featured_content #featured_content_navigation li")
		});
	} else if($("#page.subpage, #page.search").length > 0){
		$("#featured_contents .news_item").fadingThrough();
	}
	
//	$('#page #facebook_banner img').fadingThrough();
	
	var galleryItems = jQuery("[rel=gallery-object], [rel^=gallery-room], .gallery a").colorbox();
//	var galleryItems = jQuery("[rel^=gallery-room], .gallery-room a").colorbox();
	jQuery("[rel=gallery_more]").click(function(event) {
		event.preventDefault();
		event.stopPropagation();
		if (galleryItems.length && galleryItems.length > 0) {
			var itemNum = 0;
			var itemHref = jQuery(this).attr('href');
			for (var i = 0; i < galleryItems.length; i++) {
				var item = jQuery(galleryItems[i]);
				if (itemHref == item.attr('href')) {
					itemNum = i;
					break;
				}
			}
			jQuery(galleryItems[itemNum]).click();
		}
		return false;
	});
	
	$("#language_selection select").eq(0).change(function(){
		window.location.href = this.value;
	});
	
	$(".db_object_description").each(function(index, el){
		var main = $(el);
		
		main.find(".db_object_trigger").click(function(event){
			event.preventDefault();
			
			if(main.find(".db_object_content").attr("open") == "true"){
				main.find(".db_object_content").attr("open", "false");
				main.find(".db_object_content").removeClass("open");
				this.innerHTML = "więcej &raquo;"
			}
			else {
				main.find(".db_object_content").attr("open", "true");
				main.find(".db_object_content").addClass("open");
				this.innerHTML = "&laquo; mniej"
			}
		});
	});
	
	$(".db_tabs li a").click(function(event){
		event.preventDefault();
		
		$(".db_tabs li").removeClass("selected");
		$(this).parent().addClass("selected");
		
		$(".db_tab_content").removeClass("selected");
		var content = $("#"+$(this).attr("rel")).addClass("selected");
		if (content.length > 0) {
			if (FB) {
				FB.XFBML.parse(content[0]);
			}
		}
	});
	
	$(".db_object_description").each(function(index, el){
		var db_object_content = $(el).find(".db_object_content");
		db_object_content.addClass("open");
		if(db_object_content.height() <= 35){
			$(el).find(".db_object_trigger").remove();
		}
		db_object_content.removeClass("open");
	});
	
	$("#upper_footer li a:not(#upper_footer .sub-menu li a)").click(function(event){
		event.preventDefault();
	}).mouseover(function(){ $(this).css({ "cursor": "default", "text-decoration": "none"}); });
	
	// add object form
//	jQuery('#add_your_object').each(function() {
//		var el = jQuery(this);
//		el.find();
//	});
	
});




(function($){
	$.fn.fadingThrough = function(options){
		
		var settings = {
			"time": 8000,
			"play_control": null,
			"dir": null,
			"left_arrow": null /* $("#news_navigation .left_arrow") */, 
			"right_arrow": null /* $("#news_navigation .right_arrow") */,
			"navigation": null,
			"navigation_center": false
		}
		
		$.extend(settings, options);
		
		var current_index = 0;
		var length = this.length;
		var elements = $(this);
		
		elements.css("display", "none");
		elements.first().css("display", "block");
		
		if(settings["navigation"]){
			settings["navigation"].eq(0).addClass("active");
		}
			
		add_events();
		
		if(settings["navigation_center"]){
			count_news_navigation_width();
		}
		
		var interval = setTimeout(move_next, settings["time"]);
		is_playing = true;
		
		function add_events(){
			if(settings["left_arrow"]){	
				settings["left_arrow"].click(function(event){
					event.preventDefault();
					refresh_interval()
					move_prev();
				});
			}
			
			if(settings["right_arrow"]){	
				settings["right_arrow"].click(function(event){
					event.preventDefault();
					refresh_interval()
					move_next();
				});
			}
			
			if(settings["navigation"]){
				settings["navigation"].each(function(index, el){
					$(el).click(function(event){
						event.preventDefault();
						refresh_interval()
						move_to(index);
					});
				});
			}
			
			if(settings["play_control"]){
				settings["play_control"].click(function(event){
					event.preventDefault();
					refresh_interval()
					toggle_play();
				});
			}
			
//			$(window).resize(function() {
//				count_width();
//				count_height();
//			});
		}
		
		function refresh_interval(){
			if(is_playing == true){
				clearInterval(interval);
				interval = setInterval(move_next, settings["time"]);
			}
		}
		
		function move_next(){
			var cur_el = current_index;
			
			if(current_index == length-1){
				current_index = 0;
			}
			else {
				current_index += 1;
			}
			
			animation(cur_el, current_index);
		}
		
		function move_prev(){
			var cur_el = current_index;
			if(current_index == 0){
				current_index = length-1;
			}
			else {
				current_index -= 1;
			}
			
			animation(cur_el, current_index);
		}
		
		function move_to(to){
			animation(current_index, to);
			current_index = to;
		}
		
		function toggle_play(){
			if(is_playing){
				is_playing = false;
				clearTimeout(interval);
				settings["play_control"].addClass("play");
			}
			else {
				is_playing = true;
				interval = setTimeout(move_next, settings["time"]);
				settings["play_control"].removeClass("play");
			}
		}
		
		function animation(prev, next){
			if(settings["navigation"]){
				settings["navigation"].removeClass("active");
				settings["navigation"].eq(next).addClass("active");
			}
			
			$($(elements).get(prev)).fadeOut(500, function() {
				if(is_playing)
					clearTimeout(interval);
					interval = setTimeout(move_next, settings["time"]);
			});
			$($(elements).get(next)).fadeIn(500);
		}
		
		function count_news_navigation_width(){
			var els = $("#news_navigation a");
			var width = 0;
			
			els.each(function(index, el){
				width += el.offsetWidth;
				width += parseInt($(el).css("margin-left"));
				width += parseInt($(el).css("margin-right"));
			});
			
			$("#news_navigation").css("width", width);
		}
		
	};
})(jQuery);


Array.prototype.max = function() {
	var max = this[0];
	var len = this.length;
	for (var i = 1; i < len; i++) if (this[i] > max) max = this[i];
	return max;
}


