﻿var delay_time = 500; //miliseconds
var langId = 1;

var pageSizePhoto = 15;       //number of photos per page
var pagePhoto = 1;            //index of the current page
var maxPagesPhoto = 10;       //maximum displayed pages on the pager
var countAllPhoto = 0;        //total number of photos
var lowerLimitPhoto = 1;      //the smallest index on the pager
var upperLimitPhoto = 1;      //the biggest index on the pager
var upperPhoto = 0;           //temporar variable
var updLimPhoto = false;

//on load
$(document).ready(function () {
    if (window.location.href.indexOf("/admin") == -1) {
        //if ($("input[id_rel='btn_acceptPp']") != null)
        //    $("input[id_rel='btn_acceptPp']").attr("disabled", true);

        langId = $("#hf_langId").val();
        loadPhotosPage(true, langId);
    }
});

// PHOTO GALLERY SCRIPTS START

function pageClickPhoto(index, langId) {
    pagePhoto = index;
    loadPhotosPage(false, langId);
}

function previousPagePhoto(langId) {
    if (pagePhoto > 1) {
        pagePhoto--;
        loadPhotosPage(true, langId);
    }
}

function nextPagePhoto(langId) {
    var pagesCount = Math.floor(countAllPhoto / pageSizePhoto);
    
    if (pagesCount > 0) {
        if ((countAllPhoto - Math.floor(countAllPhoto / pagesCount) * pagesCount != 0) || (pagesCount == 1 && countAllPhoto > pageSizePhoto))
            pagesCount++;
    }
    else
        pagesCount = 1;

    if (pagePhoto < pagesCount) {
        pagePhoto++;
        loadPhotosPage(true, langId);
    }
}

function setLightBox() {
    var settings = { containerResizeSpeed: 350
    };
    $('#gallery a').lightBox(settings);
}

function loadPhotosPage(updateLimits, langId) {
    $("#h1_galleryTitle").html($("#ddl_gallery option:selected").text());
    loadPhotosCount(updateLimits, langId);
    setTimeout("setLightBox()", delay_time);
}

function loadPhotos(index, langId) {
    $.ajax({
        type: "POST",
        dataType: "text",
        contentType: "application/json",
        url: "lib/ws_services.asmx/LoadPhotosHtml",
        data: "{ index: " + index + ", langId: " + langId + "}",
        success: function (str_html) {
            $("#div_photos").html(str_html);
        }
    });
}

function loadPhotosCount(updateLimits, langId) {
    $.ajax({
        type: "POST",
        dataType: "json",
        contentType: "application/json",
        url: "lib/ws_services.asmx/GetPhotosCount",
        data: "{ langId: " + langId + "}",
        success: function (data1) {
            $("#hf_countTotalPhotos").val(data1.d);
            countAllPhoto = data1.d;

            pagesCount = Math.floor(countAllPhoto / pageSizePhoto);
            if (pagesCount == 0)
                pagesCount = 1;

            if ((countAllPhoto - Math.floor(countAllPhoto / pagesCount) * pagesCount != 0) || (pagesCount == 1 && countAllPhoto > pageSizePhoto))
                pagesCount++;

            if (countAllPhoto > 0 && pagesCount > 1)
                $("#div_photos_pager").show();
            else
                $("#div_photos_pager").hide();

            var temp_html = "";
            //updLimPhoto = updateLimits;

            //if (updLimPhoto) 
            {
                i = 0;

                while (true) {
                    if ((pagePhoto >= ((maxPagesPhoto * i) + 1)) && (pagePhoto <= (maxPagesPhoto * (i + 1))))
                        break;
                    else
                        i++;
                }

                lowerLimitPhoto = maxPagesPhoto * i + 1;
                upperLimitPhoto = maxPagesPhoto * (i + 1);
                upperLimitPhoto = Math.min(upperLimitPhoto, pagesCount);

                $("#div_photos_pager").html("<ul class='menu8'><li class='menutli8'><a href='#' class='mainNavlink8' onclick='previousPagePhoto(" + langId + ");'><-</a></li>");
                $("#gallery").html("");

                for (var i = lowerLimitPhoto; i <= upperLimitPhoto; i++) {
                    var str_css = "pagerGallLink";

                    if (pagePhoto != i)
                        str_css = "mainNavlink8";
                    else
                        str_css = "photoPgSel";

                    temp_html = $("#div_photos_pager").html();
                    $("#div_photos_pager").html(temp_html + "<li class='menutli8'><a href='#' class='" + str_css + "' onclick='pageClickPhoto(" + i + ", " + langId + ");'>" + i + "</a></li>");
                }

                var photos_html = loadPhotoGalleryPhotos(pagePhoto, langId);
                $("#gallery").html(photos_html);

                temp_html = $("#div_photos_pager").html();
                $("#div_photos_pager").html(temp_html + "<li class='menutli8'><a href='#' class='mainNavlink8' onclick='nextPagePhoto(" + langId + ");'>-></a></li></ul>");
            }
        }
    });
}

