function processExclusions(element) {
    if (this.tagName == "label") {
        element = $("#" + element.attr("for"));
    } else {
        element = $(element);
    }
    if (element.is(':checked')) {
        element.parent().addClass('active');
        element.parent().find('.exclusion').each(function() {
            $('#cloud-config .applist .' + $(this).text()).addClass("disabled");
            $('#cloud-config .applist .' + $(this).text() + ' input').attr("disabled", "disabled")
        });
    } else {
        element.parent().removeClass('active');
        element.parent().find('.exclusion').each(function() {
            var exID = $(this).text();
            var otherFlags = false;
            $("#cloud-config .applist input:checked").parent().find(".exclusion").each(function() {
                if ($(this).text() == exID) {
                    otherFlags = true;
                }
            });
            if (!otherFlags) {
                $('#cloud-config .applist .' + exID).removeClass("disabled");
                $('#cloud-config .applist .' + exID + ' input').attr("disabled", null);
            }
        });
    }
}
$(document).ready(function() {
    // CLOUD
    $('.cloud-section li .title a').click(
            function() {
                var parentLi = $(this).parent().parent();
                if (parentLi.hasClass('open')) {
                    parentLi.addClass('closed');
                    parentLi.removeClass('open');
                    $(this).text('prikaži');
                } else {
                    parentLi.addClass('open');
                    parentLi.removeClass('closed');
                    $(this).text('skrij');
                }
                parentLi.find('div:not(.title)').slideToggle('slow');
                return false;
            }).parent().parent().find('div:not(.title)').hide();
    $('.cloud-section li .title h3').click(
            function() {
                var parentLi = $(this).parent().parent();
                if (parentLi.hasClass('open')) {
                    parentLi.addClass('closed');
                    parentLi.removeClass('open');
                    $(this).parent().find('a').text('prikaži');
                } else {
                    parentLi.addClass('open');
                    parentLi.removeClass('closed');
                    $(this).parent().find('a').text('skrij');
                }
                parentLi.find('div:not(.title)').slideToggle('slow');
                return false;
            }).parent().parent().find('div:not(.title)').hide();
    if ($('.cloud-section').length > 0) {
        if (window.location.href.indexOf("&sid=") > -1) {
            var serviceID = window.location.href.substring(window.location.href.indexOf("&sid=") + 5, window.location.href.length);
            $('.cloud-section').find('.' + serviceID + ' h3').click();
        } else {
            $('.cloud-section .title h3').click();
        }
    }
    $('.sliding').gallerySlider();
    // CLOUD CONFIGURATOR
    if ($("#cloud-config").length > 0) {
        var w = $("#cloud-config");
        if ($(".more-text", w).length > 0) {
            $(".more-text", w).fancybox({'titleShow' : false,'padding' :10});
        }
        $("input:not(.exempt)", w).change(function() {
            processExclusions(this);
        });
        $("input:not(.exempt)", w).click(function() {
            processExclusions(this);
        });
        $("label:not(.exempt)", w).click(function() {
            processExclusions(this);
        });
        $('.top2 a', w).click(function(e) {
            e.preventDefault();
            if (w.hasClass('preset')) {
                calculateOfficeReset();
                calculateOfficeConfig();
                //$(window).scroll();
            }
                var parentLi = $(this).parent().parent();
                if (parentLi.hasClass('open')) {
                    parentLi.find('.applist').slideUp('slow', function() {
                        parentLi.addClass('closed');
                        parentLi.removeClass('open');
                    });
                    $(this).text('prikaži');
                } else {
                    parentLi.addClass('open');
                    parentLi.removeClass('closed');
                    parentLi.find('.applist').slideDown('slow');
                    $(this).text('skrij');
                }


            return false;
        });
        $('.top a', w).click(function(e) {
            if (w.hasClass('preset')) {
                calculateOfficeReset();
                calculateOfficeConfig();
                //$(window).scroll();
            }
            e.preventDefault();
            var parentLi = $(this).parent().parent();
            if (parentLi.hasClass('open')) {
                parentLi.find('.sub-sec').slideUp('slow', function() {
                    parentLi.addClass('closed');
                    parentLi.removeClass('open');
                });
            } else {
                parentLi.addClass('open');
                parentLi.removeClass('closed');
                parentLi.find('.sub-sec').slideDown('slow');
            }
            return false;
        });
        $('.amount .minus', w).click(function(e) {
            e.preventDefault();
            if (!$(this).parents('.applist li').hasClass('disabled')) {
                var currentVal = parseInt($(this).next().val(), 10);
                if (currentVal > 1) {
                    $(this).next().val(currentVal - 1);
                    $(this).next().change();
                }
            }
        });
        $('.amount .plus', w).click(function(e) {
            e.preventDefault();
            if (!$(this).parents('.applist li').hasClass('disabled')) {
                var currentVal = parseInt($(this).prev().val(), 10);
                $(this).prev().val(currentVal + 1);
                $(this).prev().change();
            }
        });
        $('.left-cont input:not(.exempt)', w).click(function() {
            var l = $(this);
            if (w.hasClass('preset')) {
                calculateOfficeReset();
            } else {
                if (l.attr("type") == "radio") {
                    if (!l.hasClass('on') && !l.hasClass('off')) {
                        l.addClass('on');
                    }
                    if (l.hasClass('on')) {
                        l.removeClass('on');
                        l.addClass('off');
                        l.parents('li').addClass('active');
                        $(this).attr('checked', true);
                    } else if (l.hasClass('off')) {
                        l.removeClass('off');
                        l.addClass('on');
                        l.parents('li').removeClass('active');
                        $(this).attr('checked', false);
                    }
                }
                calculateOfficeConfig();
            }
        });
        $('.left-cont .amount input', w).change(function() {
            if (w.hasClass('preset')) {
                calculateOfficeReset();
                calculateOfficeConfig();
            } else {
                calculateOfficeConfig();
            }
        });
        $('.package-list li').click(function() {
            if (!$(this).hasClass('last')) {
                calculateOfficeReset();
                $(this).find('.hidden li').each(function() {
                    var node = $('#dom' + $(this).find('.serv-id').text(), w);
                    node.click();
                    if (!node.parents('.sub-sec').hasClass('open')) {
                        node.parents('.sub-sec').find('.top2 a').click();
                    }
                    if (!node.parents('.main-sec').hasClass('open')) {
                        node.parents('.main-sec').find('.top a').click();
                    }
                    $('.' + $(this).find('.serv-id').text(), w).find('.amount input').val($(this).find('.serv-num').text());
                    $('.' + $(this).find('.serv-id').text(), w).find('.amount input').change();
                });
                w.addClass('preset');
                $('.left-cont input:not(.am, .exempt)', w).each(function() {
                    if (!$(this).is(":checked")) {
                        $(this).parents('li').hide();
                    }
                });
                $('#change-offer', w).show();
            }
            $('.package-list li').removeClass("active");
            $(this).addClass('active');
            var index = $('.package-list li:not(.hidden li)').index(this);
            $('.presetContent').hide();
            $('.pre' + index + '').show();
            $('.pre' + index + ' .cms').show();
            calculateOfficeConfig();
        });
        $('#change-offer, .package-list li:last').click(function(e) {
            e.preventDefault();
            calculateOfficeReset();
            calculateOfficeConfig();
            $(window).scroll();
        });
        $('.package-list li:first').click();
        $('.package-list li:last').click();
//        $(window).scroll(summaryScroll);
        $(window).scroll(sideScroller);

        $(".lbx-frm form", w).submit(function(e) {
            e.preventDefault();
            $(".result-placeholder", w).html("");
            if ($(this).find("#dmn").val()) {
                var el = $(this).find("#dmn");
                var domain = el.val();
                $(this).find(".checkbox input:checked").each(function(){
                    var ending = $(this).val();
                    $.ajax({
                        url: "/domainCheck",
                        data: {
                            domain: domain + ending
                        },
                        success: function(data, status) {
                            if("success" == status){
                                if("AVAILABLE" == data){
                                    if($(".result-placeholder .avail", w).length == 0){
                                       $(".result-placeholder", w).append('<div class="avail"><h3>Proste domene:</h3></div>');
                                    } else {
                                        $(".result-placeholder .avail", w).append("<br/>");
                                    }
                                    $(".result-placeholder .avail", w).append("Domena " + domain + ending + " je prosta.");
                                } else if ("NOT AVAILABLE" == data){
                                    if($(".result-placeholder .unavail", w).length == 0){
                                        $(".result-placeholder", w).append('<div class="unavail"><h3>Zasedene domene:</h3></div>');
                                    } else {
	                                    $(".result-placeholder .unavail", w).append("<br />");
                                    }
                                    $(".result-placeholder .unavail", w).append("Domena " + domain + ending + " je zasedena.");
                                } else if ("RESERVED" == data){
                                    if($(".result-placeholder .unavail", w).length == 0){
                                        $(".result-placeholder", w).append('<div class="unavail"><h3>Zasedene domene:</h3></div>');
                                    } else {
	                                    $(".result-placeholder .unavail", w).append("<br />");
                                    }
                                    $(".result-placeholder .unavail", w).append("Domena " + domain + ending + " je rezervirana.");
                                }
                            } else {
                                $(".result-placeholder", w).append("Prišlo je do napake, prosimo poskusite znova."+status);
                            }
                        },
                        dataType: "html"
                    });
                });
            } else {
                alert("Prosimo izpolnite polje domena.");
            }
        });

        $('.cloud-summary .summary-item', w).click(function(){
            var cloudSummary = $('.cloud-summary', w);
            $('.summary-item', cloudSummary).removeClass('active');
            $(this).addClass('active');
            var indx = $(this).index();
            $('.main-sec', w).hide().addClass("stepped");
            if(indx == 0){
                $('#main-sec-0', w).show().removeClass("stepped");
            } else if(indx == 1){
                $('#main-sec-1', w).show().removeClass("stepped");
            } else if(indx == 2){
                $('#main-sec-2', w).show().removeClass("stepped");
                $('#main-sec-3', w).show().removeClass("stepped");
            } else if(indx == 3){
                $('#main-sec-4', w).show().removeClass("stepped");
            }
        });

        $('.cloud-summary .summary-item:first', w).click();

        $('.inactive', w).click(function(e){
            e.preventDefault();
        });
    }

});

