﻿var doc = document;




///     begin copy right @ https://bootsnipp.com/snippets/zDQkr ///
$(document).ready(function () {



    var itemsMainDiv = ('.MultiCarousel');
    var itemsDiv = ('.MultiCarousel-inner');
    var itemWidth = "";

    $('.leftLst, .rightLst').click(function () {
        var condition = $(this).hasClass("leftLst");
        if (condition)
            click(1, this);
        else
            click(0, this)
    });

    function RightLstClick() {
        click(1, $('.rightLst'));
    }

    ResCarouselSize();




    $(window).resize(function () {
        ResCarouselSize();
    });

    //this function define the size of the items
    function ResCarouselSize() {
        var incno = 0;
        var dataItems = ("data-items");
        var itemClass = ('.item');
        var id = 0;
        var btnParentSb = '';
        var itemsSplit = '';
        var sampwidth = $(itemsMainDiv).width();
        var bodyWidth = $('body').width();
        $(itemsDiv).each(function () {
            id = id + 1;
            var itemNumbers = $(this).find(itemClass).length;
            btnParentSb = $(this).parent().attr(dataItems);
            itemsSplit = btnParentSb.split(',');
            $(this).parent().attr("id", "MultiCarousel" + id);


            if (bodyWidth >= 1200) {
                incno = itemsSplit[3];
                itemWidth = sampwidth / incno;
            }
            else if (bodyWidth >= 992) {
                incno = itemsSplit[2];
                itemWidth = sampwidth / incno;
            }
            else if (bodyWidth >= 768) {
                incno = itemsSplit[1];
                itemWidth = sampwidth / incno;
            }
            else {
                incno = itemsSplit[0];
                itemWidth = sampwidth / incno;
            }

            itemWidth = 250;

            $(this).css({ 'transform': 'translateX(0px)', 'width': itemWidth * itemNumbers });
            $(this).find(itemClass).each(function () {
                $(this).outerWidth(itemWidth);
            });

            $(".rightLst").addClass("over");
            $(".leftLst").removeClass("over");



        });
    }


    //this function used to move the items
    var maxItems = 9;
    var currentIndex = 1;
    function ResCarousel(e, el, s) {
        var leftBtn = ('.leftLst');
        var rightBtn = ('.rightLst');
        var translateXval = '';
        var divStyle = $(el + ' ' + itemsDiv).css('transform');
        var values = divStyle.match(/-?[\d\.]+/g);
        var xds = Math.abs(values[4]);


        if (e == 0) {
            translateXval = parseInt(xds) - parseInt(itemWidth * s);
            $(el + ' ' + leftBtn).removeClass("over");

            if (translateXval <= itemWidth / 2) {
                translateXval = 0;
                $(el + ' ' + rightBtn).addClass("over");
            }
            currentIndex--;
        }
        else if (e == 1) {
            var itemsCondition = $(el).find(itemsDiv).width() - $(el).width();
            translateXval = parseInt(xds) + parseInt(itemWidth * s);
            $(el + ' ' + rightBtn).removeClass("over");

            if (translateXval >= itemsCondition - itemWidth / 2) {
                translateXval = itemsCondition;
                $(el + ' ' + leftBtn).addClass("over");
            }

            currentIndex++;
        }


        if (currentIndex > maxItems) {
            translateXval = 0;
            currentIndex = 1;
            $(el + ' ' + leftBtn).removeClass("over");
        }

        var lb = $(el + ' ' + leftBtn);
        classLst = lb.attr("class").split(/\s+/);
        if (classLst.includes("over") && (currentIndex != 1)) {
            currentIndex = maxItems;
            $(el + ' ' + leftBtn).removeClass("over");
        }

        $(el + ' ' + itemsDiv).css('transform', 'translateX(' + translateXval + 'px)');
    }

    //It is used to get some elements from btn
    function click(ell, ee) {
        var Parent = "#" + $(ee).parent().attr("id");
        var slide = $(Parent).attr("data-slide");

        ResCarousel(ell, Parent, slide);
    }

    //setInterval(RightLstClick,3000);

});

///     end copy right @ https://bootsnipp.com/snippets/zDQkr ///





var i=1;
function HideMenu(){
    var menu = doc.getElementById("nav-bar");

    $(menu).animate({width:'toggle'}, 500);

    if(i%2==1)
        doc.getElementById("main-body").style.marginRight = "0px";
    else
        doc.getElementById("main-body").style.marginRight = "75px";

    i++;
}


