$(document).ready( function(){
	$("body").addClass("js-ready");
	window.isIE6 = false /*@cc_on || @_jscript_version < 5.7 @*/;
	
	window.setTimeout(function() { initEqualBoxes(); }, 100);
	initClickable();
	initHelpPopups();
	initHelpPopups2();
	//initExpandingForms();
	initOrderForm();
	initFaqs();
});

// equalize boxes in a row
initEqualBoxes = function(root) {
	var self = this;
	if(!root) root = $(document);
	this.setHeight = function(obj) {
		var h = 0;
		obj.each(function() { h = Math.max(h, $(this).get(0).offsetHeight); });
		obj.each(function() {
			var p = parseInt($(this).css("paddingTop")) + parseInt($(this).css("paddingBottom"));
			$(this).css({ minHeight: (h - p) + "px" });
			// compensate for stubborn IE6
			if(window.isIE6) $(this).css({ height: (h - p) + "px" });
		});
		// tickle DOM, for some browser
		$("body").addClass("tickled");
	}
	$(".equal", root).each(function() {
		self.setHeight($(".body", $(this)));
	});	
}

// make boxes clickable
initClickable = function(root) {
	var self = this;
	if(!root) root = $(document);
	$(".clickable", root).hover(
		function() {
			$(this).addClass("hover");
		},
		function() {
			$(this).removeClass("hover");
		}
	).click(
		function(e) {
			if(e.target.tagName != "A" && $("a",$(this)).length > 0) document.location = $("a",$(this)).attr("href");
		}
	);
}





// help2 popups
initHelpPopups2 = function(root) {
    var self = this;
    if (!root) root = $(document);
    this.targetPopup = null;
    this.open = function(sourceLink) {
        this.targetPopup = $("#" + sourceLink.attr("href").split("#")[1])
        this.targetPopup.css({ top: sourceLink.offset().top - 25 - $("#content").offset().top + "px", left: sourceLink.offset().left - $("#content").offset().left + sourceLink.width() / 2 + "px" });
    }
    this.close = function() {
        var self = this;
        if (!this.targetPopup) return;
        this.targetPopup.css({ left: "-3000em" });
        this.targetPopup = null;
    }
    $("a.help2", root).focus(function(e) {
        self.close();
        self.open($(this));
        //e.preventDefault();
    }).blur(function() { self.close(); }).each(function() {
        $("select, input, textarea", $(this).parent()).focus(function() {
            self.open($("a.help2", $(this).parent()));
        }).blur(function() { self.close(); });
    }).mouseover(function(e) {
        $(this).focus();
        e.preventDefault();
    }).mouseout(function(e) {
        setTimeout('self.close()', 5000);
        e.preventDefault();
    });
}



// help popups
initHelpPopups = function(root) {
    var self = this;
    if (!root) root = $(document);
    this.targetPopup = null;
    this.open = function(sourceLink) {
        this.targetPopup = $("#" + sourceLink.attr("href").split("#")[1])
        this.targetPopup.css({ top: sourceLink.offset().top - $("#content").offset().top + "px", left: sourceLink.offset().left - $("#content").offset().left + sourceLink.width() / 2 + "px" });
    }
    this.close = function() {
        var self = this;
        if (!this.targetPopup) return;
        this.targetPopup.css({ left: "-3000em" });
        this.targetPopup = null;
    }
    $("a.help", root).focus(function(e) {
        self.close();
        self.open($(this));
        //e.preventDefault();
    }).blur(function() { self.close(); }).each(function() {
        $("select, input, textarea", $(this).parent()).focus(function() {
            self.open($("a.help", $(this).parent()));
        }).blur(function() { self.close(); });
    }).click(function(e) {
        $(this).focus();
        e.preventDefault();
    });
}


// expand forms as user checks options
initExpandingForms = function(root) {	
	var self = this;
	if(!root) root = $(document);
	$("ul.options li", root).each(function() {
		var option = $(">label input", $(this));
		if(!option.attr("checked")) $(">fieldset", $(this)).hide();
		option.click(function() {
			var type = $(this).attr("type");
			if(type == "checkbox")
				if($(this).attr("checked")) $(">fieldset", $(this).parent().parent()).show();
				else $(">fieldset", $(this).parent().parent()).hide();
			if(type == "radio") {
				var name = $(this).attr("name");
				$("li>fieldset", $(this).parent().parent().parent()).hide();
				$(">fieldset", $(this).parent().parent()).show();
			}				
		});
	});
}

// extra functions of the order form
initOrderForm = function() {
	var self = this;
	// subscription type sub choice
	this.subscriptionChange = function() {
		if($("#subscription-type input").eq(0).attr("checked")) { //postpaid
			$("#customernumber").show();
			$("#abonnementstext").show();
			if($("#contract-type input").eq(0).attr("checked")) {
			    $("#sim-cardnumber").hide();
			}
			else if($("#contract-type input").eq(1).attr("checked")) {
			    $("#sim-cardnumber").show();
			    $("#customernumber").hide();
			}
		} else { //prepaid
			$("#customernumber").hide();
			$("#abonnementstext").hide();
			$("#sim-cardnumber").show();
		}
	}
	$("#subscription-type input").eq(0).click(function() { self.subscriptionChange(); });
	$("#subscription-type input").eq(1).click(function() { self.subscriptionChange(); });
	this.subscriptionChange();
	// polulate summary
}

// collapsable faqs
initFaqs = function() {
	var self = this;
	var hash = document.location.hash.split("#")[1];
	$("ul.faqs li").each(function() {
		if($(this).attr("id") && $(this).attr("id") == hash) $(this).removeClass("closed");
		else $(this).addClass("closed");
		$("a", $(this)).click(function(event) {
			$(this).parent().toggleClass("closed");	
			event.preventDefault();	
		});
	});
}