var sideScroller = function() {
    var top = $(window).scrollTop();
    var w = $('#cloud-config');
    var sideWrap = $('.side-wrap', w);
    var leftCont = $('.left-cont', w);
    if(sideWrap.length > 0){
        if (leftCont.offset().top - top <= 20 && !sideWrap.hasClass('fixed')) {
            sideWrap.css('left', sideWrap.offset().left + 'px').addClass('fixed');
        } else if (leftCont.offset().top - top > 20 && sideWrap.hasClass('fixed')) {
            sideWrap.removeClass('fixed');
            sideWrap.css('left', null);
        }
    }
};

//var summaryScroll = function() {
//    var top = $(window).scrollTop();
//    var w = $('#cloud-config');
//    var cloudSummary = $('.cloud-summary', w);
//    if(cloudSummary.length > 0){
//        var tst = $('#main-sec-0', w).offset().top;
//        var tst2 = $('#main-sec-1', w).offset().top;
//        var tst4 = $('#main-sec-3', w).offset().top;
//        var tst5 = $('#main-sec-4', w).offset().top;
//        if (w.offset().top - top <= 80 && !cloudSummary.hasClass('fixed')) {
//            cloudSummary.css('left', w.offset().left + 'px').addClass('fixed');
//        } else if (w.offset().top - top > 80 && cloudSummary.hasClass('fixed')) {
//            cloudSummary.removeClass('fixed');
//            cloudSummary.css('left', null);
//        }
//        if(tst - 392 <= top){
//            if(!$('.p1', cloudSummary).hasClass('active')){
//                $('.summary-item', cloudSummary).removeClass('active');
//                $('.p1',cloudSummary).addClass('active');
//            }
//        }
//        if(tst2 - 392 <= top){
//            if(!$('.p2', cloudSummary).hasClass('active')){
//                $('.summary-item', cloudSummary).removeClass('active');
//                $('.p2', cloudSummary).addClass('active');
//            }
//        }
//        if(tst4 - 392 <= top){
//            if(!$('.p3', cloudSummary).hasClass('active')){
//                $('.summary-item', cloudSummary).removeClass('active');
//                $('.p3', cloudSummary).addClass('active');
//            }
//        }
//        if(tst5 - 392 <= top){
//            if(!$('.p4', cloudSummary).hasClass('active')){
//                $('.summary-item', cloudSummary).removeClass('active');
//                $('.p4', cloudSummary).addClass('active');
//            }
//        }
//    }
//};

