$('document').ready(function(){

    var ds_view = parseInt(getCookie('ds_view'));
    if(ds_view == 17){
        document.getElementById('stylesheet').href = 'styles/disabled.css';
        $("a.disabled").css("display", "none");
        $("a.enabled").css("display", "inline");
    }

    $("div.menu li a").mouseover( function(){
        $(this).parents("li").addClass("hover");
        return false;
    });
	
    $("div.menu li a").mouseout( function(){
        $(this).parents("li").removeClass("hover");
        return false;
    });
	
    $('table.table tr:even').addClass('dif');
	
    $("div.faqQuestionHolder a.link").click(function(){
        ;
        e=$(this).parents("div.faqQuestionHolder").attr('class')
        if(e!=="faqQuestionHolder active"){
            $(this).parents("div.faqQuestionHolder").addClass('active');
            return false;
        }else{
            $(this).parents("div.faqQuestionHolder").removeClass('active');
            return false;
        }
    });
    $('div.faqQuestionHolder:first').addClass('active');

    $("a.disabled").click( function(){
        setCookie('ds_view', 17, 30);
        document.getElementById('stylesheet').href = 'styles/disabled.css';
        $("a.disabled").css("display", "none");
        $("a.enabled").css("display", "inline");
        return false;
    });
	
    $("a.enabled").click( function(){
        setCookie('ds_view', 3, 30);
        document.getElementById('stylesheet').href = 'styles/style.css';
        $("a.disabled").css("display", "inline");
        $("a.enabled").css("display", "none");
        return false;
    });

    /* filter selects */
    var filters = $('.filter_select');   
    var filters_length = filters.length;
    if(filters_length > 0){
        filters.change(function(){
            var id = $(this).attr('id').replace('filter_', '');
            $('#flsbm').val(id);

            if($(this).not('.filter_select_pr').length > 0){
                $('.filter_select_pr').val(-1);
            }
            $('#filterForm').submit();
        });
    }

    /* gallery */

    var gallery_mimg = $('#gallery_mimg');
    var gallery_photo = $('.gallery_photo');
    var gallery_photo_l = gallery_photo.length;
    var gallery_back = $('.gallery_back');
    var gallery_next = $('.gallery_next');
    var prev_next = false;

    gallery_back.hide();
    if(gallery_photo_l == 1){
        gallery_next.hide();
    }

    if(gallery_photo_l > 0){
        gallery_photo.click(function(e){
            e.preventDefault();
            prev_next = false;
            gallery_mimg.attr('src', $(this).attr('href'));
            gallery_photo.removeClass('active');
            $(this).addClass('active');
            active_element_key(gallery_photo, 'active');
        });

        gallery_back.click(function(e){
            e.preventDefault();
            prev_next = true;
            var active = active_element_key(gallery_photo, 'active');
            if(active > 0){
                var n = $(gallery_photo[active-1]);
                gallery_photo.removeClass('active');
                n.addClass('active');
                gallery_mimg.attr('src', n.attr('href'));
            }
            prev_next = false;
            if(active == 1){
                gallery_back.hide();
            }
            if(gallery_photo_l > 1){
                gallery_next.show();
            }
            
        });
        gallery_next.click(function(e){
            e.preventDefault();
            prev_next = true;
            var active = active_element_key(gallery_photo, 'active');
            if(active < gallery_photo_l-1){
                var n = $(gallery_photo[active+1]);
                gallery_photo.removeClass('active');
                n.addClass('active');
                gallery_mimg.attr('src', n.attr('href'));
            }
            prev_next = false;
            if(active == gallery_photo_l-2){
                gallery_next.hide();
            }
            if(gallery_photo_l > 1){
                gallery_back.show();
            }
        });

        function active_element_key(elements, clas_name){
            var i=0;
            var active = 0;
            elements.each(function(){
                if($(this).is('.'+clas_name)){
                    active = i;
                }
                i++;
            });
            if(prev_next == false){
                show_hide_next(active);
            }
            return parseInt(active);
        }

        function show_hide_next(active){
            if(gallery_next.not(':visible') && active != gallery_photo_l-1){
                gallery_next.show();
            }
            if(gallery_back.not(':visible') && active != 0){
                gallery_back.show();
            }
            if(gallery_next.is(':visible') && active == gallery_photo_l-1){
                gallery_next.hide();
            }
            if(gallery_back.is(':visible') && active < 1){
                gallery_back.hide();
            }
        }
    }



    /* new paging */
    if($('.pagging').length > 0){
        var nttp = parseInt($('#npgtt').val());
        $('.pagging a').click(function(e){
            e.preventDefault();
            var active = parseInt($('.pagging a.active').attr('href').replace('#', ''));
            
            if($(this).is('.back')){
                if(active > 1){
                    changeNewsPage(active-1);
                }
            }else if($(this).is('.next')){
                if(active < nttp){
                    changeNewsPage(active+1);
                }
            }else{
                var na = parseInt($(this).attr('href').replace('#', ''));
                changeNewsPage(na);
            }
            function changeNewsPage(page){
                var holder = $('#newListHolder');
                holder.html($('#newPage_'+page).html());
                $('.pagging a').removeClass('active');
                $('#npg_'+page).addClass('active');
            }
        });
    }


    function getCookie(c_name)
    {
        if (document.cookie.length>0)
        {
            c_start=document.cookie.indexOf(c_name + "=");
            if (c_start!=-1)
            {
                c_start=c_start + c_name.length+1;
                c_end=document.cookie.indexOf(";",c_start);
                if (c_end==-1) c_end=document.cookie.length;
                return unescape(document.cookie.substring(c_start,c_end));
            }
        }
        return "";
    }

    function setCookie(c_name,value,expiredays)
    {
        var exdate=new Date();
        exdate.setDate(exdate.getDate()+expiredays);
        document.cookie=c_name+ "=" +escape(value)+
        ((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
    }

	
});
