var Window = {
		
		opened : null,
		elementWrapper : null,
		
		fireEvent : function(ev, config, scope) {
			if(scope == undefined) {
				scope = this;
			}
			
			if(!this.objKeyExists(this, ev.toString()))  {
				throw "Unrecognized event " + ev.toString();
			} else {
				var fn = this[ev];
				
				fn.call(scope, config);
			}
		},
		
		onInitInstance : function() {
			$("document, window, body, html").keydown(this.pressedButton);
			$("#mask").live("click", $.proxy(this.closeWindow, this));
			
			this.elementWrapper = $("#window");
			this.elementWrapper.hide();
			
			this.opened = false;
		},
		
		pressedButton : function(ev) {
			if(ev.keyCode == 27 && Window.opened) {
				Window.closeWindow();
			}
		},
		
		onOpenWindow : function(obj) {
			if(!this.opened) {
				this.openWindow();
				this.opened = true;
				
				this.elementWrapper.find(".margined").html(obj.html);
			}
		},
		
		openWindow : function() {
			
			this.elementWrapper.show();
			this.elementWrapper.center();
			this.maskScreen();
		},
		
		afterCloseWindow : function() {
			this.opened = false;
		},
		
		closeWindow : function() {
			this.elementWrapper.hide();
			this.unmaskScreen();
			
			this.fireEvent("afterCloseWindow");
		},
		
		maskScreen : function() {
			$("body").append('<div id="mask"></div>');
			var mask = $("#mask");
			
			mask.css("position", "absolute");
			mask.css("left", "0px");
			mask.css("top", "0px");
			mask.css("width", $(window).width());
			mask.css("height", $(window).height());
			mask.css("z-index", 500);
			mask.css("background-color", "#7f6f6f");
			mask.css("opacity", "0.5");			
		},
		
		unmaskScreen : function() {
			$("#mask").remove();
		},
		
		objKeyExists : function(obj, key) {
			for(k in obj) {
				if(k == key) {
					return true;
				}
			}
			
			return false;
		} 
};

var Banners = {
		counter : 1,
		
		init : function(bannersSetting) {
			Window.fireEvent("onInitInstance");
			
			this.initEvents();
			
			var banners = { vertical : [], horizontal : []};
			
			for(bannerPosition in bannersSetting) {
				var bannerType = bannersSetting[bannerPosition].type;
				
				if(bannerType == "vertical") {
					banners.vertical.push(bannerPosition);
				} else {
					banners.horizontal.push(bannerPosition);
				}
			}


			
			for(bannerPosition in banners) {
				var cfgObj = {
					wrap 		: "circular",
					scroll 		: 1
				};
	
				if(bannerPosition == "vertical") {
					cfgObj = this.pMergeObj(cfgObj, { vertical : true });
				}

				var offset = 1;
				for(i = 0; i < banners[bannerPosition].length; i++) {
					var speed = bannersSetting[banners[bannerPosition][i]].animationSpeed;
					
					var bannerCfg = this.pMergeObj(cfgObj, { start : offset, animation : speed });

					$("#" + banners[bannerPosition][i]).jcarousel(bannerCfg);

					offset++;
				}
			}


			this.startTimers(bannersSetting);
		},
		
		initEvents : function() {
			$(".banner-custom-menu a").live("click", this.reconfigureBanner);
			$(".config-panel-option").live("click", this.performReconfiguration);
			$("#window .close").live("click", function() { Window.closeWindow();});
		},
		
		performReconfiguration : function() {
			var position = Banners.reconfigPosition;
			var type = $(this).attr("id");
			
			Banners.setCookie(position, type, 30);
			
			window.location = "/";
			
		},
		
		reconfigureBanner : function() {
			Banners.reconfigPosition = $(this).attr("class");
			
			Window.fireEvent("onOpenWindow", {
				html : 	'<div class="close"><span class="hidden">x</span></div>' +
						'<div class="window-heading">Přizpůsobit panel</div>' + 
						'<ul class="left-custom">' +
							'<li class="config-panel-option" id="Actions">Akce</li>' +
							'<li class="config-panel-option" id="Contests">Games</li>' +
							'<li class="config-panel-option" id="Hitparade">Hitparáda</li>' +
						'</ul>' +
						'<ul class="left-custom">' +
							'<li class="config-panel-option" id="Photogalleries">Fotogalerie</li>' +
							'<li class="config-panel-option" id="Videogalleries">Videogalerie</li>' +
							'<li class="config-panel-option" id="Infobanners" style="width: 145px;">Původní</li>' +
						'</ul>' +
						'<p>Po přihlášení zůstane tvoje nastavení zachováno.</p>'
						
			});
		},

		startTimers : function(bannersSetting) {
			for(bannerName in bannersSetting) {
				this.doAnimate(bannerName, bannersSetting[bannerName].timeOfAnimation);
			}
		},

		doAnimate : function(bannerName, time) {
			var self = this;
			
			setTimeout(function() {
				$("#" + bannerName.toString()).data("jcarousel").next();

				self.doAnimate(bannerName, time);	
			}, time);
		},
		
		pMergeObj : function(obj1, obj2) {
			var obj = {};

			for(key in obj1) {
				obj[key] = obj1[key];
			}

			for(key in obj2) {
				obj[key] = obj2[key];
			}

			return obj;
		},
		
		setCookie : function(key, value, days) {
			value = encodeURIComponent(value); 

			var date = new Date();
				date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000);
			
			value+='; expires=' + date.toGMTString();
			
			document.cookie = key + '=' + value;
		}

};

