$(window).ready(function(){
    $('#mainmenu-root').dropdownMenu();
    $('#logoclick').click(function(){document.location='/'});
    if(GStart()) $(window).scroll(function(){GPosition();}).resize(function(){GPosition();});    
});

jQuery.fn.dropdownMenu=function(opt){
    return this.each(function(opt){
        $(this).find('li').hover(function(){
            var c=$(this).addClass('hover');
        //    c.find('>ul').css('visibility','visible');
        //    c.find('>a').addClass('hover');
            c.find('>span').addClass('hover');
        },
        function(){
            var c=$(this).removeClass('hover');
        //    c.find('>ul').css('visibility','hidden');
        //    c.find('>a').removeClass('hover');
            c.find('>span').removeClass('hover');
        });
    });
};
function tubePlayer(lnk){
    this.w=0,
    this.h=0,
    this.obj={},
    this.border=16,
    this.overlay={},

    this.init=function(lnk){
        this.obj=$('<div>'+lnk+'</div>');
        var yobj=this.obj.children();
        this.w=parseInt(yobj.attr('width'));
        this.h=parseInt(yobj.attr('height'));
        obj.prepend('<div style="background-image:url(/css/images/str_close.gif);background-position:center;background-repeat:no-repeat; width:14px;height:14px;position:absolute;top:0px;right:1px;cursor:pointer;"></div>')
    },
    this.create=function(){
        this.overlay=$('<div>').attr('id','goverlay').hide();
        $('body').append(this.overlay).append(this.obj);
        this.overlay.fadeTo(200,0.8);
        this.obj.width(this.w+this.border);
        this.obj.height(this.h+this.border);
        this.obj.css('padding-left',this.border+'px').css('padding-top',this.border+'px').css('position','absolute').css('background-color','#444444');
        $(window).bind('resize scroll',this.resize);
        t=this;
        this.obj.children().click(function(){t.destroy();});
        this.overlay.click(function(){t.destroy();});
        this.resize();

    },
    this.destroy=function(){
        $(window).unbind('resize scroll',this.resize);
        this.obj.unbind();
        this.overlay.unbind();
        this.obj.remove();
        this.overlay.fadeOut(200,function(){$(this).remove();});
        var t=this;
        delete t;
    },
    this.resize=function(){
        var width=document.compatMode=='CSS1Compat'&&!window.opera?document.documentElement.clientWidth:document.body.clientWidth;
        var height=document.compatMode=='CSS1Compat'&&!window.opera?document.documentElement.clientHeight:document.body.clientHeight;
        var cwsh=this.h+this.border*2;
        var cwsw=this.w+this.border*2; 
        var h=height/2-cwsh/2;
        var w=width/2-cwsw/2;
        var hscroll=$(window).scrollTop();
        h=h+hscroll;
        this.obj.css('top',h+'px').css('left',w+'px');
        this.overlay.css('height',height+hscroll+'px').css('width',width+'px');
    };    

    this.init(lnk);
    this.create();
}
function GStart(){
    var items=$('.galleryitem a','#center');
    if(items.length) {
      items.each(function(){$(this).click(function(){GView($(this).parents('.galleryitem'));}).attr('href','javascript:;');});
      return true;
    }
    return false;
}
function GInit(){    // инициализация galleryViewer
    if(document.getElementById('gview')){
        $('#preload').html('');
    }
    else{
        var cont=$('<div id="gview" unselectable="on"><div class="close"></div><div class="header"></div><table unselectable="on" cellspacing=0 cellpadding=0 border=0><tr><td id="gwl"></td><td unselectable="on" id="gwc"></td><td id="gwr"></td></tr></table><div class="footer"></div></div>').hide();
        var preload=$('<div>').attr('id','preload').hide();
        var overlay=$('<div>').attr('id','goverlay').hide();
        overlay.click(function(){GHide();});
        $('body').append(overlay).append(cont).append(preload);
        $('#gwr').hover(function(){$(this).css('background-position','13px center');},function(){$(this).css('background-position','12px center');});
        $('#gwl').hover(function(){$(this).css('background-position','5px center');},function(){$(this).css('background-position','6px center');});
        $('.close','#gview').click(function(){GHide();});
    }
}
function GPosition(){
    var width=document.compatMode=='CSS1Compat'&&!window.opera?document.documentElement.clientWidth:document.body.clientWidth;
    var height=document.compatMode=='CSS1Compat'&&!window.opera?document.documentElement.clientHeight:document.body.clientHeight;
    var cwsh=600;
    var cwsw=750; 
    var h=height/2-cwsh/2;
    var w=width/2-cwsw/2;
    h=h+$(window).scrollTop();
    $('#gview').css('top',h).css('left',w);
}
function GShow(cont,active,image){
    var next=active.next();
    var prev=active.prev();
    var nextSrc=$('img',next).attr('w');
    var prevSrc=$('img',prev).attr('w');
    $('#gwc').addClass('loading');
    if(!window.opera){
        image.load(function(){GOnLoad(this,prevSrc,nextSrc);});
        $('img','#gwc').fadeOut(500);
    }
    else{
        $('img','#gwc').fadeOut(500);
        GOnLoad(image,prevSrc,nextSrc);
    }
    var main=$('#gview');
    main.fadeIn(200);
    $('#goverlay').css('height',$(document).height()).fadeTo(200,0.8);
    var header=cont.parent().children('.maintitle');
    var htext=$('.header',header).text();
    if($('.text',header).text()) htext+=' - '+$('.text',header).text();
    $('.header',main).text(htext);
    $('.footer',main).text($('img',active).attr('alt'));
    var r=$('#gwr');
    r.unbind('click');
    if(nextSrc){r.removeClass('hide').click(function(){GView(next);});} else {r.addClass('hide');}
    var l=$('#gwl');
    l.unbind('click');
    if(prevSrc){l.removeClass('hide').click(function(){GView(prev);});} else {l.addClass('hide');}
}
function GView(obj){
    GInit();
    GPosition();
    var cont=obj.parents('.galleryitems');
    var activeobj=$('img',obj);
    var image=$('<img src="'+activeobj.attr('w')+'">');
    GShow(cont,obj,image);
}
function GOnLoad(image,prev,next){
    $('img','#gwc').remove();
    $('#gwc').removeClass('loading').append($(image).hide());
    $(image).fadeIn(500).css('display','inline');    
    var pr='';
    if (prev) pr+='<img width="0" src="'+prev+'">';
    if (next) pr+='<img width="0" src="'+next+'">';
    if (pr>'') {$('#preload').html('').append(pr).height(0).show();}
}
function GHide(){
    $('#gview').fadeOut(200,function(){$('img','#gwc').remove();});
    $('#goverlay').fadeTo(200,0,function(){$(this).hide();});
}

