﻿function my_kwicks() {
    $('.kwicks').kwicks({
        duration: 300,
        max: 200,
        spacing: 0
    });
}

function initMenu() {
    $.getJSON('fa/Product/GetProductGroups',
        function (prdGroups) {
            for (i = 0; i < prdGroups.length; i++) {
                $('#menu-products').append('<li id="prd-grp-pname-' + i + '"></li>');
                $("#prd-grp-pname-" + i).append('<a id="prd-grp-pname-lnk-' + i + '" name="' + prdGroups[i].ProductGroupTypeId + '" href="#"></a>');
                $("#prd-grp-pname-lnk-" + i).text(prdGroups[i].GroupPName);
                $("#prd-grp-pname-lnk-" + i).click(function () {
                    $.get("fa/Product/ProductsByGroup", { groupTypeId: this.name }, function (data) {
                        $("#ContentArea").html(data);
                    });
                });
            }
        });
    $.getJSON('fa/Service/GetServices',
        function (services) {
            for (i = 0; i < services.length; i++) {
                $('#menu-services').append('<li id="svc-pname-' + i + '"></li>');
                $("#svc-pname-" + i).append('<a id="svc-pname-lnk-' + i + '" name="' + services[i].Id + '" href="#"></a>');
                $("#svc-pname-lnk-" + i).text(services[i].PName);
                $("#svc-pname-lnk-" + i).click(function () {
                    $.get("fa/Service/Details", { serviceId: this.name }, function (data) {
                        $("#ContentArea").html(data);
                    });
                });
            }
        });
    $.getJSON('=fa/NewsPiece/GetLatest',
        function (latestNews) {
            for (i = 0; i < latestNews.length; i++) {
                $('#newsBody').append('<div id="news-' + i + '"></div>');
                $('#news-' + i).html(latestNews[i].PNewsTitle + '<br />' + latestNews[i].PNewsText + '<br /><br />');
            }
        });
    $('#menu ul').hide();
    $('#menu ul:first').show();
    $('#menu li a').click(
    function () {
        var checkElement = $(this).next();
        if ((checkElement.is('ul')) && (checkElement.is(':visible'))) {
            return false;
        }
        if ((checkElement.is('ul')) && (!checkElement.is(':visible'))) {
            $('#menu ul:visible').slideUp('normal');
            checkElement.slideDown('normal');
            return false;
        }
    }
    );
}

$(document).ready(function () {

    my_kwicks(); initMenu();
    //      $.blockUI({ message: '<h1><img src="busy.gif" /> Just a moment...</h1>' });
    //      $(document).ajaxStart($.blockUI).ajaxStop($.unblockUI);
    initGallery();

    $('div.HeaderDiv2 marquee').mouseover(function () {
        $(this).trigger('stop');
    }).mouseout(function () {
        $(this).trigger('start');
    })

});

function onXhrComplete(res, status) {
    if (status === "success" || status === "notmodified") {
        $("#ContentArea").html(res.responseText);
    }
    $.unblockUI();
}

function onXhrBeforeSend(res) {
	$.blockUI({ message: '<h3><img src="images/busy.gif" /> ...در حال بارگذاری؛ لطفا صبر کنید</h3>' });
}

function aboutUs() {
    $.ajax({
        url: "fa/Home/AboutUs",
        method: 'GET',
        dataType: "html",
        complete: onXhrComplete,
        beforeSend: onXhrBeforeSend
    });
}

function Certificate() {
    $.ajax({
        url: "fa/Home/Certificate",
        method: 'GET',
        dataType: "html",
        complete: onXhrComplete,
        beforeSend: onXhrBeforeSend
    });
}

function Employement() {
    $.ajax({
        url: "fa/JobApplicant/Create",
        method: 'Get',
        dataType: "html",
        complete: onXhrComplete,
        beforeSend: onXhrBeforeSend
    });
}

function contactUs() {
    $.ajax({
        url: "fa/Home/ContactUs",
        method: 'GET',
        dataType: "html",
        complete: onXhrComplete,
        beforeSend: onXhrBeforeSend
    });
}

function aboutSite() {
    $.ajax({
        url: "fa/Home/AboutSite",
        method: 'GET',
        dataType: "html",
        complete: onXhrComplete,
        beforeSend: onXhrBeforeSend
    });
}

function customers() {
    $.ajax({
        url: "fa/Home/Customers",
        method: 'GET',
        dataType: "html",
        complete: onXhrComplete,
        beforeSend: onXhrBeforeSend
    });
}


function productDetail(id) {
    $.ajax({
        url: "fa/Product/Details",
        method: 'GET',
        dataType: "html",
        data: { productId: id },
        complete: onXhrComplete,
        beforeSend: onXhrBeforeSend
    });
}

function aa() {
    $.ajax({
        url: "fa/Questionnaire/Create",
        method: 'GET',
        dataType: "html",
        complete: onXhrComplete,
        beforeSend: onXhrBeforeSend
    });
}

function searchItems() {
    $.ajax({
        url: "fa/Home/SearchSite",
        method: 'GET',
        dataType: "html",
        data: { searchText: searchText.value },
        complete: onXhrComplete,
        beforeSend: onXhrBeforeSend
    });
}

function changeLang(newLang) {
    if (newLang == "EN")
        location.href = "/?lang=en";
    else
        location.href = "/?lang=fa";
}

function initGallery() {

    //<li><a href="#"><img src="ProductImages/1.jpg"/></a><span>تست تستی تستی</span></li>
    //    var URL = "fa/Product/GetImages";
    //    $('#gallery').append('<ul id="pikame"  class="jcarousel-skin-pika"></ul>');
    //    $.getJSON(URL,
    //        function (products) {
    //            for (i = 0; i < products.length; i++) {
    //                $('#pikame').append('<li id="prd-img-' + i + '"></li>');
    //                $("#prd-img-" + i).append('<img src="ProductImages/' + products[i].Id + '.jpg" />');        
    //                $("#prd-img-" + i).append('<a id="prd-img-lnk-' + i + '" name="' + products[i].Id + '" href="fa/Product/Details?productId=' + products[i].Id + '"></a>');
    //                $("#prd-img-lnk-" + i).append('<span id="prd-img-span-' + i + '>' + products[i].PName + ' </span>');                
    //            }
    //        });

    $("#pikame").PikaChoose();
    $("#pikame").jcarousel({ scroll: 1,
        initCallback: function (carousel) {
            $(carousel.list).find('img').click(function () {
                //console.log($(this).parents('.jcarousel-item').attr('jcarouselindex'));
                carousel.scroll(parseInt($(this).parents('.jcarousel-item').attr('jcarouselindex')));
            });
        }
    }
      );
}



function OrganizationChart() {
    $.ajax({
        url: "fa/Home/OrganizationChart",
        method: 'GET',
        dataType: "html",
        complete: onXhrComplete,
        beforeSend: onXhrBeforeSend
    });
}