var calculateOfficeReset = function() {
    var presetFlag = false;
    var w = $('#cloud-config');
    if (w.hasClass('preset')) {
        presetFlag = true;
        w.removeClass('preset');
    }
    $('.left-cont input:not(.exempt)', w).each(function() {
        if ($(this).is(":checked")) {
            $(this).attr('checked', null);
            var subSec = $(this).parents('.sub-sec');
            if (subSec.hasClass('open')) {
                subSec.addClass('closed');
                subSec.removeClass('open');
                subSec.find('.top2 a').text('prikaži');
                if ($(this).parents('li').hasClass('active')) {
                    $(this).parents('li').removeClass('active');
                }
            }
            var mainSec = $(this).parents('.main-sec');
            if (mainSec.hasClass('open')) {
                mainSec.addClass('closed');
                mainSec.removeClass('open');
                mainSec.find('.top a').text('prikaži');
            }
        }
        if ($(this).attr("disabled")) {
            $(this).attr("disabled", null);
            $(this).parent().removeClass("disabled");
        }
        if ($(this).parent().hasClass('active')) {
            $(this).parent().removeClass("active");
        }
        if ($(this).parent().hasClass('amount')) {
            $(this).val('1');
        }
    });
    if (presetFlag) {
        $('.package-list li').removeClass("active");
        $('.package-list li:last').addClass("active");
        $('.presetContent').hide();
        $('.pre4').show();
        $('.pre4 .cms').show();
        $('.applist li', w).show();
        $('#change-offer', w).hide();
//        calculateOfficeConfig();
        $('.main-sec:not(.initClose)', w).each(function() {
            if ($(this).hasClass('closed')) {
                $(this).addClass('open');
                $(this).removeClass('closed');
            }
            $(this).find('.top a').text('skrij');
        });
        $('.sub-sec', w).each(function() {
            if ($(this).hasClass('closed')) {
                $(this).addClass('open');
                $(this).removeClass('closed');
            }
            $(this).find('.top2 a').text('skrij');
        });
    }
    $('.cloud-summary .bottom a', w).addClass('.inactive').click(function(e){e.preventDefault();});
};