function GetMajors()
{
    var selCollege = doc.getElementById("selCollege");
    var collegeID = selCollege[selCollege.selectedIndex].value;

    path = "/College/GetCollegeMajorsAjax/" + collegeID;


    $.ajax({
        type: 'GET',
        url: path,
        headers: {
            'X-Requested-With': 'XMLHttpRequest'
        },
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        success: function (result) {

            // holding majors' information
            majors = result;

            
            var selMajor = doc.getElementById("selMajor");
            for (var i = selMajor.options.length - 1 ; i >= 0 ; i--) {
                selMajor.remove(i);
            }

            for (var i = 0; i < majors.length; i++) {

                var opt = document.createElement("option");
                opt.value = majors[i].MajorID;
                opt.innerHTML = majors[i].MajorName;

                selMajor.appendChild(opt);
            }
        },
        error: function (result) {
            alert("خطا در بارگذاری داده ها");
            console.log(result);
        }
    });


}

var menucounter = 1;
function ShowHideMenu()
{
    var index = menucounter % 2;
    var mainMenu = doc.getElementById("mainMenu");
    if(index)
    {
        mainMenu.style.display = "block";
        mainMenu.style.height = "300px";
        mainMenu.style.width = "100%";
        mainMenu.style.backgroundColor = "#131313";
        mainMenu.style.clear = "both";
        mainMenu.style.borderBottom = "2px solid #000";
        mainMenu.style.position = "absolute";
        mainMenu.style.top = "120px";
        mainMenu.style.zIndex = "2";
        mainMenu.style.textAlign = "center";
    }
    else
    {
        mainMenu.style.display = "none";
    }

    menucounter++;

}


window.addEventListener("resize", function () {
    
    var mainMenu = doc.getElementById("mainMenu");

    if (document.body.offsetWidth > 1033) {
        mainMenu.style.display = "inline-block";
        mainMenu.style.height = "75px";
        mainMenu.style.position = "static";
        mainMenu.style.zIndex = "0";
        mainMenu.style.width = "75%";
        menucounter = 0;
    }
    else
    {
        mainMenu.style.display = "none";
        menucounter = 1;
    }
});


function ClearSearch()
{
    doc.getElementById("txtSearch").value = "";
}


function OpenPage(evt, pageName) {
    var i, tabcontent, tablinks;

    tabcontent = document.getElementsByClassName("activeTabContent");
    for (i = 0; i < tabcontent.length; i++) {
        tabcontent[i].style.display = "none";
    }

    tabcontent = document.getElementsByClassName("tabcontent");
    for (i = 0; i < tabcontent.length; i++) {
        tabcontent[i].style.display = "none";
    }
    tablinks = document.getElementsByClassName("tablinks");
    for (i = 0; i < tablinks.length; i++) {
        tablinks[i].className = tablinks[i].className.replace(" active", "");
    }
    document.getElementById(pageName).style.display = "block";
    evt.currentTarget.className += " active";
}

function ValidateUserProfileForm(e) {
    txtNewPassword = doc.getElementById("txtNewPassword");
    txtRePassword = doc.getElementById("txtRePassword");


    if (txtNewPassword.value != txtRePassword.value) {
        alert("کلمه عبور جدید و تکرار آن باید برابر باشند");
        txtNewPassword.style.border = "1px solid #f00";
        txtRePassword.style.border = "1px solid #f00";
        e.preventDefault();
        return;
    }
    else {
        txtNewPassword.style.border = "1px solid #ccc";
        txtRePassword.style.border = "1px solid #ccc";
    }
}


function SetSearchCollege(collegeID)
{
    doc.getElementById("txtCollegeID").value = collegeID;
}

function LikeProduct(id)
{

    path = "/AjaxHandler/LikeProduct/" + id;

    $.ajax({
        type: 'GET',
        url: path,
        headers: {
            'X-Requested-With': 'XMLHttpRequest'
        },
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        success: function (result) {

            if (result == -1) {
                alert("لطفا ابتدا وارد حساب کاربری خود شوید")
                return;
            }

            if (result > 0) {
                var target = doc.getElementById("divProductLike" + id);
                target.innerHTML = "مورد پسند: " + result + " نفر";
            }
        },
        error: function (result) {
            alert("خطا در بارگذاری داده ها");
        }
    });
}

function SubmitForm(formID)
{
    var frm = doc.getElementById(formID);
    doc.getElementById(formID).submit();
}

function openTab(evt, tab) {
    // Declare all variables
    var i, tabcontent, tablinks;

    // Get all elements with class="tabcontent" and hide them
    tabcontent = document.getElementsByClassName("tabcontent");
    for (i = 0; i < tabcontent.length; i++) {
        tabcontent[i].style.display = "none";
    }

    // Get all elements with class="tablinks" and remove the class "active"
    tablinks = document.getElementsByClassName("tablinks");
    for (i = 0; i < tablinks.length; i++) {
        tablinks[i].className = tablinks[i].className.replace(" active", "");
    }

    // Show the current tab, and add an "active" class to the button that opened the tab
    document.getElementById(tab).style.display = "block";
    evt.currentTarget.className += " active";
}

