﻿var ie6 = isIE6();
var ie7 = isIE7();
var ie8 = isIE8();
$(document).ready(function(){ 
    //Cufon.replace('.menu ul li a.theFirst span.m-c em', { fontFamily: 'Frutiger', hover: true, hoverables: {a:true, em:true, span:true}});
    $(".accordion").accordion();
    $(".mail-form input[type=text], .mail-form textarea").focus(function(){$(this).addClass("focused");}).blur(function(){$(this).removeClass("focused");});
    if (ie6) {
        $(".mfButn").hover(function(){$(this).addClass("hovera");}, function(){$(this).removeClass("hovera");});
    }
    $(".dbtn a").hover(function(){$(this).parent().addClass("dhover");}, function(){$(this).parent().removeClass("dhover");});
    $(".mfButn").mousedown(function(){$(this).addClass("down");}).mouseup(function(){$(this).removeClass("down");});
    var index_f = parseInt($("#ctl00_InfoRight_KurserList1_ctl00_ctl00_hfCategoryIndex").val());
    if (!index_f || (index_f > 4)) index_f = 0;
    $(".accordion-small").accordion({active: index_f});
    $('.f-level li .sub table td ul').each(function(){
        lii = $(this).find("li").length;
        if (lii == 0) {
            $(this).parent().remove();
        }
    });
    $('.f-level li').hover(function() {
        if (this.timer) clearTimeout(this.timer);
        $(this).addClass("hover");
        if (!ie8)
            $(this).children('div').slideDown();
        else
            $(this).children('div').show();
        $(this).find(".sub").each(function(){
            leng = $(this).find("td").length;
            if (leng == 2) {
                $(this).css("left", "-130px");
            }
            if (leng == 3) {
                $(this).css("left", "-250px");
            }
        });
        if (ie7 || ie6) {
            $(this).find(".sub").each(function(){
                wdth = $(this).find("table").width();
                $(this).find(".subt .subtc").css("width", wdth + "px");
                $(this).find(".subb .subbc").css("width", wdth + "px");
            });
        }
    }, function() {
        var _this = this;
        if ($(this).parent().is('.f-level')) {
            this.timer = setTimeout(function() {
                $(_this).removeClass("hover");
                if (!ie8)
                    $(_this).children('div').slideUp();
                else 
                    $(_this).children('div').hide();
            }, 200);
        } else {
            $(_this).removeClass("hover");
            if (!ie8)
                $(_this).children('div').slideUp();
            else
                $(_this).children('div').hide();
        }
    }); 
    $('.f-level li a').mousedown(function(){$(this).addClass("active");}).mouseup(function(){$(this).removeClass("active");}).mouseover(function(){$(this).addClass("hover");}).mouseout(function(){$(this).removeClass("hover");});
    $('.f-level li a.active').mouseover(function(){$(this).removeClass("hover");}).mouseup(function(){$(this).addClass("active");});
    $('.f-level li div ul li a').mousedown(function(){$(this).addClass("actives");}).mouseup(function(){$(this).removeClass("actives");}).mouseover(function(){ $(this).addClass("hover");}).mouseout(function(){$(this).removeClass("hover")});
    $(".items .item").hover(function(){$(this).css("background-color", "#e9e9e9");}, function(){$(this).css("background-color", "#FFFFFF");});
    $(".btn").hover(function(){$(this).addClass("hover");}, function(){$(this).removeClass("hover");}).mousedown(function(){$(this).addClass("active");}).mouseup(function(){$(this).removeClass("active");});
    $(".popup-fields").focus(function(){$(this).css("border-color", "#636363");}).blur(function(){$(this).css("border-color", "#c3c3c3");});
    slider();
    var loc_path = location.href.toString();
    if (loc_path.indexOf("cmspagemode=edit") == -1) sliderBanner();
    else {
        $(".images ul li").css("margin", "50px 0");
        $(".images ul li").css("display", "list-item");
        $(".women").hide();
    }
    var height_items = parseInt($(".items").height());
    if (ie6 === true) {
        $(".ui-state-default").hover(function(){$(this).addClass("ui-state-hover");$(this).removeClass("ui-state-default");}, function(){$(this).removeClass("ui-state-hover");$(this).addClass("ui-state-default");});
        $(".skicka").hover(function(){$(this).css("background-position", "0 -46px");}, function(){$(this).css("background-position", "0 0");}).mousedown(function(){$(this).css("background-position", "0 -92px");}).mouseup(function(){$(this).css("background-position", "0 -46px");});
        if (height_items > 460) {$("div.scrollable div.items .item").css("width", "190px");}
    }
    if (ie7 === true) {if (height_items > 460) {$("div.scrollable div.items .item").css("width", "190px");}}
});
var count = 1;
var c_all = 0;
var count_banner = 1;
var c_all_banner = 0;
function slider() {
    li_list = $(".logos-box").find("li");
    len = li_list.length;
    if (len < 2) return false;
    c_all = len;
    cc = 1;
    $(".logos-box").find("li").each(function() {$(this).css("display", "none");});
    $(".logos-box").find("li:first").css("display", "list-item");
    count = 2;
    setInterval('showNewLogos()', 3000);
}
function showNewLogos() {
    cc = 1;
    $(".logos-box").find("li").each(function() {
        if (cc == count) {$(this).fadeIn("slow"); } else $(this).css("display", "none");
        cc++;
    });
    if (count > c_all) {
        count = 2;
        $(".logos-box").find("li:last").css("display", "none");
        $(".logos-box").find("li:first").fadeIn("slow");
    }
    else count += 1;
}
function sliderBanner() {
    li_list = $(".images").find("li");
    len = li_list.length;
    if (len < 2) return false;
    c_all_banner = len;
    cc = 1;
    $(".images").find("li").each(function() {$(this).css("display", "none");});
    $(".images").find("li:first").css("display", "list-item");
    count_banner = 2;
    setInterval('showNewBanners()', 4000);
}
function showNewBanners() {
    cc = 1;
    $(".images").find("li").each(function() {
        if (cc == count_banner) {$(this).fadeIn("slow"); } else $(this).css("display", "none");
        cc++;
    });
    if (count_banner > c_all_banner) {
        count_banner = 2;
        $(".images").find("li:last").css("display", "none");
        $(".images").find("li:first").fadeIn("slow");
    }
    else count_banner += 1;
}
function isIE6() {
   if (navigator.userAgent.toString().indexOf("MSIE 6.0") != -1) return true;
   return false;
}
function isIE7() {
   if (navigator.userAgent.toString().indexOf("MSIE 7.0") != -1) return true;
   return false;
}
function isIE8() {
   if (navigator.userAgent.toString().indexOf("MSIE 8.0") != -1) return true;
   return false;
}
function openPopup(id) {
    if (document.getElementById(id) == null) return false;
    setHeaderTextInField();
    $("#" + id).css("display", "block");
}
function closePopup(id) {
    if (document.getElementById(id) == null) return false;
    $("#" + id).css("display", "none");
    clearForm("#" + id);
}
function setRedBorder(obj) {
    if (obj == null) return false;
    $(obj).css("border-color", "#FF0000");
}
function setGrayBorder(obj) {
    if (obj == null) return false;
    $(obj).css("border-color", "#c3c3c3");
}
function getHeaderModulaText() {
    var txt_tmp = "";
    collect = $("h1 span.hgray");
    if (collect.length == 1) {
        txt_tmp += collect[0].innerHTML + ", ";
    }
    collect = $("h1 span.horange");
    if (collect.length == 1) {
        txt_tmp += collect[0].innerHTML;
    } 
    return txt_tmp;
}
function setHeaderTextInField() {
    $(".popup").find(".popup-fields:first").each(function(){
        $(this).val(getHeaderModulaText());
    });
}
function showError(indicator, text) {
    if (!indicator || !text) return false;
    $(indicator).html(text);
    $(indicator).css("display", "block");
}
function hideError(indicator) {
    if (!indicator) return false;
    $(indicator).css("display", "none");
    $(indicator).html("");
}
function clearForm(obj_handle) {
    if (!obj_handle) return false;
    $(obj_handle).find(".popup-fields").each(function(){
        $(this).val(""); setGrayBorder(this); hideError(obj_handle + " .success"); hideError(obj_handle + " .error");
    });
}
function validateMailForm() {
    var count_errors = 0;
    hideError(".popup .error");
    $(".popup").find(".popup-fields").each(function(){setGrayBorder(this);});
    $(".popup").find(".required").each(function(){
        var val = $(this).val().toString();
        regEmpty = /^(\s)+$/; 
        if (!val.length) {count_errors += 1; setRedBorder(this);}
        else if (regEmpty.test(val)) {count_errors += 1; setRedBorder(this);}
    });
    $(".popup").find(".email").each(function(){
        val = $(this).val();
        regMail = /^(\s)*[a-zA-Z0-9_\-]+@([a-zA-Z0-9_\-]+\.)+[a-zA-Z]{2,6}(\s)*$/; 
        if (!val) {count_errors += 1; setRedBorder(this);}
        else if (!regMail.test(val)) {count_errors += 1; setRedBorder(this);}
    });
    if (count_errors == 0) return true;
    return false;
}
function onSuccSendMail() {
    closePopup('popupMail'); 
    hideError('.popup .success');
}
function sendMail() {
    v = validateMailForm();
    if (!v) {
        text = "Fyll i alla fält för att vi ska kunna kontakta dig";
        showError(".popup .error", text); 
        return false;   
    }
    else {
        showPreloader();
        name = $("#pName").val();
        phone = $("#pPhone").val();
        email = $("#pEmail").val();
        kurs = $("#pKurs").val();
        HelperService.BookCourse(kurs, name, phone, email, onCompleteSending, onErrorSending, onErrorTimeout);       
    }
}
function onCompleteSending(arg) {
        hidePreloader();
        text = "E-post skickas";
        showError(".popup .success", text);
        setTimeout("onSuccSendMail()", 1500);    
}
function onErrorSending(arg) {
        hidePreloader();
        text = "Fel Data";
        showError(".popup .error", text);
}
function onErrorTimeout(arg) {
        hidePreloader();
        text = "Response timeout passerat";
        showError(".popup .error", text);
}
function showPreloader() {
    /*  place for ajax sending email  */
    $(".popup .btn").css({ opacity: 0.0 }); // remove input
    $(".popup .stumb div").css("display", "block"); // while mail is sending, lock button
}
function hidePreloader() {
    $(".popup .btn").css({ opacity: 1.0 });
    $(".popup .stumb div").css("display", "none");
}
function fixTrans(img_stump){if (typeof document.body.style.maxHeight == 'undefined') {var imgs = document.getElementsByTagName("img");for (i = 0; i < imgs.length; i++){if (imgs[i].src.indexOf(img_stump.toString()) != -1){return;}if (imgs[i].src.indexOf(".png") != -1){var src = imgs[i].src;imgs[i].src = img_stump.toString();imgs[i].runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "',sizingMethod='scale')";imgs[i].style.display = "inline-block";}}}}