jQuery.fn.center = function () {
    this.css("position","absolute");
    this.css("top", (($(window).height() - this.outerHeight()) / 2) + $(window).scrollTop() + "px");
    this.css("left", (($(window).width() - this.outerWidth()) / 2) + $(window).scrollLeft() + "px");
    return this;
}

$(document).ready(function(){
	var rightItems = $("#right-banner li").length;
	$(".right-mask").click(function() {
		var jc =  $("#right-banner").data("jcarousel");
		
		var visible = jc.first;
		var items = rightItems;
		var current = 0;

		if(visible > items - 1) {
			if(visible % items == 0) {
				current = items - 1;
			} else {
				current = visible % items;
				current = current - 1;
			}
		} else {
			current = visible - 1;
		}


		var el = $("#right-banner li").eq(current).find("a");

		if(el.length > 0) {
			window.location = el.attr("href");
		}
	});	

	var centerItems = $("#center-banner li").length;	
	$(".center-mask").click(function() {
		var jc =  $("#center-banner").data("jcarousel");
		
		var visible = jc.first;
		var items = centerItems;
		var current = 0;

		if(visible > items - 1) {
			if(visible % items == 0) {
				current = items - 1;
			} else {
				current = visible % items;
				current = current - 1;
			}
		} else {
			current = visible - 1;
		}


		var el = $("#center-banner li").eq(current).find("a");

		if(el.length > 0) {
			window.location = el.attr("href");
		}
	});


	var leftItems = $("#left-banner li").length;
	$(".left-mask").click(function() {
		var jc =  $("#left-banner").data("jcarousel");

		var visible = jc.first;
		var items = leftItems;
		var current = 0;

		if(visible > items - 1) {
			if(visible % items == 0) {
				current = items - 1;
			} else {
				current = visible % items;
				current = current - 1;
			}
		} else {
			current = visible - 1;
		}


		var el = $("#left-banner li").eq(current).find("a");

		if(el.length > 0) {
			window.location = el.attr("href");
		}
	});	
});

function runPlayer(speed) {
	  window.open("http://www.abradio.cz/player.php?kod=43&amp;quality=asx"+speed+"","ABradio","width=395,height=250,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,copyhiostory=no");
}

