var teatercentrum = {
	init : function () {
		this.ieFixes();
		this.fonts.setup();
		this.submenu.setup();
		this.downloadIcons();
		this.lightBox();
		this.targetLinks();
		this.resetFilter();	
	},
	
	ieFixes : function () {
		$('#menu li').hover(function() {  
	    	$(this).addClass('hover');  
	   	}, function() {  
	   		$(this).removeClass('hover');  
	   });
	},
	
	fonts : {
		settings : {
			path : "http://sv.teatercentrum.se/fonts/",
			font : "futura",
			extension : ".png",
			maxWidth : 320
		},
		
		setup : function () {
			if (jQuery.browser.msie) {
  			if(parseInt(jQuery.browser.version) < 7) {
					return;
				}
			}
			this.replace("#menu > li > a, #search button", 10, "ffffff");
			this.replace("#submenu li a", 8, "444C0F");
			this.replace("#calmenu li a", 8, "444C0F");
			
			this.replace(".featureditem h1", 11, "411207");
			this.replace(".item:not(.featureditem) h2", 11, "411207");
			this.replace(".category", 11, "f60327");
			
			//this.replace("#content .readmore a", 10, "ffffff");
			this.replace("#sidebar h3", 9, "ffffff");
			this.replace("#sidebar .readmore a", 9, "6a7614");
		},
		
		replace : function (selector, size, color) {
			
			path = this.settings.path;
			font = this.settings.font;
			extension = this.settings.extension;
			maxWidth = this.settings.maxWidth;
			
			$(selector).each(function() {
				if ($(this).is("h2")) {
					var parent = $(this).parents(".item");
					if (parent.length > 0) {
						var image = parent.find(".imagecontainer");
						if (image.length > 0) {
							maxWidth = $(this).width() - image.width() - 15;
						} else {
							maxWidth = $(this).width();
						}
					}
				}
				
				var val = $(this).html();
				var imgName = decodeURI(val);
				if ($(this).css("text-transform") === 'uppercase') {
					imgName = imgName.toUpperCase();
				}
				imgName = imgName.replace(/\?/g,'%3F');
			 
				$(this).html('<img src="' + path + font + '/' + size + '/' +  color + '/'  + maxWidth + '/' + imgName + extension + '" alt="'+ val +'"/>');
			});
			
		}
	},
	
	submenu : {
		
		baseUrl : "/wp-content/themes/tc/feed/categoryitems.php?view=",
		currentFilter : null,
		cookieName : "tc-currentFilter",
		defaultValue : "alla",
		pageLoad : true,
		itemContainer : null,
		
		setup : function () {
			var menu = $("#submenu");
			if (menu.length === 0) { return; }
			this.itemContainer = $("#categoryitems");
			if (this.itemContainer.length === 0) { return; }
			var items = menu.find("li");
			var links = menu.find("a");
			
			
			this.checkForSavedFilter(links);
			this.pageLoad = false;
			
			links.click( function() {
				var item = $(this);
				items.removeClass("active");
				item.parents("li").addClass("active");
				var val = item.attr("href").split("view=")[1];
				if (teatercentrum.submenu.currentFilter != val) {
					teatercentrum.submenu.currentFilter = val;
					teatercentrum.submenu.saveFilter();
					teatercentrum.submenu.filterItems();
				}
				return false;
			});
		},
		
		checkForSavedFilter : function (links) {
			var val = $.cookie(this.cookieName);
			var noCookieSet = false;
			if (val == null) {		
				val = this.defaultValue;
				noCookieSet = true;
			}
			links.filter("[href$=" + val + "]").parents("li").addClass("active");
			this.currentFilter = val;
			if (!noCookieSet) {
				teatercentrum.submenu.filterItems();
			}
		},
		
		saveFilter : function () {
			$.cookie(this.cookieName, this.currentFilter, { path: '/', expires: 10 });
		},
		
		filterItems : function () {
			if (this.currentFilter.length === 0) { return; }
			var loadItem = true;
			if ((this.currentFilter === this.defaultValue) && (this.pageLoad)) {
				loadItem = false;
			}
			if (loadItem) {
				var url = this.baseUrl + this.currentFilter;
				this.itemContainer.load(url, '', function() {
					var replaceFonts = true;
					if (jQuery.browser.msie) {
	  				if(parseInt(jQuery.browser.version) < 7) {
							replaceFonts = false;
						}
					}
					if (replaceFonts) {
						teatercentrum.fonts.replace("#categoryitems .item:not(.featureditem) h2", 11, "411207");
						teatercentrum.fonts.replace("#categoryitems .category", 11, "f60327")
					}
				});
			}
		}
	},
	

	resetFilter : function () {
		var homelinks = $("#logoblock a, #menu a:first");
		homelinks.click( function() {
			teatercentrum.submenu.currentFilter = null;
			teatercentrum.submenu.saveFilter();
		});
	},
		
	downloadIcons : function () {
		$(".featureditem p").each(function() {
			$(this).find('a[@href$=.pdf]').addClass("download-pdf");
		});
	},
	
	lightBox : function () {
		$("a[@rel*=lightbox]").lightBox();
	},
	
	targetLinks : function () {
		$("a[@rel*=external]").click(function(){window.open(this.href); return false}); 
	}
	
};

$(document).ready(function() {
	teatercentrum.init();
});