jQuery.fn.fadeToggle = function(speed, easing, callback) {
   return this.animate({opacity: 'toggle'}, speed, easing, callback);
};  
String.prototype.urlDecode = function(f,t) {
	var $this=this+"";
	//try {
		$this=decodeURIComponent(unescape($this));
	//} catch(ex) { }
	while(($this+"").indexOf(f)>0) {
		$this=$this.replace(f,t);
	}
	return $this;
};

var Gullruten = {
	init : function(options) {
		options=$.extend({ servertime: new Date() }, options);
		var $this=this;
		$this.ui.countdown(options.servertime);
		$this.ui.cycle();
		$this.ui.menu(options.page);
	},
	ui : {
		menu : function(page) {
			
			var w=0;
			
			$.each($(".nav ul.select li"), function() {
				w+=$(this).width()+40;
			});
			
			$(".nav").width(w);
			if(page=="") {
				$(".nav ul.select li a#forside").addClass("active");
			} else {
				$(".nav ul.select li a#"+page).addClass("active");
			}
			$(".nav ul.select li a.static").click(function() {
				var $a=$(this);
				$(".dropdown").not($a.siblings(".dropdown")).fadeOut("fast");
				$(".nav ul.select li a.active").removeClass("active");
				$(this).addClass("active").blur();
				$a.siblings(".dropdown").fadeToggle("fast", "", function() {
					if($(this).css("display")=="none") {
						$a.removeClass("active");
					}
				});
			});
			
			$(".dropdown .item").hover(function() {
				$(this).addClass("hover");
			}, function() {
				$(this).removeClass("hover");
			}).click(function() {
				window.location=$(this).find("a").attr("href");
			});
			
			var embedQuickTime=function() {
				$.embedquicktime({
		      plugin: 'js/jquery.embedquicktime.js'
		    });
		    $(".play_overlay").hover(function() {
		    	$(this).addClass("hover");
		    }, function() {
		    	$(this).removeClass("hover");
		    });
			};
			
			if(page=="aktuelt" || page=="oyeblikk" ) {
				var playButtonHover=function() {
					$(".gal_box,.gal_box_nopadding").hover(function() {
						$(this).find(".play_big").addClass("hover");
					}, function() {
						$(this).find(".play_big").removeClass("hover");
					});
				};
				playButtonHover();
				
				var _isFetching=false;
				var browse = {
					total : parseInt($("#_tot").val()),
					max : parseInt($("#_max").val()),
					pos : 0,
					fetch : function() {
						var $this=this;
						_isFetching=true;
						$.post("ax.php", { type:"articles", cat:(page=="aktuelt" ? 0 : 1), pos:this.pos, start:(this.pos*this.max), max:this.max }, function(d) {
							eval(d);
							$this.applyResult({ response : response, callback : function() {
									_isFetching=false;
									playButtonHover();
								}
							});
						});
					},
					applyResult : function(options) {
						var tmp=$("#gal_box_template").html();
						var str="";
						$.each(options.response.objs, function(i) {
							str+=tmp.replace("#class#", "gal_box"+(i % 3 == 2 ? "_nopadding" : ""))
							.replace("#id#",this.id)
							.replace("#ingress#",this.ingress.urlDecode("+"," ").substr(0,60))
							.replace("#image#",this.image)
							.replace("#playbutton#", (this.articletype==1 ? "<div class=\"play_big\"></div>" : ""));
						});
						$("#gal_box_wrapper").html(str);
						if(options.callback) {
							options.callback();
						}
					},
					hasNext : function() {
						return (this.total>(this.pos*this.max+this.max));
					},
					isEnd : function() {
						return ((this.pos*this.max+this.max)>=this.total);
					},
					isFetching : function() {
						return _isFetching;
					}
				};
				$(".arrow.previous").unbind("click").click(function() {
					if(!browse.isFetching()) {
						if(browse.pos > 0) {
							browse.pos--;
							if(browse.pos==0) {
								$(".arrow.previous").addClass("hidden");
							}
							browse.fetch();
						}
						$(".arrow.next").removeClass("hidden");
					}
				});
				$(".arrow.next").unbind("click").click(function() {
					if(!browse.isFetching()) {
						if(browse.hasNext()) {
							browse.pos++;
							if(browse.isEnd()) {
								$(".arrow.next").addClass("hidden");
							}
							browse.fetch();
						}
						$(".arrow.previous").removeClass("hidden");
					}
				});
			}
			else
			if(page=="artikkel") {// || page=="nominerte" || page=="fagpris") {
		  	embedQuickTime();
			}
			else
			if(page=="vinnere") {
				 $(".selectbox select").change(function() {
				 	window.location='?p=vinnere&year='+$(this).val();
				});
			}
		},
		cycle : function() {
			if($.browser.opera) {
				$("#slide_wrapper").css("width","940px");
			}
			var _activeSlide=0;
			$('#fade_wrapper').cycle({
		    fx:     'fade', 
		    speed:   450, 
		    timeout: 6000, 
		    pager : '#slide_pagingbox ul',
        pagerAnchorBuilder: function(i, slide) {
        	return '#slide_pagingbox ul li:eq(' + i + ') a';
		    },
		    updateActivePagerLink : function(i, slide) {
		    	$("#slide_pagingbox ul li").not(slide).removeClass("active");
		    	$("#slide_pagingbox ul li:eq(" + slide + ")").addClass("active");
		    }
			}).click(function() {
				window.location=$(".slide:visible").attr("href");
			});
		},
		countdown : function(servertime) {
			
			var showStart = new Date();
			showStart = new Date(2010, 4, 8);
			showStart.setHours(20);
			showStart.setMinutes(0);
			showStart.setSeconds(0);
			$("p.countdown").countdown({  
				serverSync : function() {
					return servertime;
				},
				until: showStart,
				layout: "{dn} {dl} {hn} {hl} {mn} {ml} {sn} {sl}"
			});
		}
	}
};