jQuery.extend({
	interval: function(fn, time, bind){
		return setInterval(function(){
			fn.apply(bind);
		}, time);
	},
	fnbind: function(fn, bind){
		return function(){
			fn.apply(bind, arguments);
		}
	}
});
jQuery.fn.extend({
	loop: function(fn, bind){
		return this.each(function(){
			fn.call(bind, this);
		});
	},
	addEvent: function(type, fn, bind){
		return this.each(function(){
			$(this)[type](function(){
				return fn.apply(bind, arguments);
			});
		});
	}
});
var Fields = {
	init: function(){
		$(".field").each(function(){
			var el = $(this);
			if(el.attr("title").length > 0){
				el.blur(Fields.toggle);
				el.focus(Fields.toggle);
				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 Slideshow = function(el){
	this.el = el;
	this.images = [];
	this.loaded = this.index = 0;
	this.init();
};
Slideshow.prototype = {
	init: function(){
		
		if(typeof(rooturl) != "undefined"){
			$.getJSON(rooturl + "startpage/slideshow", $.fnbind(this.complete, this));
			this.el.addClass("slideshow-active");
		}
		
	},
	complete: function(obj, status){
		if(!obj.images || obj.images.length == 1)
			return;
		this.length = obj.images.length;
		if(obj.images.length > 0){
			$(obj.images).loop(function(obj){
				this.images.push($("<a href=\"" + obj.href + "\" />").append($("<img />").addEvent("load", this.load, this).attr("src", obj.src)).hide().appendTo(this.el));
			}, this);
		}
	},
	load: function(e){
		this.loaded++;
		if(this.length == this.loaded){ this.start(); }
	},
	start: function(){
		this.timer = $.interval(this.next, 8000, this);
		var prev = $("<div class=\"control-prev\" />").append($("<div></div>").css("opacity", 0.25), $("<span title=\"Previous slide\">Previous</span>").css("opacity", 0.5)).mouseenter(this.enter).mouseleave(this.leave).addEvent("click", this.prev, this);
		var next = $("<div class=\"control-next\" />").append($("<div></div>").css("opacity", 0.25), $("<span title=\"Next slide\">Next</span>").css("opacity", 0.5)).mouseenter(this.enter).mouseleave(this.leave).addEvent("click", this.next, this);
		this.el.append(prev, next);
	},
	next: function(){
		this.index++;
		if(this.index == this.length){ this.index = 0; }
		this.fade();
	},
	prev: function(){
		this.index--;
		if(this.index == -1){ this.index = this.length - 1 }
		this.fade();
	},
	fade: function(){
		clearInterval(this.timer);
		this.timer = $.interval(this.next, 8000, this);
		this.el.find("a").fadeOut(2000);
		this.images[this.index].fadeIn(2000);
	},
	enter: function(){
		var el = $(this);
		el.find("span").stop().fadeTo(200, 1);
		el.find("div").stop().fadeTo(200, 0.5);
	},
	leave: function(){
		var el = $(this);
		el.find("span").stop().fadeTo(200, 0.5);
		el.find("div").stop().fadeTo(200, 0.25);
	}
};
var Share = function(el){
	this.el = el;
	this.init();
}
Share.prototype = {
	init: function(){
		this.el.addClass("share-active");
		this.form = this.el.find("form").addEvent("submit", this.submit, this);
		this.form.find("input[name=share_close]").addEvent("click", this.hide, this);
		this.fields = this.form.find("input[type=text], textarea");
		this.fields.focus(function(){
			$(this).removeClass("error");
		});
	},
	submit: function(){
		var url = this.form.attr("action");
		this.fields.each(function(){
			if(this.value.length <= 0 || this.value == this.title){
				$(this).addClass("error");
			}
		});
		$("#share-email").each(function(){
			var reg = /^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/;
			if(!reg.test($(this).val())){
				$(this).addClass("error");
			}
		});
		if($(".error", this.form).length <= 0){
			$.getJSON(url, this.form.serialize(), $.fnbind(this.complete, this));
			this.form.addClass("loading");
		}
		return false;
	},
	complete: function(obj, status){
		this.el.find("p.response").remove();
		$("<p class=\"response\">" + obj.response + "</p>").insertBefore(this.form);
		this.form.removeClass("loading");
		this.fields.each(function(){
			this.value = this.title;
		});
	},
	show: function(e){
		var offset = $(e.target).position();
		this.el.css({
			top: offset.top + 15,
			left: offset.left
		});
		this.el.addClass("share-show");
	},
	hide: function(e){
		this.el.find("p.response").remove();
		this.el.removeClass("share-show");
	}
};
var Toggable = function(el){
	this.el = $(el);
	this.init();
}
Toggable.prototype = {
	init: function(){
		this.handle = this.el.find(".toggle");
		this.el.addClass("toggable-active").wrapInner("<div class=\"toggable-holder\" />").prepend(this.handle);
		this.handle.addEvent("click", this.toggle, this);
		this.holder = $(".toggable-holder", this.el).hide();
	},
	toggle: function(){
		this.holder.slideToggle(200);
	}
}

var Site = {
	init: function(){
		/* Start Page */
		$("div#start-article-1").addClass("js-active").click(function(e){
			var el = $(e.target);
			if(!el.hasClass("article")){ el = el.parents(".article"); }
			window.location = el.find("a").attr("href");
		});
		$("div#start-article-3 li").each(function(){
			var el = $(this).addClass("js-active");
			var link = el.find("a").text(">>>");
			el.click(function(){
				window.location = link.attr("href");
			});
		});
		var slideshow = new Slideshow($("#start-slideshow"));
		
		/* Product Page */
		var share = new Share($("div.share"));
		$("a.share").click(function(e){
			share.show(e);
			return false;
		});
		$("a.print").click(function(){
			try {
				print();
			} catch(ex) { }
			return false;
		});
		$("div.toggable").each(function(){
			new Toggable(this);
		});
		$("a[rel=external]").each(function(){
			$(this).click(function(){
				var win = window.open(this);
				if(win){ win.focus() }
				return false;
			});
		});
	}
};
$(document).ready(Site.init);