/// <reference path="jquery-1.3.2.js" />

(function ($) {
    //jQuery plugin
    //gives tab-functionality to a list with its tab-container
    $.fn.makeTabs = function () {
        if (this.hasTabs) { return; }
        this.hasTabs = true;

        var containers = $(this).nextAll().hide();
        var tabs = $(this).children().find("a");

        var foundActive = 0;
        tabs
			.each(function (i, val) {
			    this.container = containers[i] ? containers[i] : $("<div/>");
			    this.show = function () {
			        tabs.each(function () {
			            this.hide();
			        });
			        $(this.container).show();
			        $(this).parents("li").addClass("active");
			    }
			    this.hide = function () {
			        $(this.container).hide();
			        $(this).parents("li").removeClass("active");
			    }
			    if (!foundActive && $(this).parents("li").hasClass("active")) {
			        foundActive = i;
			    }
			    else {
			        this.hide();
			    }
			});

        this.containers = containers;
        this.tabs = tabs;

        if (tabs[foundActive]) {
            tabs[0].show();
        }
        return $(this);
    }

    //use this to activate a tab
    $.fn.showTab = function (i) {
        if (this.hasTabs && this.tabs[i]) {
            this.tabs[i].show();
        }

        return $(this);
    }

    //decides if the user uses a shitbrowser or not
    $.shitbrowser = ($.browser.msie && parseInt($.browser.version) < 7);
    $.semishitbrowser = ($.browser.msie && parseInt($.browser.version) == 7);
    $.aftersemishitbrowser = ($.browser.msie && parseInt($.browser.version) == 8);

    //expander is like an accordeon but can has more than one item opened
    $.fn.expander = function () {
        if (this.isExpander) { return; }
        this.isExpander = true;

        $(this).children(".expander-header").find("a")
			.click(function (e) {
			    e.preventDefault();
			    if ($(this).hasClass("closed")) {
			        $(this).parent().next().slideDown("fast", function () {
			            $(this).prev().children().removeClass("closed");
			        });
			    }
			    else {
			        $(this).parent().next().slideUp("fast", function () {
			            $(this).prev().children().addClass("closed");
			        });
			    }
			});

        //Default kein Tab geöffnet
        //$(this).children(".expander-header:first").find("a").click();

    }

    //use to display hints in inputfields, via title-attribute
    $.fn.inputField = function () {
        if (!$(this).attr("title") || this.isMarked) {
            return $(this);
        }
        this.isMarked = true;
        $(this)
			.focus(function () {
			    if (!this.changed) {
			        $(this).val("").removeClass("hint");
			    }
			})
			.blur(function () {
			    if (!this.changed) {
			        $(this).val($(this).attr("title")).addClass("hint");
			    }
			})
			.keyup(function () {
			    this.changed = ($(this).val() ? true : false);
			})
			.keyup()
			.blur()
			.each(function () {
			    if ($(this).val() == $(this).attr("title")) {
			        this.changed = false;
			    }
			});
        return $(this);
    }

    $.dropdowns = [];
    $.fn.recreateDropdown = function () {

        if (this.isDropdown) return $(this);
        this.isDropdown = true;
        var dd = this;
        var options = [];
        var dropTitle = $("<div class='drop-title'/>");
        var dropTitleText = $("<span/>")
        dropTitle.append(dropTitleText);
        var dropList = $("<ul class='drop-list'/>").hide();
        $("option", this).each(function () {
            var opt = this;
            dropList
				.append(
					$("<li/>")
						.append(
							$("<a href='#'/>")
								.each(function () {
								    this.partner = opt;
								    this.partner.partner = this;
								    options.push(opt);
								})
								.click(function (e) {
								    e.preventDefault();
								    $("option", dd).removeAttr("selected");
								    $(opt).attr("selected", "selected");
								    dd.close();
								    dd.updateDropList();
								})
						)
				);
        });
        this.updateDropList = function () {
            $(this.options).each(function () {
                $(this.partner).text($(this).text());
            });
            var selected = $(this).find(":selected")
            dropTitleText.text(selected.text());
            if (selected.attr("value") == "") {
                dropTitleText.addClass("hint");
            }
            else {
                dropTitleText.removeClass("hint");
            }
            $(dd).change();
        }
        this.options = options;
        this.dropList = dropList;
        this.dropTitle = dropTitle;
        $(this.dropTitle).each(function () {
            this.dropdown = dd;
        });
        this.dropTitleText = dropTitleText;
        var dropMenu = this.parents('div.dropdown').children('.drop-menu');
        //ToDo: Unbind oder Unload, löschen

        dropMenu.empty();
        dropMenu.append(dropTitle).append(dropList);

        this.updateDropList();

        this.open = function () {
            $.closeAllDrops();
            this.dropList.show().parents().css("z-index", 100);
            this.dropTitle.addClass("open")
        }

        this.close = function () {
            this.dropList.hide().parents().css("z-index", "");
            this.dropTitle.removeClass("open")
        }

        this.dropTitle.click(function () {
            if (!$(this).hasClass("open")) {
                this.dropdown.open();
            }
            else {
                this.dropdown.close();
            }
        });

    }
    $.fn.dropdown = function () {
        if (this.isDropdown) return $(this);
        this.isDropdown = true;
        var dd = this;
        var options = [];
        var dropTitle = $("<div class='drop-title'/>");
        var dropTitleText = $("<span/>")
        dropTitle.append(dropTitleText);
        var dropList = $("<ul class='drop-list'/>").hide();
        $("option", this).each(function () {
            var opt = this;
            dropList
				.append(
					$("<li/>")
						.append(
							$("<a href='#'/>")
								.each(function () {
								    this.partner = opt;
								    this.partner.partner = this;
								    options.push(opt);
								})
								.click(function (e) {
								    e.preventDefault();
								    $("option", dd).removeAttr("selected");
								    $(opt).attr("selected", "selected");
								    dd.close();
								    dd.updateDropList();
								})
						)
				);
        });
        this.updateDropList = function () {
            $(this.options).each(function () {
                $(this.partner).text($(this).text());
            });
            var selected = $(this).find(":selected")
            dropTitleText.text(selected.text());
            if (selected.attr("value") == "") {
                dropTitleText.addClass("hint");
            }
            else {
                dropTitleText.removeClass("hint");
            }
            $(dd).change();
        }
        this.options = options;
        this.dropList = dropList;
        this.dropTitle = dropTitle;
        $(this.dropTitle).each(function () {
            this.dropdown = dd;
        });
        this.dropTitleText = dropTitleText;
        var dropWrap = $("<div class='drop-wrap'/>");
        var dropMenu = $("<div class='drop-menu'/>");
        $(this).wrap(dropWrap).parent().after(dropMenu);
        dropMenu.append(dropTitle).append(dropList);
        this.updateDropList();

        this.open = function () {
            $.closeAllDrops();
            this.dropList.show().parents().css("z-index", 100);
            this.dropTitle.addClass("open")
        }

        this.close = function () {
            this.dropList.hide().parents().css("z-index", "");
            this.dropTitle.removeClass("open")
        }

        this.dropTitle.click(function () {
            if (!$(this).hasClass("open")) {
                this.dropdown.open();
            }
            else {
                this.dropdown.close();
            }
        });


        //adjust IE6
        if ($.shitbrowser) {
            var dropWidth = parseInt(dropTitleText.css("width")) + parseInt(dropTitleText.css("padding-right"));
            dropTitleText.width(dropWidth);
            $(this.dropList)
				.show()
				.width($(this.dropList).outerWidth())
				.each(function () {
				    if ($(this).height() > 120) {
				        $(this).height(120);
				    }
				})
				.hide();
            $("li", this.dropList)
				.mouseover(function () { $(this).children().addClass("hover"); })
				.mouseout(function () { $(this).children().removeClass("hover"); })
				.each(function () {
				    $(this).css("margin-bottom", -1)
				    $("a", this).css("height", 1);
				});
        }

        //adjust IE7
        if ($.semishitbrowser) {
            $(this.dropList)
				.show()
				.width($(this.dropList).outerWidth())
				.each(function () {
				    if ($(this).height() > 120) {
				        $(this).width($(this).outerWidth());
				        $(this).height(120);
				    }
				})
				.hide();
        }

        $.dropdowns.push(this);

        //prevent bubbeling click events
        $(dropMenu).click(function (e) {
            e.stopPropagation();
        });
    }

    $.closeAllDrops = function () {
        $($.dropdowns).each(function () {
            this.close();
        });
    }
    $(document).click(function () {
        $.closeAllDrops();
    });

})(jQuery);


