// Just a couple browser tests
var is_chrome = /chrome/.test(navigator.userAgent.toLowerCase());
var is_win = /windows/.test(navigator.userAgent.toLowerCase());
var is_firefox = /firefox/.test(navigator.userAgent.toLowerCase());

var $jq = jQuery.noConflict();

jQuery.fn.extend({
    /**
    * Returns get parameters.
    *
    * If the desired param does not exist, null will be returned
    *
    * To get the document params:
    * @example value = $(document).getUrlParam("paramName");
    * 
    * To get the params of a html-attribut (uses src attribute)
    * @example value = $('#imgLink').getUrlParam("paramName");
    */
    getUrlParam: function(strParamName) {
        strParamName = escape(unescape(strParamName));

        var returnVal = new Array();
        var qString = null;

        if ($jq(this).attr("nodeName") == "#document") {
            //document-handler

            if (window.location.search.search(strParamName) > -1) {

                qString = window.location.search.substr(1, window.location.search.length).split("&");
            }

        } else if ($jq(this).attr("src") != "undefined") {

            var strHref = $jq(this).attr("src")
            if (strHref.indexOf("?") > -1) {
                var strQueryString = strHref.substr(strHref.indexOf("?") + 1);
                qString = strQueryString.split("&");
            }
        } else if ($(this).attr("href") != "undefined") {

            var strHref = $(this).attr("href")
            if (strHref.indexOf("?") > -1) {
                var strQueryString = strHref.substr(strHref.indexOf("?") + 1);
                qString = strQueryString.split("&");
            }
        } else {
            return null;
        }


        if (qString == null) return null;


        for (var i = 0; i < qString.length; i++) {
            if (escape(unescape(qString[i].split("=")[0])) == strParamName) {
                returnVal.push(qString[i].split("=")[1]);
            }

        }


        if (returnVal.length == 0) return null;
        else if (returnVal.length == 1) return returnVal[0];
        else return returnVal;
    }
});




function clickIE4() {
    if (event.button == 2) {
        
        return false;
    }
}

function clickNS4(e) {
    if (document.layers || document.getElementById && !document.all) {
        if (e.which == 2 || e.which == 3) {
            
            return false;
        }
    }
}

if (document.layers) {
    document.captureEvents(Event.MOUSEDOWN);
    document.onmousedown = clickNS4;
}
else if (document.all && !document.getElementById) {
    document.onmousedown = clickIE4;
}

document.oncontextmenu = new Function("return false")



