var q = function ($){
    $(document).ready(function(){
        if($.browser.mozilla && parseFloat($.browser.version.slice(0,3)) < 1.9){
            $('.iblock').not('.ads-block,.ad-block,.ad-col').each(function(){
                $(this).wrapInner($('<div class="iblock-in">'));
            });
        }
        if($.browser.opera && $.browser.version < 10){
            $('#layout').wrap($('<div id="l-wrap">'));
            $('#l-wrap').css({
                'overflow':'hidden',
                'position':'relative'
            });
        }

        var c = 0;
        $('.p-tab-d .carousel-wrap','#media-part').each(function(){
            c++;
            var id = '#c-'+c;
            var cCur,cTotal;
            $(this).attr('id','c-'+c);
            cTotal = $(id+' .carousel .c-item').size();
            $(this).find('.c-controls .c-cur').attr('id','c-cur'+c).html('1');
            $(this).find('.c-controls .c-total').html(cTotal);
            cCur = $('#c-cur'+c);
            if($.isFunction($.fn.jCarouselLite)){
                $('.carousel',id).jCarouselLite({
                    btnPrev: id+' .p-prev, '+id+' .prev-btn',
                    btnNext: id+' .p-next, '+id+' .next-btn',
                    visible: 1,
                    circular: false
                });
            }
            $(id+' .p-prev, '+id+' .prev-btn').click(function(){
                var curVal = parseInt(cCur.html());
                if(curVal > 1){
                    cCur.html(curVal-1);
                }
            });
            $(id+' .p-next, '+id+' .next-btn').click(function(){
                var curVal = parseInt(cCur.html());
                if(curVal < cTotal){
                    cCur.html(curVal+1);
                }
            });
            
        });
        if($.isFunction($.fn.placeholder)){
            $('input[placeholder]').placeholder();
        }

        if($('#timeline').size() > 0){
            var tl = {
                tl:$('#timeline'),
                tChW:0,
                calcWifth: function(){
                    tl.tl.children('li').each(function(){
                        tl.tChW += $(this).outerWidth(true);
                    });
                    tl.tl.css({
                        'width':Math.ceil(tl.tChW)+'px'
                    });
                }
            }
            tl.calcWifth();
        }
        if($.isFunction($.fn.jScrollPane)){
            $("#timeline-wrap").bind('jsp-initialised',function(event, isScrollable)
            {
                if(isScrollable){
                    $('#timeline-block').append(
                        $('<div id="tb-prev" class="prev-btn c-btn"/>'),
                        $('<div id="tb-next" class="next-btn c-btn"/>')
                        );
                    $('#tb-prev').mousedown(function(){
                        $('#timeline-wrap>.jspContainer>.jspHorizontalBar>.jspArrowLeft').trigger('mousedown.jsp');
                    });
                    $('#tb-next').mousedown(function(){
                        $('#timeline-wrap>.jspContainer>.jspHorizontalBar>.jspArrowRight').trigger('mousedown.jsp');
                    });
                }
            })
            .jScrollPane({
                showArrows : true,
                horizontalDragMinWidth:122,
                horizontalDragMaxWidth:122
            });
        }

        $('body').append(
            $('<div id="alt-block" class="pv-alt  sb-wrap"/>').append(
                $('<div id="alt-in" class="sb-in"/>'),
                $('<div class="sb stl"/>'),
                $('<div class="sb str"/>'),
                $('<div class="sb sbl"/>'),
                $('<div class="sb sbr"/>')
                ));


        $('.p-alt').hover(function(e){
            var dH;
            vDesc = ($(this).attr('alt') != '' )? $(this).attr('alt') : false;
            if(vDesc){
                $('#alt-in').empty().append(vDesc);
                dH = $('#alt-block').outerHeight()+5;
                $('#alt-block').show();
                $(this).parent().mousemove(function(e){
                    $('#alt-block').css({
                        'top':e.pageY-dH+'px',
                        'left':e.pageX-12+'px'
                    });
                });
            }
        },
        function(){
            if(vDesc){
                $('#alt-block').hide();
                $('#alt-in').empty()
                $(this).unbind('mousemove');
            }
        });
        var pCount = 0;
        $('.player').each(function(){
            $(this).attr('id','player'+(++pCount));
        });

        $('.ci-link').each(function(){
            var playerId = function(e){
                return e.parents('.ptd-in').find('.player').attr('id');
            }($(this));
            $(this).click(function(event){

                if ($(this).attr("href") != $(this).attr("href").replace('http://www.youtube.com', ''))
                {
                    event.preventDefault();
                    var href = $(this).attr('href');
                    var contName = playerId+'in';
                    var $e = $('#'+playerId);
                    $e.hide().css('left','-10000px');
                    swfobject.removeSWF(contName);
                    $e.empty().append($('<div id="'+contName+'" />'));
                    swfobject.embedSWF(href, contName, "352", "284", "9.0.0",'./js/expressInstall.swf',{},{
                        'allowFullScreen':true,
                        'wmode':'opaque'
                    });
                    $e.css({
                        'left':'0px'
                    }).show();
                }
                else if ($(this).attr("href") != $(this).attr("href").replace('video.rutube.ru', ''))
                {
                    event.preventDefault();
                     var href = $(this).attr('href');
                    var contName = playerId+'in';
                    var $e = $('#'+playerId);
                    swfobject.registerObject("myFlashContent", "9.0.0");
                    $e.hide().css('left','-10000px');
                    swfobject.removeSWF(contName);
                    $e.empty().append($('<div id="'+contName+'" />'));
                    $html = '<object width="352" height="284"><param name="movie" value="' + href + '"></param><param name="wmode" value="opaque"></param><param name="allowFullScreen" value="true"></param><embed src="' + href + '" type="application/x-shockwave-flash" wmode="opaque" width="352" height="284" allowFullScreen="true"></embed></object>';
                    $e.html($html);
                    $e.css({
                        'left':'0px'
                    }).show();
                }
                else
                {
                    event.preventDefault();
                    var href = $(this).attr('href');
                    var contName = playerId+'in';
                    var $e = $('#'+playerId);
                    swfobject.registerObject("myFlashContent", "9.0.0");
                    $e.hide().css('left','-10000px');
                    swfobject.removeSWF(contName);
                    $e.empty().append($('<div id="'+contName+'" />'));
					var img_src = $(this).attr('at');
                    var htm = '<object width="352" height="284" id="myFlashContent" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" style="visibility: visible;"><param value="/swf/player.swf" name="movie"><param value="true" name="allowfullscreen"><param value="file=' + href + '&image=' + img_src + '" name="flashvars"><!--[if !IE]>--><object width="352" height="284" data="/swf/player.swf" type="application/x-shockwave-flash"><param value="true" name="allowfullscreen"><param value="file=' + href + '&image=' + img_src + '" name="flashvars"><!--<![endif]--><b>Пожалуйста, установите <a href="http://www.adobe.com/go/getflashplayer">последнюю</a> версию FlashPlayer!</b><!--[if !IE]>--></object><!--<![endif]--></object>';
                    $e.html(htm);
                    $e.css({
                        'left':'0px'
                    }).show();
                }
            });
        });

        $('.ptd-in').each(function(){
            if($(this).find('.carousel li a').size()>0){
                $(this).find('.carousel li:eq(0) a:eq(0)').click();
            }
        });

        $('.p-tab-t').click(function(){
            $(this).siblings('.p-tab-t').removeClass('ptt-active');
            $(this).siblings('.p-tab-d').removeClass('ptd-active');
            $(this).addClass('ptt-active');
            $(this).next().addClass('ptd-active');
        });
    });
    
}(jQuery);