(function ($){
	// document onDomReady init
	$(function(){
	    
	    $('#subject a[href*=#]').each(function() {
	        var $link = $(this);
	        var href = $link.attr('href');
	        var pos = href.lastIndexOf('#');
	        if (pos > 0) {
	            $link.attr('href', href.substr(0, pos) + '?x=' + Math.random() + href.substr(pos));
	        }
	    });
		
		//make tabs
		var foundTabs = false;
		$("ul.tabs").each(function(){
			$(this).makeTabs();
			foundTabs = true;
		});	
		//history safe tabs
		if(foundTabs){
			$(".tabs a").history(function(){
				this.show();
			});
			$.ajaxHistory.initialize();
		}
		
		//make expander
		var foundExpander = false;
	    $(".expander").each(function(){		    
		    $(this).expander();
		    foundExpander = true;
	    });	
	    //history safe expander
	    if(foundExpander)
	    {
	        $(".expander a.history").history(function(){
			   this.click();
		    });
		    $.ajaxHistory.initialize();
	    }
		
		//initialize form fields
		$(".form-textline input.text").each(function(){
			$(this).inputField();
		});
		
		//initialize dropdowns
		$(".dropdown select").each(function(){
			$(this).dropdown();
		});
		
		//initialize checkboxes
		var updateCbLabel = function(){
			if(this.checked){
				$(this.label).addClass("perfectcheckbox-checked");
			}
			else{
				$(this.label).removeClass("perfectcheckbox-checked");
			}
        }

        // added by garaio, 16.06.09
        // prepares checkboxes
        var prepareCb = function() {
            if ($(this).is(":disabled")) {
                $(this.label).addClass("perfectcheckbox-disabled");
                $('label[for=' + this.id + ']').addClass("perfectcheckbox-disabled");
            }
            else {
                $(this)
				    .change(updateCbLabel)
				    .click(updateCbLabel) //IE6 hack -> wrong event firing
				    .focus(function() {
				        $(this.label).addClass("perfectcheckbox-focus");
				    })
				    .blur(function() {
				        $(this.label).removeClass("perfectcheckbox-focus");
				    })
				    .each(updateCbLabel);
            }
        }

        // added by garaio, 16.06.09
        // checkboxes wich separates checkbox-control from label
        $("input.perfectcheckbox-box").each(function() {
            var chbx = this;
            var $label = $(chbx).find('label.perfectcheckbox-box');
            // check if label exists
            if (!$label.get(0)) {
                $label = $('<label for=\'' + chbx.id + '\'>&#160;</label>');
                // style the label as checkbox
                $label.addClass('perfectcheckbox-box');
            }
            // set label on checkbox
            chbx.label = $label.get(0);
            // insert the label before the real input control
            $label.insertBefore($(chbx));

            $(chbx).each(prepareCb);
        });
		
		$("label.perfectcheckbox").each(function(){
			var chbx = $("#"+$(this).attr("for")).get(0);
			chbx.label = this;
			this.checkbox = chbx;

			// changed by garaio, 16.06.09
			// use a function instead of inline code
			$(chbx).each(prepareCb)				
		});
		
		//initialize radios		
		var radioGroupNames = [];//get all radio groupnames of perfect radios

		// changed by garaio, 16.06.09 from $("input.perfectradio").each(function(){ to the following
		// selector now takes input types of two classes
		$("input.perfectradio,input.perfectradio-box").each(function() {
			if((","+radioGroupNames.toString()).indexOf(this.name)<1){
				radioGroupNames.push(this.name);
			}
        });
		
		var updateLabels = function(){ //this function updates the label-class according the checked-state of the relative 
			$(this.group).each(function(){
				if(this.checked){
					$(this.label).addClass("perfectradio-checked");
				}
				else{
					$(this.label).removeClass("perfectradio-checked");
				}
			});
        }

        // added by garaio, 16.06.09
        // prepares radiobuttons
        var prepareRd = function() {
            if ($(this).is(":disabled")) {
                $(this.label).addClass("perfectradio-disabled");
            }
            else {
                $(this)
					.change(updateLabels)
					.click(updateLabels)	//IE6 hack -> wrong event firing
					.focus(function() {
					    $(this.label).addClass("perfectradio-focus");
					})
					.blur(function() {
					    $(this.label).removeClass("perfectradio-focus");
					}).each(updateLabels);
            }
        }


        for (var i = 0; i < radioGroupNames.length; i++) { //create radio groups and init the states
            // changed by garaio, 16.06.09, selector now takes input types of two classes
            var grp = $("input[name='" + radioGroupNames[i] + "']");
            grp.filter("input[class='perfectradio']").each(function() {
                this.group = grp;
                this.label = $("label[for='" + this.id + "']").get(0);
                this.label.radio = this;
                
                // changed by garaio, 16.06.09
			    // use a function instead of inline code
                $(this).each(prepareRd);
            })
            // added by garaio, 16.06.09
            .end().filter("input.perfectradio-box").each(function() {
                var rdo = this;
                rdo.group = grp;
                var $label = $(rdo).find('label.perfectradio-box');

                // check if label exists
                if (!$label.get(0)) {
                    $label = $('<label for=\'' + rdo.id + '\'>&#160;</label>');
                    // style the label as radio
                    $label.addClass('perfectradio-box');
                }
                // set label on radio
                rdo.label = $label.get(0);
                // insert the label before the real input control
                $label.insertBefore($(rdo));

                $(rdo).each(prepareRd);
            });
        }
		
		//Initialize Lightbox
		$('a.lightbox').lightbox();
		
	});
})(jQuery); 