var comment_code_valid=false;
var comment_block_ajax=false;
var comment_timeout='';
function addComment(){
    $('#add-comment-text').hide();
    $('#commentForm').show();
    $('#comment_submit').attr('disabled','disabled');
    $('#comment_preview').attr('disabled','disabled');
    $('input[type="text"], textarea','#commentForm').val('').keyup(onUpdateComment);
    onUpdateComment()
}

function onUpdateComment(){
    var form=$('#commentForm');
    var name=$('#comment_name').val();
    var text=$('#comment_text').val();
    var code=$('#comment_code').val();
    if(name>'' && text>''){
        if(code.length>=6) $('#comment_submit').removeAttr('disabled');
        $('#comment_preview').removeAttr('disabled');
    }
    else $('#comment_submit').attr('disabled','disabled');
}
function previewComment(){
}

function submitComment(){
    var data={};
    $('#comment_submit').attr('disabled','disabled');
    var form=$('#commentForm');
    $('input:not([type="button"]), textarea',form).each(function(){data[$(this).attr('name')]=$(this).val();});
    jQuery.post(form.attr('action'),data,function(data){
                                                if(data.success==true) {
                                                    $('#commentForm').hide();
                                                    fin=$('<div id="commentFinish">Ваш комментарий принят и будет опубликован в ближайшее время.</div>');
                                                    $('#commentForm').after(fin);
                                                }
                                                else{
                                                    $('#comment_captcha_update').click();
                                                    $('#comment_errors').html(data.errors);
                                                    $('#comment_code').val('');
                                                    onUpdateComment();
                                                }
                                                },"json");
}


