$(function() {
	
	$("#welcome-area h1,h2,.fader,#scroller,#message label,#close-form,#message,#form-send,#back-to-top").ifixpng();

	$("a").attr("target", "_self");

	$("#scroller .logos").scrollBackground({});
	$("#tabs h3").hover(function() {
		if (!$(this).hasClass("selected")) $(this).addClass("over");
	}, function() {
		if (!$(this).hasClass("selected")) $(this).removeClass("over");
	}).click(function() {
		var self = this;
		$(self).removeClass("over").toggleClass("selected");
		var tab = $(self).next(".tab-content");
		var open = $("#tabs h3.selected").not(self);
		//open.removeClass("selected").next(".tab-content").hide();
		if ($("*",tab).length == 0) {
			tab.show();
			$.get($(self).attr("url"), function(data, status) {
				var images = $(data).find("img");
				var imgCount = images.length;
				images.each(function(idx, i) {
					var img = new Image();
					$(img).load(function() {
						imgCount--;
						if (imgCount <= 0) {
							tab.css({height:'auto', background: 'none'}).html(data);		
						}
					});
					img.src = $(i).attr("src");
				});
			}, 'text');
		} else {
			tab.slideToggle(2000);
		}
		return false;
	});
	/*
	$("h3[url]").each(function(idx, item) {
		var tab = $(item).next(".tab-content");
		$.get($(item).attr("url"), function(data, status) {
			tab.css({height:'auto'}).html(data);
		}, 'text');
	});*/
	
	$("input, textarea").each(function(idx, inp) {
		var def = $(inp).attr("def");
		$(inp).val(def);
		$(inp).focus(function() {
			$(inp).addClass("focus").removeClass("error");
			if ($(inp).val() == def) {
				$(inp).val("");	
			}
		}).blur(function() {
			if ($(inp).val() == "") {
				$(inp).removeClass("focus");
				$(inp).val(def);	
			};
		});
	});
	
	$("#contact,#close-form").click(function() {
		if ($("#contact-form").is(":visible")) {
			$("#contact-form").slideUp(1500);
		} else {
			$("#contact-form").slideDown(2000, "easeOutBounce");
		}
		return false;
	});
	
	$("#message input#form-send").hover(function() {
		$(this).css({backgroundPosition: "0px 0px"});
	}, function() {
		if (!$(this).hasClass("sending")) {
			$(this).css({backgroundPosition: "0px -23px"});
		}
	});
	
	$("#form-callback input").checkbox();
	
		
	$("#back-to-top").click(function() {
		$(document).scrollTo("0", 3000);
		return false;
	});
	
	var idx = location.href.indexOf("#");
	if (idx != -1) {
		var ref = location.href.substring(idx+1);
		var tab = $("#tabs h3[name='"+ref+"']");
		if (tab.length > 0) {
			tab.addClass("selected");
			$.get(tab.attr("url"), function(data, status) {
				tab.next(".tab-content").css({height:'auto', background: 'none'}).show().html(data);
				$(document).scrollTo(tab, 2000);
			});
		};
	}
});

$.fn.checkbox = function(settings) {
	settings = $.extend({
		css: {
			def: "checkbox",
			on: "checkbox_on"
		}
	}, settings);
	return this.each(function(n, item) {
		var cb = $('<div class="'+settings.css.def+'"></div>');
		$(item).hide().after(cb);
		item.checked = false;
		cb.click(function() {
			$(this).toggleClass(settings.css.on);
			item.checked = $(this).hasClass(settings.css.on);
		});
	});
} 


$.fn.scrollBackground = function(settings) {
	settings = $.extend({
		speed: 13,
		factor: 60.0,
		defaultSpeed: -1,
		currentSpeed: -1
	}, settings);
	return this.each(function(n, item) {
		var currentLeft = 0;
		window.setInterval(function() {
			currentLeft += settings.currentSpeed;
			$(item).css({backgroundPosition:  currentLeft+'px center'});
		}, settings.speed);
		var w = $(this).width();
		$("#scroller").mousemove(function(e) {
			var x = e.pageX - $(this).offset().left;
			var speed = ((w/2-x) / settings.factor)|0; 
			settings.currentSpeed = (speed < 0 ? -Math.abs(speed): speed);
		});
		$("#scroller").mouseout(function(e) {
			settings.currentSpeed = settings.defaultSpeed;
		});
	});
}