function loadPhotoGalleryPhotos(index, langId) {
    $.ajax({
        type: "POST",
        dataType: "text",
        contentType: "application/json",
        url: "lib/ws_services.asmx/LoadPhotosHtml",
        data: "{ index: " + index + ", langId: " + langId + "}",
        success: function (str_html) {
            $("#gallery").html(str_html);
        }
    });
}

// PHOTO GALLERY SCRIPTS END 

function querySt(ji) {
    hu = window.location.search.substring(1);
    gy = hu.split("&");
    for (i = 0; i < gy.length; i++) {
        ft = gy[i].split("=");
        if (ft[0] == ji) {
            return ft[1];
        }
    }
}

// JScript1 File
//Phone numbers
function IntegerNumberPhone(e, text_box) {
    var tb_value = new String(text_box.value);

    var iKeyCode = 0;
    if (window.event)
        iKeyCode = window.event.keyCode
    else
        if (e)
            iKeyCode = e.which;

    if (tb_value.charAt(0) == ' ') {
        tb_value = tb_value.substring(1, tb_value.length);
        text_box.value = tb_value;
    }

    if (iKeyCode != 8 && iKeyCode != 16 && iKeyCode != 17 && iKeyCode != 18 &&
            iKeyCode != 37 && iKeyCode != 39 && iKeyCode != 36 && iKeyCode != 9) {
        if (iKeyCode >= 96 && iKeyCode <= 105) {
        }
        else
            if (iKeyCode >= 48 && iKeyCode <= 57) {
            }
            else {
                var temp_str = "";
                var last_char;
                var i;
                for (i = 0; i < tb_value.length; i++) {
                    last_char = tb_value.charAt(i);

                    if (last_char == '0' || last_char == '1' ||
                            last_char == '2' || last_char == '3' ||
                            last_char == '4' || last_char == '5' ||
                            last_char == '6' || last_char == '7' ||
                            last_char == '8' || last_char == '9' || last_char == ' ') {
                        temp_str += last_char;
                    }
                }

                text_box.value = temp_str;
                tb_value = new String(text_box.value);
            }
    }
}

function IntegerNumber1(e, text_box) {
    var tb_value = new String(text_box.value);

    var iKeyCode = 0;
    if (window.event)
        iKeyCode = window.event.keyCode
    else
        if (e)
            iKeyCode = e.which;

    if (tb_value.charAt(0) == '0') {
        tb_value = tb_value.substring(1, tb_value.length);
        text_box.value = tb_value;
    }

    if (iKeyCode != 8 && iKeyCode != 16 && iKeyCode != 17 && iKeyCode != 18 &&
        iKeyCode != 37 && iKeyCode != 39 && iKeyCode != 36 && iKeyCode != 9) {
        if (iKeyCode >= 96 && iKeyCode <= 105) {
        }
        else
            if (iKeyCode >= 48 && iKeyCode <= 57) {
            }
            else {
                var temp_str = "";
                var last_char;
                var i;
                for (i = 0; i < tb_value.length; i++) {
                    last_char = tb_value.charAt(i);

                    if (last_char == '0' || last_char == '1' ||
                            last_char == '2' || last_char == '3' ||
                            last_char == '4' || last_char == '5' ||
                            last_char == '6' || last_char == '7' ||
                            last_char == '8' || last_char == '9') {
                        temp_str += last_char;
                    }
                }

                text_box.value = temp_str;
                tb_value = new String(text_box.value);
            }
    }
 }
