﻿/// <reference path="jquery-1.6.2.min.js" />

var current_row = 1,
    pics_in_a_row = 2,
    rows_per_page = 3,
    row_counter = 0,
    picture_width = 90,
    picture_hight = 85,
    breadcrumb_length = 3,
    redirected_page = "/Iskalnik_TSMedia.aspx",
    empty_string = '';

$(document).ready(function () {
    var $gallery = $('ul.gallery'),
        $pagination_controls = $("ul.controls"),
        $thumb_items = $gallery.children('li'),
        total_no_of_images = $thumb_items.length,
        row_name = "",
        pos_left = 0, pos_top = 0, max_pos_top = 0, page_counter = 0,
        no_of_needed_pages = Math.ceil(total_no_of_images / (rows_per_page * pics_in_a_row)),
        hide_breadcrumb = empty_string.concat('.breadcrumb:gt(', (breadcrumb_length + 2), ')');

    $('.active').next().addClass("activate_third_level");
    $('.item_up').not('.active').next().addClass("hide_third_level");
    $(hide_breadcrumb).hide();

    if ($('div.second_level_navigation').text().length > 0) {
        $('div.second_level_navigation').addClass("box_border");
    }

    //hover main menu event
    $(".main_menu_submenu").hover(
       function () {
           var menu_item_id = $(this).prev().find('span').attr('id');
           var menu_item_class = $(this).prev().find('span').attr('class');
           //check for existing classes
           if (menu_item_class.length == 0) {
               $(this).prev().find('span').addClass(empty_string.concat('hover_', menu_item_id));
           }
       },
       function () { // mouseout
           var menu_item_id = $(this).prev().find('span').attr('id');
           var menu_item_class = $(this).prev().find('span').attr('class');
           if (!$(this).prev().find('span').hasClass("activated_page")) {
               $(this).prev().find('span').removeClass(empty_string.concat('hover_', menu_item_id));
           }
       }
    );
    $('#magnifying_glass').click(function () {
        var queried_data = escape(encodeSpaces($("#search_najdi").val()));
        var redirect_to_arr = [];
        redirect_to_arr.push(redirected_page); redirect_to_arr.push('?q='); redirect_to_arr.push(queried_data); redirect_to_arr.push('&o=0');
        window.location.href = redirect_to_arr.join('');
    });
    if (!($('#similaritemList').children().hasClass("similiar_item"))) {
        $('#similaritemList').hide();
    }
    $('#search_najdi').keypress(function (event) {
        var keycode = (event.keyCode ? event.keyCode : event.which);
        if (keycode == '13') {
            $("#search_rounded_left").removeClass();
            $("#magnifying_glass").removeClass();

            var queried_data_enter_pressed = escape(encodeSpaces($("#search_najdi").val()));
            var redirect_to_entered_arr = [];
            redirect_to_entered_arr.push(redirected_page); redirect_to_entered_arr.push('?q='); redirect_to_entered_arr.push(queried_data_enter_pressed); redirect_to_entered_arr.push('&o=0');
            window.location.href = redirect_to_entered_arr.join('');
            return false;
        }
    });
    $('a.email').nospam({ replaceText: true });
    //onfocus textbox
    $("#search_najdi").focus(function () {
        $("#search_rounded_left").addClass("focus_rounded_left");
        $("#magnifying_glass").addClass("focus_magnifying_glass");
        $(this).animate({
            width: "210px"
        })
    });
    $("#search_najdi").focusout(function () {
        $("#search_rounded_left").removeClass();
        $("#magnifying_glass").removeClass();
        $(this).animate({
            width: "130px"
        })
    });
    //assing classes to thumbs
    $thumb_items.each(function (index) {
        //new row
        if (index % pics_in_a_row == 0) {
            if (row_counter % rows_per_page == 0) {
                pos_top = 0;
                page_counter++;
            }
            else {
                pos_top += picture_hight;
                if (pos_top > max_pos_top) { max_pos_top = pos_top; }
            }
            row_counter++;
            pos_left = 0;
        }
        pos_left += picture_width;
        row_name = empty_string.concat('gallery_row_', row_counter);
        var page_classname = empty_string.concat('page_', page_counter);
        $(this).addClass(row_name.concat(' ', page_classname));

        //check if first page
        if (index < (rows_per_page * pics_in_a_row)) {
            assign_css_to_element(this, 'absolute', pos_left - picture_width, pos_top, 'list-item');
        }
        else {
            assign_css_to_element(this, 'absolute', pos_left - picture_width, pos_top, 'none');
        }
        //first child on page
        change_info_about_picture(1)
    });

    var pagination_html_arr = [];
    //pagination 
    for (var _page = 1; _page < no_of_needed_pages + 1; _page++) {
        if (_page == 1) {
            pagination_html_arr.push('<li class="page_number active_page">');
            pagination_html_arr.push(_page);
            pagination_html_arr.push('</li>');
        }
        else {
            pagination_html_arr.push('<li class="page_number">');
            pagination_html_arr.push(_page);
            pagination_html_arr.push('</li>');
        }
    }
    pagination_html = pagination_html_arr.join('');

    $(".page_number").live('click', function () {
        $thumb_items.css('display', 'none');
        $("ul.controls").children(".page_number").removeClass("active_page");
        $(this).addClass("active_page");
        $('ul.gallery li').css('display', 'none');
        $('.page_' + $(this).text()).css('display', 'list-item');
        var nth_child = ((parseInt($(this).text()) - 1) * pics_in_a_row * rows_per_page) + 1;
        change_info_about_picture(nth_child);
    });
    $pagination_controls.html(pagination_html);

    $("#next_page").live('click', function () {
        var page_id = $("ul.controls li.active_page").text(),
             next_page_idx = (parseInt(page_id) + 1);

        if (next_page_idx < no_of_needed_pages + 1) {
            var next_page = "page_".concat(next_page_idx.toString()),
                next_page_id = "page_".concat(next_page_idx);

            $(".controls").children(".page_number").removeClass("active_page");
            $(empty_string.concat("ul.controls li:eq(", (parseInt(next_page_idx) - 1), ")")).addClass("active_page");
            $('ul.gallery li').css('display', 'none');
            $(empty_string.concat('.', next_page)).css('display', 'list-item');
            var nth_child = ((parseInt(next_page_idx) - 1) * pics_in_a_row * rows_per_page) + 1;
            change_info_about_picture(nth_child);
        }
    });
    $("#prev_page").click(function () {
        var page_id = $("ul.controls li.active_page").text(),
            prev_page_idx = (parseInt(page_id) - 1);
        if (prev_page_idx > 0) {
            var prev_page = empty_string.concat("page_", prev_page_idx.toString());
            $(".controls").children(".page_number").removeClass("active_page");
            $(empty_string.concat("ul.controls li:eq(", (parseInt(prev_page_idx) - 1), ")")).addClass("active_page");
            $('ul.gallery li').css('display', 'none');
            $(empty_string.concat('.', prev_page)).css('display', 'list-item');
            var nth_child = ((parseInt(prev_page_idx) - 1) * pics_in_a_row * rows_per_page) + 1;
            change_info_about_picture(nth_child);
        }
    });

    $("ul.gallery li img").click(function () {
        change_info_about_picture(parseInt($(this).parent().index()) + 1);
    });
});
function encodeSpaces(str) {
    return str.replace(" ", "+");
}
function assign_css_to_element(element, _position, _left, _top, _display) {
    $(element).css({
        position: _position,
        left: empty_string.concat(_left.toString(), 'px'),
        top: empty_string.concat(_top.toString(), 'px'),
        display: _display
    });
}
function change_info_about_picture(child) {
    var nth_child_selector = empty_string.concat("ul.gallery li:nth-child(", child.toString(), ")");
    var result = $(nth_child_selector).children(".div_hidden").children(".about_text").text();
    $("#preview_text").html(result);
    //    $("#small_pic_href").attr("href", $(nth_child_selector).children("img").attr("src"));
    //    $("#med_pic_href").attr("href", $(nth_child_selector).children(".div_hidden").children(".img_med").attr("src"));
    $("#big_pic_href").attr("href", $(nth_child_selector).children(".div_hidden").children(".img_big").attr("src"));
    $("#preview_pic").css("background", empty_string.concat("url(", $(nth_child_selector).children(".div_hidden").children(".img_med").attr("src"), ")"));
    $("#vector_pic_href").attr("href", $(nth_child_selector).children(".div_hidden").children(".vector_pdf").text());
}
jQuery.fn.nospam = function (settings) {
    settings = jQuery.extend({
        replaceText: false, 	// optional, accepts true or false
        filterLevel: 'normal' 	// optional, accepts 'low' or 'normal'
    }, settings);

    return this.each(function () {
        e = null;
        if (settings.filterLevel == 'low') { // Can be a switch() if more levels added
            if ($(this).is('a[rel]')) {
                e = $(this).attr('rel').replace('//', '@').replace(/\//g, '.');
            } else {
                e = $(this).text().replace('//', '@').replace(/\//g, '.');
            }
        } else { // 'normal'
            if ($(this).is('a[rel]')) {
                e = $(this).attr('rel').split('').reverse().join('').replace('//', '@').replace(/\//g, '.');
            } else {
                e = $(this).text().split('').reverse().join('').replace('//', '@').replace(/\//g, '.');
            }
        }
        if (e) {
            if ($(this).is('a[rel]')) {
                $(this).attr('href', 'mailto:' + e);
                if (settings.replaceText) {
                    $(this).text(e);
                }
            } else {
                $(this).text(e);
            }
        }
    });
};
