jQuery.extend({
	interval: function(fn, time, bind){
		return setInterval(function(){
			fn.apply(bind);
		}, time);
	},
	timeout: function(fn, time, bind){
		return setTimeout(function(){
			fn.call(bind);
		}, time);
	},
	fnbind: function(fn, bind){
		return function(){
			fn.apply(bind, arguments);
		}
	}
});
jQuery.fn.extend({
	loop: function(fn, bind){
		return this.each(function(i){
			fn.call(bind, this, i);
		});
	},
	times: function(fn, bind){
		return this.each(function(){
			for(var i=0;i<this;i++){
				fn.call(bind, i);
			}
		});
	},
	addEvent: function(type, fn, bind){
		return this.each(function(){
			$(this)[type](function(){
				return fn.apply(bind, arguments);
			});
		});
	}
});
var Site = {
	init: function(){
		/*if(sIFR != null && !sIFR.isActive){
			Site.sIFRonReplace();
		}*/
		$("div#main").find("a").each(function(){
			var el = $(this);
			var url = el.attr("href");
			var rel = el.attr("rel");
			
			if(/^http:\/\//.test(url)){
				el.click(function(e){
					pageTracker._trackPageview("outgoing:" + url.replace(/^http:\/\//, ""));
					if (rel == "external") {
					    e.preventDefault();
					    window.open(url);
					}
				});
			}
		});
		$("div.product p.back").addEvent("click", function(){ if (history.length > 1) {history.go(-1); return false;} }, this);
	},
	sIFRonReplace: function(){
		//$("div.comment").hide();
	}
};
$(window).load(Site.init);

var Adress = function(){
    this.init();
};
Adress.prototype = {
    init: function(){
        this.hash = unescape( location["hash"]).substr(1);
    },
    follow: function(val){
        if(typeof(val) != typeof(undefined)){
            this.hash = val;
            location["hash"] = escape( this.hash );
        }
        return this.hash;
    }
    ,
    getValue: function (key) {
        var keys = this.hash.split("&");
        for (var i=0;i<keys.length;i++) {
            var b = keys[i].split("=");
            if (b[0] == key && b[1].length > 0)
                return b[1].replace("%26","&");
        }
        return "";
    },
    setValue: function(key,val) {
        var curVal = this.getValue(key);
        if (val === undefined || curVal == val)
            return this.hash;
        
        val = val.toString().replace("&","%26");
        curVal = curVal.toString().replace("&","%26");

        if (curVal.length > 0) {
            this.hash = this.hash.replace("&"+key+"="+curVal,(val.length > 0 ? "&" +key+"="+val : ""));
        }
        else {
            if (this.hash.indexOf("&"+key+"=") > -1)
                this.hash = this.hash.replace("&"+key+"=","");
            
            if (val.length > 0)
                this.hash += "&" + key + "=" + val;
        }
        
        location["hash"] = this.hash;
        
        return this.hash;
    }
}; 

var Fields = {
	init: function(){
		$(".field").each(function(){
			var el = $(this);
			if(el.attr("title").length > 0){
				el.blur(Fields.toggle);
				el.focus(Fields.toggle);
				
				if (el.val().length <= 0)
				    el.val(el.attr("title"));
			}
		});
	},
	toggle: function(){
		var el = $(this);
		if(el.val() == el.attr("title")){
			el.val("");
			el.addClass("focus");
		}
		else if(el.val() == ""){
			el.val(el.attr("title"));
			el.removeClass("focus");
		}
	}
};
$(window).load(Fields.init);

var Tooltip = function(){
	this.wrapper = $("<div class=\"tooltip\" />").appendTo(document.body).hide();
	this.el = $("<div class=\"tooltip-i\" />").appendTo(this.wrapper);
	this.text = $("<p />").appendTo(this.el);
};
Tooltip.prototype = {
	set: function(text){
		this.text.text(text);
	},
	show: function(){
		this.wrapper.fadeIn();
	},
	hide: function(){
		this.wrapper.fadeOut();
	},
	bind: function(el){
		el.addEvent("mousemove", this.move, this);
	},
	move: function(e){
		this.wrapper.css({
			"top": e.pageY,
			"left": e.pageX
		});
	}
};
var Tooltips = {
	init: function(){
		var tips = $(".tooltip");
		if(tips.length > 0){
			var tooltip = new Tooltip();
			tips.each(function(){
				var el = $(this);
				el.tooltip = el.attr("title");
				el.attr("title", "");
				el.mouseenter(function(){
					tooltip.bind(el);
					tooltip.set(el.tooltip);
					tooltip.show();
				});
				el.mouseleave(function(){
					tooltip.hide();
				});
			});
		}
	}
};
$(window).load(Tooltips.init);

var Archive = function(el){
	this.el = el;
	this.init();
};
Archive.prototype = {
	init: function(){
		this.el.addClass("js-active");
		this.items = this.el.find("li a");
		this.items.click(this.click);
	},
	click: function(){
		var el = $(this);
		el.parent().toggleClass("open");
		return el.hasClass("post");
	}
}
var Archives = {
	init: function(){
		$("div.archive > ul").each(function(){
			new Archive($(this));
		});
	}
};
$(window).load(Archives.init);

var Videos = {
	init: function(){
		$("ul.videos li").hover(Videos.hover, Videos.hover).click(Videos.click);
	},
	hover: function(){
		$(this).toggleClass("over");
	},
	click: function(){
		// Do something
	}
};
$(window).load(Videos.init);