var calculateOfficeConfig = function() {
    var w = $('#cloud-config');
    var jsonData = {"services":{}, "mobile":{}, "hardware":{}, "internet":{}};
    var cookieName = "cookie-" + $("#guid", w).attr("class");
    var sum = 0;
    var total = 0;
    var dif = 0;
    var discount = 0;
    var fixedSum = 0;
    var fixedTotal = 0;
    var fixedDif = 0;
    var fixedDiscount = 0;
    var mobileSum = 0;
    var mobileTotal = 0;
    var mobileDif = 0;
    var mobileDiscount = 0;
    var mobileQuant = 0;
    var internetSum = 0;
    var internetTotal = 0;
    var internetQuant = 0;
    var internetDif = 0;
    var internetDiscount = 0;
    $('.left-cont input:not(.exempt)', w).each(function() {
        var serviceID = this.id.replace("dom", "");
        if ($(this).is(':checked') && (!($(this).parent().is(":hidden")) ||  (($(this).parent().is(":hidden")) && ($(this).parents().find('.main-sec').hasClass("stepped"))))) {
            if ($(this).hasClass("onetime")) {
                jsonData.hardware[serviceID] = $(this).parent().find(".am").val();
                var pricePerFixed = parseFloat($(this).parent().find('.r label .price').text().match(/([0-9\,]*)/)[0].replace(",", "."));
                var discountPerFixed = parseFloat($(this).parent().find('.r .discount').text().match(/([0-9\,]*)/)[0].replace(",", "."));
                if ($(this).parent().find('.r .amount input').length > 0) {
                    fixedSum = fixedSum + pricePerFixed * parseInt($(this).parent().find('.r .amount input').val(), 10);
                    fixedDiscount = fixedDiscount + discountPerFixed * parseInt($(this).parent().find('.r .amount input').val(), 10) + 0.0;
                } else {
                    fixedSum = fixedSum + pricePerFixed;
                    fixedDiscount = fixedDiscount + discountPerFixed + 0.0;
                }
            } else if ($(this).hasClass("mobile")) {
                jsonData.mobile[serviceID] = $(this).parent().find(".am").val();
                var pricePerMobile = parseFloat($(this).parent().find('.r label .price').text().match(/([0-9\,]*)/)[0].replace(",", "."));
                var discountPerMobile = parseFloat($(this).parent().find('.r .subvention').text().match(/([0-9\,]*)/)[0].replace(",", "."));
                if ($(this).parent().find('.r .amount input').length > 0) {
                    if ($(this).hasClass("package")) {
                        mobileQuant += parseInt($(this).parent().find('.r .amount input').val(), 10);
                    }
                    mobileSum = mobileSum + pricePerMobile * parseInt($(this).parent().find('.r .amount input').val(), 10);
                    mobileDiscount = mobileDiscount + discountPerMobile * parseInt($(this).parent().find('.r .amount input').val(), 10) + 0.0;
                } else {
                    mobileSum = mobileSum + pricePerMobile;
                    mobileDiscount = mobileDiscount + discountPerMobile + 0.0;
                }
            }else if ($(this).hasClass("internet")) {
                jsonData.internet[serviceID] = $(this).parent().find(".am").val();
                var pricePerInternet = parseFloat($(this).parent().find('.r label .price').text().match(/([0-9\,]*)/)[0].replace(",", "."));
                var discountPerInternet = parseFloat($(this).parent().find('.r .subvention').text().match(/([0-9\,]*)/)[0].replace(",", "."));
                if ($(this).parent().find('.r .amount input').length > 0) {
                    if ($(this).hasClass("package")) {
                        internetQuant += parseInt($(this).parent().find('.r .amount input').val(), 10);
                    }
                    internetSum = internetSum + pricePerInternet * parseInt($(this).parent().find('.r .amount input').val(), 10);
                    internetDiscount = internetDiscount + discountPerInternet * parseInt($(this).parent().find('.r .amount input').val(), 10) + 0.0;
                } else {
                    internetSum = internetSum + pricePerInternet;
                    internetDiscount = internetDiscount + discountPerInternet + 0.0;
                }
            } else {
                jsonData.services[serviceID] = $(this).parent().find(".am").val();
                var pricePer = parseFloat($(this).parent().find('.r label .price').text().match(/([0-9\,]*)/)[0].replace(",", "."));
                var discountPer = parseFloat($(this).parent().find('.r .discount').text().match(/([0-9\,]*)/)[0].replace(",", "."));
                if ($(this).parent().find('.r .amount input').length > 0) {
                    sum = sum + pricePer * parseInt($(this).parent().find('.r .amount input').val(), 10);
                    discount = discount + discountPer * parseInt($(this).parent().find('.r .amount input').val(), 10) + 0.0;
                } else {
                    sum = sum + pricePer;
                    discount = discount + discountPer + 0.0;
                }
            }
        } else if ($(this).attr("type") == "checkbox" || $(this).attr("type") == "radio") {
            if (jsonData.services[serviceID] != null) {
                delete jsonData.services[serviceID];
            }
            if (jsonData.hardware[serviceID] != null) {
                delete jsonData.hardware[serviceID];
            }
            if (jsonData.internet[serviceID] != null) {
                delete jsonData.internet[serviceID];
            }
            if (jsonData.mobile[serviceID] != null) {
                delete jsonData.mobile[serviceID];
            }
        }

    });
    if (w.hasClass('preset')) {
        if (discount <= 10.00) {
            total = sum * 0.95;
            dif = sum * 0.05;
        } else if (discount > 10.00 && discount <= 50.00) {
            total = sum * 0.90;
            dif = sum * 0.10;
        } else if (discount > 50.00 && discount <= 250.00) {
            total = sum * 0.85;
            dif = sum * 0.15;
        } else if (discount > 250.00) {
            total = sum * 0.80;
            dif = sum * 0.20;
        } else {
            total = sum;
        }
        $('.pres', w).show();
    } else {
        total = sum;
        $('.pres', w).hide();
    }
    if (mobileQuant > 2 && mobileQuant < 8) {
        mobileTotal = mobileSum * 0.97;
        mobileDif = mobileSum * 0.03;
    } else if (mobileQuant > 7 && mobileQuant < 16) {
        mobileTotal = mobileSum * 0.92;
        mobileDif = mobileSum * 0.08;
    } else if (mobileQuant > 15) {
        mobileTotal = mobileSum * 0.88;
        mobileDif = mobileSum * 0.12;
    } else {
        mobileTotal = mobileSum;
    }
    $('.monthly-conf', w).text((sum.toFixed(2) + "").replace(".", ","));
    $('.price-per-user', w).text($(".package-list .active strong").text().replace("€/m",""));
    $('.discount-conf', w).text((dif.toFixed(2) + "").replace(".", ","));
    $('.total-conf', w).text((total.toFixed(2) + "").replace(".", ","));
    $('.fixed-conf', w).text((fixedSum.toFixed(2) + "").replace(".", ","));
    $('.fixed-discount-conf', w).text((fixedDif.toFixed(2) + "").replace(".", ","));
    $('.fixed-total-conf', w).text((fixedSum.toFixed(2) + "").replace(".", ","));
    $('.internet-conf', w).text((internetSum.toFixed(2) + "").replace(".", ","));
    $('.internet-discount-conf', w).text((internetDif.toFixed(2) + "").replace(".", ","));
    $('.internet-total-conf', w).text((internetSum.toFixed(2) + "").replace(".", ","));
    $('.mobile-conf', w).text((mobileSum.toFixed(2) + "").replace(".", ","));
    $('.mobile-dif-conf', w).text((mobileDif.toFixed(2) + "").replace(".", ","));
    $('.mobile-discount-conf', w).text((mobileDiscount.toFixed(2) + "").replace(".", ","));
    $('.mobile-total-conf', w).text((mobileTotal.toFixed(2) + "").replace(".", ","));
    if(mobileTotal != 0 || fixedSum != 0 || total != 0){
        $('.cloud-summary .bottom a', w).removeClass('inactive').unbind('click');
    } else {
        $('.cloud-summary .bottom a', w).addClass('inactive').click(function(e){e.preventDefault();});
    }
    jsonData.discount = (dif.toFixed(2) + "").replace(".", ",");
    jsonData.points = (discount.toFixed(2) + "").replace(".", ",");
    jsonData.monthlyFee = (sum.toFixed(2) + "").replace(".", ",");
    jsonData.totalFee = (total.toFixed(2) + "").replace(".", ",");
    jsonData.packCode = $(".package-list li.active").index();
    $.cookie(cookieName, JSON.stringify(jsonData));
};
