/*
 * Slidegallery.
 * By Kolobok
 * Copyright (c) 2010 Kolobok
*/

jQuery.fn.slidegallery = function(options){
    var sgIe, sgTw, sgIw, sgMn, sgCd, sgTWidth;
    var options = jQuery.extend({
        sgResize: false,
        sgImgFields: 22,
        sgThumbFields: 7,
        sgThumbWidth: 120,
        sgThumbHeight: 9,
        sgImgStartId: false
    },options);

    sgIe=document.all?true:false;

    return this.each(function() {
        sgMn=this;
        sgTw=jQuery(this).find('.sgThumbwrapper');

        //������ ����� ��������� � ������ ������� (:last �� ��������)
        sgTWidth=jQuery(sgTw).find('.sgThumbs li').length*(options.sgThumbWidth+options.sgThumbFields);

        //���������� ������ ��� ����������
        $(sgMn).find(".sgImage .sgAddinfo").width($(sgMn).find(".sgImage").width());

        if(sgIe){
            $(sgMn).find(".sgThumbwrapper").css('height','102px');
            $(sgMn).find(".sgImage .sgImgDescr").width(parseInt($(sgMn).find(".sgImage").width())-14);
        }else{
            $(sgMn).find(".sgImage .sgImgDescr").width($(sgMn).find(".sgImage").width());
        }

        //������� ��������
        var img_id;
        if(options.sgImgStartId){
            img_id=options.sgImgStartId;
        }else{
            var elem=$(sgMn).find(".sgThumbs li:first");
            img_id=$(elem).attr('value');
        }
        if(img_id){
            sgSetImage(img_id);
        }

        //������������ ����
        jQuery(this).find('.sgThumbs li img').mouseover(function(){
            jQuery(this).animate({ opacity: 1 }, "normal");
        });
        jQuery(this).find('.sgThumbs li img').mouseout(function(){
            jQuery(this).animate({ opacity: 0.5 }, "normal");
        });

        //��������� � ���������
        jQuery(this).find('.sgThumbs li img').mousedown(function(){
            jQuery(sgMn).find('.sgThumbs li img').removeClass('sgThumbSelected');;
            jQuery(this).addClass('sgThumbSelected');
            var parent=jQuery(this).parent().get(0);
            var image_id=jQuery(parent).attr('value');
            sgSetImage(image_id);
        });

        //��������� � ����� �������
        jQuery(this).find('.sgImage .sgPrevimg').mousedown(function(){
            var image_id=jQuery(sgMn).find('.sgImage .sgImageId').text();
            image_id=parseInt(image_id);
            var elem=$(sgMn).find("li[name*='img_"+image_id+"']").prev();
            var next_image=$(elem).attr('value');
            if(next_image && next_image!=image_id){
                sgSetImage(next_image);
            }
            //���������� ������� ������� � ���� ����� ��������������
            var cl=(jQuery(this).attr('class'));
            cl=cl.replace(' sgNavimg', '');
            var isScrolling=true;
            var curImgId=parseInt(jQuery(sgMn).find('.sgImage .sgImageId').text());
            var firstImgId=parseInt(jQuery(sgTw).find('.sgThumbs li:first').attr('value'));
            var lastImgId=parseInt(jQuery(sgTw).find('.sgThumbs li:last').attr('value'));

            if(cl=='sgPrevimg' && curImgId==firstImgId){
                isScrolling=false;
            }

            if(cl=='sgNextimg' && curImgId==lastImgId){
                isScrolling=false;
            }

            if(!isScrolling){
                jQuery(this).animate({ opacity: 0.5 }, "normal");
            }
        });

        jQuery(this).find('.sgImage .sgNextimg').mousedown(function(){
            var image_id=jQuery(sgMn).find('.sgImage .sgImageId').text();
            image_id=parseInt(image_id);
            var elem=$(sgMn).find("li[name*='img_"+image_id+"']").next();
            var next_image=$(elem).attr('value');
            if(next_image && next_image!=image_id){
                sgSetImage(next_image);
            }

            //���������� ������� ������� � ���� ����� ��������������
            var cl=(jQuery(this).attr('class'));
            cl=cl.replace(' sgNavimg', '');
            var isScrolling=true;
            var curImgId=parseInt(jQuery(sgMn).find('.sgImage .sgImageId').text());
            var firstImgId=parseInt(jQuery(sgTw).find('.sgThumbs li:first').attr('value'));
            var lastImgId=parseInt(jQuery(sgTw).find('.sgThumbs li:last').attr('value'));

            if(cl=='sgPrevimg' && curImgId==firstImgId){
                isScrolling=false;
            }

            if(cl=='sgNextimg' && curImgId==lastImgId){
                isScrolling=false;
            }

            if(!isScrolling){
                jQuery(this).animate({ opacity: 0.5 }, "normal");
            }
        });

        //������������ ��������� ���� ������ ��� ��������� �� ������ ����
        jQuery(this).find('.sgImage .sgNavimg').mouseover(function(){
             var cl=(jQuery(this).attr('class'));
             cl=cl.replace(' sgNavimg', '');
             var isScrolling=true;
             var curImgId=parseInt(jQuery(sgMn).find('.sgImage .sgImageId').text());
             var firstImgId=parseInt(jQuery(sgTw).find('.sgThumbs li:first').attr('value'));
             var lastImgId=parseInt(jQuery(sgTw).find('.sgThumbs li:last').attr('value'));

             if(cl=='sgPrevimg' && curImgId==firstImgId){
                isScrolling=false;
             }

             if(cl=='sgNextimg' && curImgId==lastImgId){
                isScrolling=false;
             }

             if(isScrolling){
                jQuery(this).animate({ opacity: 1 }, "normal");
             }

        });

        jQuery(this).find('.sgImage .sgNavimg').mouseout(function(){
             jQuery(this).animate({ opacity: 0.5 }, "normal");
        });

        //��������� ������� ��������� ����� ���������
        jQuery(sgTw).mouseout(function(e){sgCancelScroll();});
        jQuery(sgTw).mouseover(function(e){sgScrollThumbnails(e);});
        jQuery(this).find('.sgThumbs li').mouseover(function(e){sgScrollThumbnails(e);});
        jQuery(this).find('.sgThumbs').mouseover(function(e){sgScrollThumbnails(e);});
        jQuery(this).find('.sgThumbarea').mouseover(function(e){sgScrollThumbnails(e);});
    });

    function sgSetImage(image_id){
        var elem=$(sgMn).find("li[name*='img_"+image_id+"']");
        var title=$(elem).find('h3').text();
        var src=$(elem).find('em[class*=\'image\']').html();
        var description=$(elem).find('em[class*=\'description\']').text();
        //������������� ���������� ������
        $(sgMn).find('.sgImagearea .sgAddinfo').hide();
        //������� ������ ��������
        $(sgMn).find('.sgImagearea .sgImage .sgImageN').remove();

        //������ ������ ���� ����� ������� (� ���� ������ ���� ��������� �������� ������ ��������)
        if(options.sgResize){
            var height=parseInt($(elem).find('em.height').text())+options.sgImgFields;
            $(sgMn).find('.sgImage').height(height);
            $(sgMn).find('.sgImage .sgNavimg').hide();
            $(sgMn).find('.sgImage .sgNavimg').height(height-10);
            $(sgMn).find('.sgImage .sgNavimg').show();
        };
        //�������� ����� ��������
        $(sgMn).find('.sgImagearea .sgImage').append('<img src="" class="sgImageN"/>');
        $(sgMn).find('.sgImagearea .sgImage .sgImageN').animate({ opacity: 0 }, 0);
        $(sgMn).find('.sgImagearea .sgImage .sgImageN').attr('src', src);
        $(sgMn).find('.sgImagearea .sgImage .sgImageN').attr('title', title);
        $(sgMn).find('.sgImagearea .sgImage .sgImageN').animate({ opacity: 1 }, "normal");
        $(sgMn).find('.sgImagearea .sgImageId').text(image_id);
        //��������� ������� �������� ��������
        if(title){
            jQuery(sgMn).find(".sgImage .sgImgDescr").text(title);

            jQuery(sgMn).find(".sgImage .sgImageN").mouseover(function(){
                jQuery(sgMn).find(".sgImage .sgImgDescr").animate({ opacity: 0.5 }, "normal");
            });

            jQuery(sgMn).find(".sgImage .sgImageN").mouseout(function(){
                jQuery(sgMn).find(".sgImage .sgImgDescr").animate({ opacity: 0 }, "normal");
            });
        }else{
            jQuery(sgMn).find(".sgImage .sgImgDescr").text('');

            jQuery(sgMn).find(".sgImage .sgImageN").mouseover(function(){
                return;
            });
        }


        if(sgIe && options.sgResize){
            $(sgMn).find(".sgImage .sgAddinfo").css('left','0px');
        }

        //��������� � �������� ��� ���� (���� ������ �� ��������� thickbox ���������)
        var source=$(elem).find('em[class*=\'source\']').text();
        if(source){
            $(sgMn).find('.sgImage .sgAddinfo .sourcetext').text(source);
            $(sgMn).find('.sgImage .sgAddinfo .source').show();
        }else{
            $(sgMn).find('.sgImage .sgAddinfo .source').hide();
        }

        var uploader_nick=$(elem).find('em[class*=\'uploader_nick\']').text();
		var uploader_name=$(elem).find('em[class*=\'uploader_name\']').text();
        $(sgMn).find('.sgImage .sgAddinfo .uploader_nick').text(uploader_name);
        $(sgMn).find('.sgImage .sgAddinfo .uploader_nick').attr('href', '/'+uploader_nick+'/');

        var anchor=$(elem).find('em[class*=\'anchor\']').text();
        $(sgMn).find('.sgImage .sgAddinfo .anchor').attr('href', anchor);

        var is_editable=parseInt($(elem).find('em[class*=\'is_editable\']').text());
        if(is_editable){
            $(sgMn).find('.sgImage .sgAddinfo .edit .delref').attr('href', '/galleries/?action=gallery_delete&image_id='+image_id);
            $(sgMn).find('.sgImage .sgAddinfo .edit .delref').attr('onClick', 'delGalleryItem('+image_id+');return false;');
            $(sgMn).find('.sgImage .sgAddinfo .edit .editref').attr('href', '/galleries/?action=galleries_edit&width=550&height=235&image_id='+image_id);
            $(sgMn).find('.sgImage .sgAddinfo .edit').show();
        }else{
            $(sgMn).find('.sgImage .sgAddinfo .edit').hide();
        }

        $(sgMn).find('.sgImagearea .sgAddinfo').show();
    }

    function sgScrollThumbnails(e){
        var px, px1;
        px = jQuery(sgTw).offset().left;
        px1= px+jQuery(sgTw).width();

        var mouseX=sgIe?event.clientX+document.documentElement.scrollLeft:e.pageX;
        var mouseY=sgIe?event.clientY+document.documentElement.scrollTop:e.pageY;

        if(mouseX<(px+(px1-px)/4)){
            if(sgCd!='right'){
                sgStartScroll('right');
            }
        }else if(mouseX>(px+(px1-px)*3/4)){
            if(sgCd!='left'){
                sgStartScroll('left');
            }
        }else{
            sgCancelScroll();
        }
        return true;
    };

    function sgCancelScroll(){
        clearTimeout(sgTw.timer);
        sgCd='stop';
    }

    function sgStartScroll(direction){
        clearTimeout(sgTw.timer);
        sgCd=direction;

        var lt=$(sgMn).find('.sgThumbs').css('left');
        var pat=/px|em|pt/gi;
        lt=lt.replace(pat,'');
        lt=parseFloat(lt);

        var max=$(sgMn).find('.sgThumbarea').width();

        var rt=sgTWidth+lt;

        if(direction=='left' && rt>max){
            lt=lt-2;
        }else if(direction=='right' && lt<0){
            lt=lt+2;
        }
        $(sgMn).find('.sgThumbs').css('left', lt);
        sgTw.timer=setInterval(function(){sgStartScroll(direction);},5);
    }
};