$jq(function() {
    // Browsers
    if ($jq.browser.safari) $jq("body").addClass("sfr");
    if (is_chrome) $jq("body").addClass("chr");
    if (is_win && is_firefox) $jq("body").addClass("ffwin");


    // Input fields
    $jq("input[type=text], input[type=password]").addClass("txt");
    $jq("input[type=checkbox]").addClass("chk");
    if ($jq.browser.msie) $jq("select.error").wrap('<span class="error"></span>');


    // Enable tabs
    $jq(".tabbed-content").tabbed();


    // Video teaser equal height
    $jq(".video-teaser > div").equalHeight();


    // Fix IE bug on floated list
    $jq("ul.tripple-img li.clear, ul.double-img li.clear").before('<li style="clear: both; float: none;"></li>');


    // News scrolling
    $jq(".news-scroll-cont").newsScroller({
        controls: $jq(".news-scroll")
    });


    // Subnav IE6 fix
    $jq("#subnav li.on.open").addClass("on-open");


    // Explorer accordion
    $jq("#enav li a").hover(
		 function() { $jq(this).parent("li").addClass("hover"); }
		, function() { $jq(this).parent("li").removeClass("hover"); }
	);
    //    $jq("#enav").accordion();


    // Accordion
    if(!($jq.browser.msie && $jq.browser.version.substr(0,1)<7)){
        $jq(".accordion").accordion();
    }


    // Contact ppicker
    $jq(".contacts").contactPicker();


    // Vertical align
    $jq("#cont .reference-logos img").vAlign();


    // Print function
    //    $jq(".tools a.druck").click(function() {
    //        window.print();
    //        return false;
    //    });


    // Button hover
    $jq("button, input[type=submit,button]").hover(
		 function() { $jq(this).addClass("hover"); }
		, function() { $jq(this).removeClass("hover"); }
	);
    $jq("a.btn, a.btn em").hover(
		 function() { $jq(this).addClass("btnhover"); }
		, function() { $jq(this).removeClass("btnhover"); }
	);


    // Rouded corners, but only for modern browsers
    /*$jq("input[type=text], input[type=password]").css({
    "-moz-border-radius": 		"3px"
    ,"-webkit-border-radius": 	"3px"
    });*/


    // Click on search field
    $jq("#search input").focus(function() {
        if (!$jq(this).attr("rel")) {
            $jq(this).attr("rel", "1");
            $jq(this).val("");
        } //end if
    }).blur(function() {
        if (!$jq(this).val()) {
            //            $jq(this).val("Suchbegriff");
            $jq(this).removeAttr("rel");
        } //end if
    });

    $jq("#txtPassword").focus(function() {
        if (!$jq(this).attr("rel")) {
            $jq(this).attr("rel", "1");
            $jq(this).val("");
            //$jq(this).attr("type","password");
        } //end if
    }).blur(function() {
        if (!$jq(this).val()) {
            $jq(this).val("Passwort");
            $jq(this).removeAttr("rel");
            //$jq(this).attr("type", "text");
        } //end if
    });

    $jq("#txtUserName").focus(function() {
        if (!$jq(this).attr("rel")) {
            $jq(this).attr("rel", "1");
            $jq(this).val("");
        } //end if
    }).blur(function() {
        if (!$jq(this).val()) {
            $jq(this).val("Benutzername");
            $jq(this).removeAttr("rel");
        } //end if
    });


    // Fancybox
    $jq(".gallery a.img, .txt-img a.img").fancybox({
        'imageScale': true,
        'padding': 10,
        'zoomOpacity': true,
        'zoomSpeedIn': 400,
        'zoomSpeedOut': 400,
        'overlayShow': true,
        'overlayOpacity': 0.5,
        'hideOnContentClick': false,
        'centerOnScroll': true,
        'callbackOnShow': function() {
            jQuery("#fancy_inner").css({
                width: jQuery("#fancy_outer").css("width"),
                height: jQuery("#fancy_outer").css("height")
            });
            var w = jQuery.fn.fancybox.getViewport();
            var ow = jQuery("#fancy_outer").outerWidth();
            var oh = jQuery("#fancy_outer").outerHeight();
            var pos = {
                'top': (oh > w[1] ? w[3] : w[3] + Math.round((w[1] - oh) * 0.5)),
                'left': (ow > w[0] ? w[2] : w[2] + Math.round((w[0] - ow) * 0.5))
            };
            jQuery("#fancy_outer").css(pos);
            jQuery('#fancy_title').css({
                'top': pos.top + oh - 32,
                'left': pos.left + 19,
                'width': jQuery("#fancy_inner").outerWidth() - 2
            });
        }
    });
    $jq(".gallery a.zoom, .txt-img a.zoom").click(function() {
        $jq(this).prev("a.img").click();
        return false;
    });

    // Fancybox iframe
    $jq(".iframe").each(function() {
        var w = ($jq(this).attr("frameWidth") != "") ? $jq(this).attr("frameWidth") : 916;
        var h = ($jq(this).attr("frameHeight") != "") ? $jq(this).attr("frameHeight") : 544;
        $jq(this).fancybox({
            'padding': 0,
            'frameWidth': w,
            'frameHeight': h,
            'overlayShow': true,
            'overlayOpacity': 0.5,
            'hideOnContentClick': false,
            'centerOnScroll': false,
            'callbackOnShow': function() {
                jQuery("#fancy_inner").css({
                    width: jQuery("#fancy_outer").css("width"),
                    height: jQuery("#fancy_outer").css("height")
                });
                var w = jQuery.fn.fancybox.getViewport();
                var ow = jQuery("#fancy_outer").outerWidth();
                var oh = jQuery("#fancy_outer").outerHeight();
                var pos = {
                    'top': (oh > w[1] ? w[3] : w[3] + Math.round((w[1] - oh) * 0.5)),
                    'left': (ow > w[0] ? w[2] : w[2] + Math.round((w[0] - ow) * 0.5))
                };
                jQuery("#fancy_outer").css(pos);
                jQuery('#fancy_title').css({
                    'top': pos.top + oh - 32,
                    'left': pos.left + 19,
                    'width': jQuery("#fancy_inner").outerWidth() - 2
                });
            }
        });
    });

    var fancyboxproxy = jQuery.fn.fancybox.showIframe;
    jQuery.fn.fancybox.showIframe = function() {
        try { fancyboxproxy(); } catch (e) { }
        jQuery(document).trigger("fancyIframeLoad");
    };

    jQuery(document).bind("fancyIframeLoad", function(param) {
        window.setTimeout(function() {
            var helpFrame = jQuery("#fancy_frame");
            var innerDoc = (helpFrame.get(0).contentDocument) ? helpFrame.get(0).contentDocument : helpFrame.get(0).contentWindow.document;
            var isMaps = (jQuery("#fancy_frame").contents().find('#GMap1').length == 1);

            if (jQuery(innerDoc.body).find(".page_margins").length > 0 || isMaps) {
                var h, w;
                if (isMaps) {
                    h = jQuery("#fancy_frame").contents().find('#GMap1').height() + 20;
                    w = jQuery("#fancy_frame").contents().find('#GMap1').width() + 20;
                    jQuery("#fancy_content").css({ 'margin': 10 });

                } else {
                    h = parseInt(jQuery(innerDoc.body).find(".page_margins").outerHeight(true)) + 30;
                    w = jQuery(innerDoc.body).find(".page_margins").outerWidth(true);
                }

                jQuery("#fancy_frame").parent().animate({
                    height: h - ((isMaps)? 20 : 0),
                    width: w - ((isMaps) ? 20 : 0)
                }, 0);
                jQuery("#fancy_inner,#fancy_outer").animate({
                    width: w,
                    height: h
                }, 0, function() {
                    jQuery("#fancy_inner").css({
                        width: jQuery("#fancy_outer").css("width"),
                        height: jQuery("#fancy_outer").css("height")
                    });
                    var w = jQuery.fn.fancybox.getViewport();
                    var ow = jQuery("#fancy_outer").outerWidth();
                    var oh = jQuery("#fancy_outer").outerHeight();
                    var pos = {
                        'top': (oh > w[1] ? w[3] : w[3] + Math.round((w[1] - oh) * 0.5)),
                        'left': (ow > w[0] ? w[2] : w[2] + Math.round((w[0] - ow) * 0.5))
                    };
                    jQuery("#fancy_outer").css(pos);
                    jQuery('#fancy_title').css({
                        'top': pos.top + oh - 32,
                        'left': pos.left + 19,
                        'width': jQuery("#fancy_inner").outerWidth() - 2
                    });
                });
            }
        }, 50);
    });


    $jq("#enav li li a").click(function() {
        // Because of a bug in IE8
        if ($jq(this).attr("href") != "#") document.location = $jq(this).attr("href");
        return false;
    })

    // Fancybox hover
    $jq("div#fancy_close").hover(
		 function() { $jq(this).addClass("fancy_close_hover"); }
		, function() { $jq(this).removeClass("fancy_close_hover"); }
	);


    // Add action to meta navi
    $jq("#language-pick select").change(function() {
        if ($jq(this).val()) document.location = '?sc_lang=' + $jq(this).val();
    });

    $jq("#passwort-frame form a.inf").hover(
		 function() { $jq("#passwort-frame .nachricht").fadeIn(200); }
		, function() { $jq("#passwort-frame .nachricht").fadeOut(100); }
	);


    //    $jq("#login-pick select").click(function(){
    //        window.open($jq(this).val());
    //    });

    $jq("#login-pick select").change(function() {

        //window.location.href = $jq(this).val();
        setTimeout(function() {

            var vals = [];
            var texts = [];
            $jq("#login-pick select option").each(function() {
                vals[vals.length] = $jq(this).val().replace(/\s+/, '');
                texts[texts.length] = $jq(this).text().replace(/\s+/, '');
            });

            //            console.debug(texts);
            //            console.debug(vals);

            var cur = $jq("#login-pick").find(".newListSelected .selectedTxt").text().replace(/\s+/, '');
            for (var j in texts) {
                if (cur == texts[j]) {
                    window.open(vals[j]);
                    // window.location.href = vals[j];
                    break;
                }
            }

            $jq("#login-pick").find(".newListSelected").remove();
            $jq("#login-pick select").get(0).selectedIndex = 0;
            $jq("#login-pick select.ssel").sSelect({

                hideSelected: true
            });
        }, 30);


        //if ($jq(this).val()) document.location = '?sc_site=' + $jq(this).val();
        //        $jq("#login-pick .newListSelected").remove();
        //        $jq("#login-pick select option").removeAttr("selected");
        //        $jq("#login-pick select option:eq(0)").attr("selected", "selected");
        //        $jq("#login-pick select").val(0);
        //        $jq("#login-pick select").sSelect({
        //            hideSelected: true
        //        });
    });

//    // Sortable tables
    $jq("table.sortable").tablesorter({
        sortList: [[0, 1]]
    });

    // Create custom buttons, selectboxes and checkboxes
    custom_checkboxes();
    custom_selectboxes();
    custom_buttons();

    $jq(document).ready(function() {
        var max = 0;
        $jq(".tripple-img h3").css({ display: 'block' }).each(function() {
            if ($jq(this).height() > max) {
                max = $jq(this).height();
            }
        }).css({ height: max });
    });
});




function custom_selectboxes() {
    // Custom select boxes
    $jq("select.ssel").sSelect({
        hideSelected: true
    });

} //end custom_selectboxes()

function custom_checkboxes() {
    // Custom radio buttons and checkboxes
    $jq("input[type=checkbox]").prettyCheckboxes();
    $jq("input[type=radio]").prettyCheckboxes();

} //end custom_checkboxes()

function custom_buttons() {
    $jq("input[type=submit], input[type=reset], input[type=button], button").not("#search button").customButton();
}

