$(document).ready(function(){      
    if ($.browser.opera || $.browser.mozilla) // опера и мозилла
    {
       $(".navigation-popup").css("padding-bottom", "2px");
    }; 
    if ($.browser.opera) // опера
    {
       $(".checkbox").css("top", "0px");
       if ($.browser.version < 9) {
           $(".video_overlay").css({"background" : "none"});
           $(".object-info").css({"z-index": "3000", "width": "350px"})
       
       }
    }; 
    if ($.browser.mozilla) {
        if ($.browser.version == '1.8.1.20') {
            $(".object-info").css("width", "350px");
        }
      
    }
    
    // top navigation 
	var navt_tds = $(".navigation-top td");
	var navt_pops = $(".navigation-popup");
	var navt_timeout = null;

	var navt_block_hide_ex = function(){
        navt_tds.removeClass('hover');
        $('.navigation-popup').hide();
    }
	var navt_block_hide = function(){
        
		if (navt_timeout) clearTimeout(navt_timeout);
		navt_timeout = setTimeout(navt_block_hide_ex, 300);
	}
	navt_tds.mouseover(function (){
        if (navt_timeout) clearTimeout(navt_timeout);navt_tds.removeClass('hover'); 
        $(this).addClass('hover'); 
        $(".navigation-popup").hide();
        $(this).find(".navigation-popup").show();
        if ($(this).find(".navigation-popup").length) {
            var position = $(this).find(".navigation-popup").position();
            if (position.left+$(this).find(".navigation-popup").width() >980) {
                var right = 980 - $(this).find(".navigation-popup").width();
                $(this).find(".navigation-popup").css('left', right+'px');
            }                
        }
    });
	navt_pops.mouseover(function (){if (navt_timeout) clearTimeout(navt_timeout);});
	navt_tds.mouseout(navt_block_hide);
	navt_pops.mouseout(navt_block_hide);
    
   
    
    
    /*main-news*/
    if ($(".main-news").length) {
        var newsLeft = $(".main-news").find(".news-items:eq(0)");
        var newsRight = $(".main-news").find(".news-items:eq(1)");
        var hLeft = newsLeft.height();
        var hRight = newsRight.height();
        if (hLeft > hRight) {
            newsRight.find(".news-center").append('<div style="width: 100%; height: '+(hLeft-hRight)+'px"></div>');
        }
        else  if (hLeft < hRight) {
            newsLeft.find(".news-center").append('<div style="width: 100%; height: '+(hRight-hLeft-1)+'px"></div>');
        }
        newsLeft.removeClass("info-visibility");
        newsRight.removeClass("info-visibility");
    }
    
    /*inner-left*/
    if ($(".inner-left").length && $(".inner-left-content").length) {
        var leftDivParent = $(".inner-left");
        var leftDiv = $(".inner-left-content");
        var rightDiv = $(".inner-right");
        var hR = rightDiv.height();
        var hP = leftDivParent.height();
        var h = leftDiv.height();
        if (hR>hP) hP = hR;
        if (hP > (h+50)) {
    
            leftDiv.append('<div class="noprint" style="width: 100%; height: '+(hP-h+55)+'px"></div>');
        }
    }    

    
    /* table class table */
    $('table.table').each(function(){
        if ($(this).find("th:first").length) {
            $(this).find("th:first").wrapInner('<div class="first" />');
            $(this).find("th").not(":first").not(":last").wrapInner('<div class="center" />');
            $(this).find("th:last").wrapInner('<div class="last" />');
            $(this).find("tr:even").addClass("graytr");            
        }
        else {
            $(this).find("tr:odd").addClass("graytr"); 
        }

    });
    
     $('.form-item').each(function(){
         if($(this).find("select").length) $(this).addClass("form-item-select");
    });
    
      $('.form').each(function(){//form-inner-no-head
          if (!$(this).find(".form-title").length) $(this).find(".form-inner").addClass("form-inner-no-head");
          if (!$(this).find(".niceBtn").length && !$(this).find("input:reset").length && !$(this).find("input:submit").length && !$(this).find("input:button").length) {
              $(this).find(".form-item:last").addClass("form-item-last2");
          }

    });  
    
    /*play video*/
 $(".video-prev").each(function(){
        if ($(this).next(".video-play").length) {
            var wB = $(this).attr("width");
            var wP = 56;
            var hB = $(this).attr("height");
            var hP = 56;
            var positionL = (wB-wP)/2;                
            var positionT = (hB-hP)/2;
            if ($(this).parent().find(".video-show").length) {
                $(this).parent().find(".video-show").each(function(){
                    $(this).css("top", "0px");
                    var bigImg = $(this).find("img:first");
                    var relInfo = bigImg.width()+"x"+bigImg.height();
                    videoInfo = bigImg.parent().find(".video-big-info");
                    videoInfo.css("width",bigImg.width()+"px").attr("id", videoInfo.height());//.addClass("none").addClass("hidden");
                    videoInfo.parent().addClass("none");
                    bigImg.attr("id", relInfo).attr({"width" : wB, "height" : hB}).parent().addClass("none");
                });                
            }
            $(this).next(".video-play").css({
                "top" : positionT+"px",
                "left" : positionL+"px"
            }).show();                
        }   
        $(this).parent().removeClass("video-info-visibility").addClass("none");
    });
    /*video-prev*/
    if ($(".video-prev").length) {
        $(".video-prev").each(function(){
     
        });
    }
    if ($(".video-name").length) {
        $(".video-name span").each(function(){
            $(this).click(function(){
                if (!$(this).parent().hasClass("video-name-active")) {$(".video-name-active").removeClass("video-name-active").parent().removeClass("active");}
                $(this).parent().toggleClass("video-name-active");
                $(this).parent().parent().toggleClass("active");
               // $(this).next("div").find(".video-text").hide().show();

            });
            $(this).hover(function(){
               $(this).parent().addClass("video-name-hover"); 
            }, function(){
                $(this).parent().removeClass("video-name-hover"); 
            });
        });
    }
       
    var p = new Array();
    var overlayDiv = $('.video_overlay');
    var smallSize = null;
    var opacity = 0.5;
    if ($.browser.opera && $.browser.version < 9) opacity = 1;
    var an_time = 800;
    $(".video-item:first").addClass("active").find(".video-name").addClass("video-name-active");
    $(".video-item").each(function(){
        var number = $(this).parent().children(".video-item").index($(this));
        var videShow = $(this).find('.video-show');        
        p[number] = null;
        var top10_r = $(this).find('.video-play').get();
        var top10_l = $(this).find('.top10_a_left').get();
        $(top10_r).mouseover(function (){$(this).addClass("t10_r_hover")});
        $(top10_r).mouseout(function (){$(this).removeClass("t10_r_hover")});
        $(top10_l).mouseover(function (){$(this).addClass("t10_l_hover")});
        $(top10_l).mouseout(function (){$(this).removeClass("t10_l_hover")});
        $(top10_r).click(function (){
            if ($.browser.opera && $.browser.version < 9) {
                location.href=$(this).parents(".video-item").find(".video-link a").attr("href"); return true;
            }
            $(this).addClass("hidden");
            smallSize = $(this).parent().find("img:first").attr("width");
            smallSize += "x"+$(this).parent().find("img:first").attr("height");
            overlayDiv.css({width: $(document).width(),height:$(document).height()});
            overlayDiv.css({'opacity': opacity, "z-index" : "260"}).fadeIn(400);
            var bigDiv = $(this).parent().find(".video-show");
            bigDiv.removeClass("none");
            var bigPhoto = $(this).parent().find(".video-show").find("img:first").addClass("imgInShow");
            var number = $(this).parents(".video-item").attr("id").split("video-item-").join("");
            var videoInfo = $(this).parent().find(".video-big-info");
            var realSize = explode("x", bigPhoto.attr("id"));
            bigPhoto.animate( { width: realSize[0]+"px", height: realSize[1]+"px"}, an_time, false, function(){
                bigDiv.css({
                    'height' : bigPhoto.height()+intval(videoInfo.attr("id")),
                    'width' : bigPhoto.width(),
                    'background' : 'url(/images/preloder.gif) center center no-repeat'
                });
                if (p[number]) {
                    
                    bigPhoto.hide();
                    var video = bigPhoto.parent();
                    p[number].appendTo(video);
                  //  p[number] = null;
                }
                bigPhoto.hide().parent().find(".video-player").show();
                videoInfo.removeClass("hidden");
                
            })
            $(".top10_block").fadeIn(an_time);
        });        
    });
    overlayDiv.click(function (){
        var top10_r = $(this).find('.video-play').get();
        var top10_l = $(this).find('.top10_a_left').get();
        var realSize = explode("x", smallSize);
        var bigPhoto = $(".imgInShow");
        var number = bigPhoto.parents(".video-item").attr("id").split("video-item-").join("");
        var playIcon = bigPhoto.parents(".video-item").find(".video-play");
        var bigDiv = bigPhoto.parents(".video-item").find(".video-show");
        bigPhoto.parent().css("background", 'none').find(".video-player").hide();
        bigPhoto.show();
        if (realSize[0] > 0) {
             bigPhoto.animate( { "width": realSize[0]+"px", "height": realSize[1]+"px", "top" : "0"}, an_time, false, function(){
                $(top10_r).show();
                bigPhoto.removeClass("imgInShow");
                playIcon.removeClass("hidden");
                if (number) {
                    p[number] = bigDiv.find(".video-player").detach();
                    bigDiv.addClass("none");
                }
            });                     
        }
        $(this).fadeOut(1200);
    });
    $(".video-close").click(function(){
        var top10_r = $(this).find('.video-play').get();
        var top10_l = $(this).find('.top10_a_left').get();
        var realSize = explode("x", smallSize);
        var bigPhoto = $(".imgInShow");
        var number = bigPhoto.parents(".video-item").attr("id").split("video-item-").join("");
        var playIcon = bigPhoto.parents(".video-item").find(".video-play");
        var bigDiv = bigPhoto.parents(".video-item").find(".video-show");
        bigPhoto.parent().css("background", 'none').find(".video-player").hide();
        bigPhoto.show();
        if (realSize[0] > 0) {
             bigPhoto.animate( { "width": realSize[0]+"px", "height": realSize[1]+"px", "top" : "0" }, an_time, false, function(){
                $(top10_r).show();
                bigPhoto.removeClass("imgInShow");
                playIcon.removeClass("hidden");
                if (number) {
                    p[number] = bigDiv.find(".video-player").detach(); 
                    bigDiv.addClass("none");
                }
            });                     
        }
        overlayDiv.fadeOut(1200);            
    });    
   
    /* tabs */
    if ($("#text-tab1").length) {
        $("#text-tab1").show();
    }
    $(".tabs div").click(function(){
        if (!$(this).hasClass("active")) {
            var tabtext = $(this).attr("id").split("tab").join("");
            if ($("#text-tab" + tabtext).length) {
                $(".tab-text").hide();
                $("#text-tab" + tabtext).show();    
            }
            $(".tabs div.active").toggleClass("active");
            $(this).toggleClass("active");
        }
    });
 
     /*main-tabs*/
    if($(".map-tabs").length){
       $(".map-tabs").find("div").click(function(){
           if ($(this).hasClass("active")) return false;
           $(this).parent().find(".active").removeClass("active");
           $(this).addClass("active");
           if ($(this).hasClass("map-tab-first")) {
               $(this).parent().removeAttr("style");
               $(this).parent().css("background-position", "left top");
               $(".new-object").removeClass("hide"); 
               $(".affiliates, .points, .in-affiliates").addClass("hide"); 
               $(".in-object").show();   
           }
           else {
               
               $(".new-object").addClass("hide");
               $(".affiliates, .points, .in-affiliates").removeClass("hide"); 
               $(".in-object").each(function(){
                   if(!$(this).hasClass("in-affiliates")) $(this).hide();
               })
               $(this).parent().removeAttr("style");
               if ($.browser.msie && $.browser.version < 7) $(this).parent().css("background-position", "left bottom");    
               $(this).parent().css("background-position", "left -29px");               
           } 
       }); 
    }
    
    /*main map objects*/
    if($(".map-object").length) {
        var obTimeout = null;
        $(".map-object").each(function(){
            var number = $(this).parent().children(".map-object").index($(this));
            if ((!$.browser.msie || $.browser.version > 6) && (!$.browser.opera || $.browser.version > 8)) simple_tooltip($(this),"toll", number, "right");   
            $(this).hover(function(){$(this).css("z-index", 3000)}, 
            function(){$(this).css("z-index", $(this).find("a").attr("rel"))}); 

        });
        $(".object-info").each(function(){
            if ($.browser.safari ) {
               $(this).find(".object-info-left-bottom img").load(function () {
                    var leftH = $(this).parents(".object-info").find(".object-info-left-bottom").height();
                    var rightH = $(this).parents(".object-info").find(".object-info-right-bottom").height(); // поправка на разницу высот шапок
                    if(leftH > rightH) $(this).parents(".object-info").find(".object-info-right-bottom").height(leftH);
                    if(leftH < rightH) {
                        rightH+=9;
                        $(this).parents(".object-info").find(".object-info-left-bottom").height(rightH);
                        $(this).parents(".object-info").find(".object-info-right-bottom").height(rightH);
                    }
                    $(this).parents(".object-info").fadeOut(400);
                });                
            }
            else {
                
                var leftH = $(this).find(".object-info-left-bottom").height();
                var rightH = $(this).find(".object-info-right-bottom").height(); // поправка на разницу высот шапок
                
                if ($.browser.msie && $.browser.version < 7) rightH = rightH + 23; 
                if(leftH > rightH) $(this).find(".object-info-right-bottom").height(leftH);
                if(leftH < rightH) {
                    if ($.browser.msie && $.browser.version < 7) rightH-=5;
                    else rightH+=9;
                    $(this).find(".object-info-left-bottom").height(rightH);
                    $(this).find(".object-info-right-bottom").height(rightH);
                    
                }
                $(this).parents(".object-info").fadeOut(400);
        }              
            
        });
        $(".map-object").click(function(){
            $(".map-tabs").css("z-index", "18");
            var maxW = 577;
            var pos = $(this).position();
            var obInfo = $("#object-info"+$(this).attr("id").split("map-object").join(""));
            var pLeft = pos.left - 50;
            var pTop = pos.top - 60;
            if ((pLeft+obInfo.width()) > maxW) pLeft = maxW-obInfo.width();
            $(".object-info").addClass("hide");
            if ($.browser.msie && $.browser.version < 7) {
                obInfo.find(".object-info-left").css("width", obInfo.find(".object-info-left img").width()+"px");
               
            }
            obInfo.removeClass("hide").css({
                'left' : pLeft+"px",
                'top' : pTop+"px"
            }).fadeIn(400);
            return false;
        });
        
        $(".object-info").mouseout(function(){
           obTimeout = setTimeout(function(){
               $(".object-info").fadeOut(400, function(){$(this).addClass("hide");$(".map-tabs").css("z-index", "22");});
               clearTimeout(obTimeout);
           },1000); 
        });
        $(".object-info").mouseover(function(){if(obTimeout) clearTimeout(obTimeout)});
        $(".object-info-close").click(function(){$(".object-info").fadeOut(400, function(){$(this).addClass("hide");$(".map-tabs").css("z-index", "22");}); if (obTimeout) clearTimeout(obTimeout);})
        
   }
   
   // affiliates
   if($(".affiliate-object").length) {
        var obTimeout = null;
        
        $(".affiliate-object").each(function(){
            var number = $(this).parent().children(".affiliate-object").index($(this));
           number +=1000;
           if ((!$.browser.msie || $.browser.version > 6) && (!$.browser.opera || $.browser.version > 8)) simple_tooltip2($(this),"toll", number, "right", $(this).attr("title"));  
           $(this).hover(function(){$(this).addClass("points-active")}, 
            function(){$(this).removeClass("points-active")}); 
            

        });
        
        $(".affiliate-object").click(function(){
            if (!$(this).hasClass("affiliate-object-inner")) {
                $(".map-tabs").css("z-index", "18");
                $(".main-news").css("z-index", "18");
                var maxW = 577;
                var pos = $(this).position();
                var obInfo = $("#affiliate-info"+$(this).attr("id").split("map-affiliate").join(""));
                var pLeft = pos.left - 50;
                var pTop = pos.top - 60;
                if ((pLeft+obInfo.width()) > maxW) pLeft = maxW-obInfo.width();
                $(".affiliate-info").addClass("hide");
                obInfo.removeClass("hide").css({
                    'left' : pLeft+"px",
                    'top' : pTop+"px"
                }).fadeIn(400);
                return false;                
            }
            return false; 
        });
        
        $(".affiliate-info").mouseout(function(){
           obTimeout = setTimeout(function(){
               $(".affiliate-info").fadeOut(400, function(){$(this).addClass("hide");});
               $(".map-tabs").css("z-index", "22");
               $(".main-news").css("z-index", "200");
               clearTimeout(obTimeout);
           },1000); 
        });
        $(".affiliate-info").mouseover(function(){if(obTimeout) clearTimeout(obTimeout)});
        $(".affliate-info-close").click(function(){$(".affiliate-info").fadeOut(400, function(){$(this).addClass("hide"); $(".map-tabs").css("z-index", "22");}); if (obTimeout) clearTimeout(obTimeout);})
        
   }

    
    
    /* search form */
    $(".search-main-lupa").click(function(){
        $('#search-form').submit();    
    });
    /* subscribe form */
    var subText = "e-mail";
    $("#subscribe-form input:text").each(function(){
        if ($(this).val().length < 3) 
            $(this).val(subText);
        $(this).focus(function(){
            if($(this).val() == subText) {
                $(this).val("");
            }
        });
        $(this).blur(function(){
            if ($(this).val().length < 3)  $(this).val(subText);
        });
    });
    $(".subscribe-main-lupa").click(function(){
        $('#subscribe-form').submit();    
    });
    $('#subscribe-form').submit(function(){
        if($(this).find("input:text").val() == subText) {
            return false;
        } else {
            return true;
        }
    });

       /*buttons*/
    $('input:submit').transformBtn("niceBtn");
    $('input:button').transformBtn("niceButton");
    $('input:reset').transformBtn("niceButton");
  //  $('input:checkbox', this).each(CheckAdd);
  //  $('input:radio', this).each(RadioAdd);
  //  $("form").jNice(); // для select
    
    /* выравнивание формы подписки ie7 */
    if($.browser.msie && $.browser.version < 8)  {
        $(".main-top-info-photo").find(".label").hide().show();
        $(".language").find(".label").hide().show();
    }
    
    /*accordion-min for headers*/
    $(".cname").each(function(){
        $(this).find("span").find("span").addClass("work-position");
        $(this).find("span:first").addClass("top-manager-span");
    });
    $(".cname .top-manager-span").click(function(){
        if($(this).parents(".cname").hasClass("cname-active")) {
            $(this).parents(".cname").removeClass("cname-active");
            $(this).parents(".cname").next(".cinner").hide();
        }
        else {
            $(this).parents(".cname").next(".cinner").show();
            $(this).parents(".cname").addClass("cname-active");
        }
    });

    $("a.fancybox").fancybox({'titleShow'     : false});    
     $("img.img-fancybox").each(function(){
         $(this).fancybox({
                'titleShow'     : false, 
                "href" : $(this).attr("src")
         });
     }) 
     
     if ($(".fancy-question").length){
        $(".fancy-question").each(function(){
            $(this).fancybox({
            });
        });
        $("#error-choice-click").fancybox();
        $(".answer-news-question .niceButton").click(function(){
            var form = $(this).parents(".answer-news-question");
            var showNews = form.find('input:radio:checked').val();
            if (showNews == "Y") {
                var url = form.find("input:hidden:eq(1)").val();
                url += form.find("input:hidden:eq(0)").val()+"/";
                location.href=url;
            }
            else {
                form.find('input:radio:checked').removeAttr("checked");
                
                $("#error-choice-click").click();
            }
        });
     }
     
     if ($.browser.mozilla) {
        if ($.browser.version == '1.8.1.20') {;
            $("button span").css({"padding-left":"11px;"});
            $("button span span").css({"margin-right" : "0px", "right" : "15px", "padding-left" : "20px"});
        }
      
    }

    
});   




/*buttons*/
$.fn.transformBtn = function(varclass){
    return $(this).each(function(){
        var onclick='';
        for (var i = 0; i < this.attributes.length; i++) {
            if (this.attributes[i].nodeName=='onclick'){
             onclick = ' onclick="'+this.attributes[i].nodeValue+'"';
             //console.log(onclick )         
            }
        } 
        var btn = $('<button type="'+ this.type +'" ' + onclick + ' value="'+ this.value +'" class="'+varclass+'"><span><span><span><nobr>'+this.value+'</nobr></span>'+this.value+'</span></span></button>');
        btn.focus(function(){ $(this).addClass('niceBtnFocus')}).blur(function(){ $(this).removeClass('niceBtnFocus')});
        btn.mouseover(function(){ $(this).addClass('niceBtnHover')}).mouseout(function(){ $(this).removeClass('niceBtnHover')}); 
        if (typeof this.name != 'undefined') btn.attr('name', this.name);
        if (typeof this.id != 'undefined') btn.attr('id', this.id);

        $(this).replaceWith(btn);
    });
};

     function strpos (haystack, needle, offset) {
        var i = (haystack+'').indexOf(needle, (offset || 0));
        return i === -1 ? false : i;
    } 
    
    
    function explode( delimiter, string ) {
        var emptyArray = { 0: '' };
        if ( arguments.length != 2
            || typeof arguments[0] == 'undefined'
            || typeof arguments[1] == 'undefined' )
        {
            return null;
        }
        if ( delimiter === ''
            || delimiter === false
            || delimiter === null )
        {
            return false;
        }
        if ( typeof delimiter == 'function'
            || typeof delimiter == 'object'
            || typeof string == 'function'
            || typeof string == 'object' )
        {
            return emptyArray;
        }
        if ( delimiter === true ) {
            delimiter = '1';
        }
        return string.toString().split ( delimiter.toString() );
    }
    function intval (mixed_var, base) {
        var type = typeof( mixed_var );
     
        if (type === 'boolean') {
            return (mixed_var) ? 1 : 0;
        } else if (type === 'string') {
            tmp = parseInt(mixed_var, base || 10);
            return (isNaN(tmp) || !isFinite(tmp)) ? 0 : tmp;
        } else if (type === 'number' && isFinite(mixed_var) ) {
            return Math.floor(mixed_var);
        } else {
            return 0;
        }
    }
    
    function simple_tooltip(target_items, name, number, position){
        $(target_items).each(function(){
            var thtml = "<div class='"+name+"' id='"+name+number+"'><div class='r-star-shape'><div class='cn tl'></div><div class='cn tr'></div><div class='content-tool'>"+$(this).find("img").attr('alt')+"</div><div class='cn bl'></div><div class='cn br'></div></div></div>";
            $("body").append(thtml);
            $(this).removeAttr("title");
            $(this).find("img").removeAttr("alt");
            var pos = $(this).position();
            var wWight = $(window).width();
            if (pos.left > 500 && wWight < 1020) position = 'left';
            var my_tooltip = $("#"+name+number);
            $(this).removeAttr("title").mouseover(function(){ 
                my_tooltip.css({display:"none"}).show();
            }).mousemove(function(kmouse){
                if(position == 'right') my_tooltip.css({left:kmouse.pageX+6, top:kmouse.pageY+9});
                else if(position == 'left') my_tooltip.css({left:kmouse.pageX-$("#"+name+number).width(), top:kmouse.pageY+10});
            }).mouseout(function(){ 
                my_tooltip.hide();
            });
        });
    }
    
    function simple_tooltip2(target_items, name, number, position, name2){
        $(target_items).each(function(){
            var thtml = "<div class='"+name+"' id='"+name+number+"'><div class='r-star-shape'><div class='cn tl'></div><div class='cn tr'></div><div class='content-tool'>"+name2+"</div><div class='cn bl'></div><div class='cn br'></div></div></div>";
            $("body").append(thtml);
            var pos = $(this).position();
            var wWight = $(window).width();
            if (pos.left > 500 && wWight < 1020) position = 'left';
            var my_tooltip = $("#"+name+number);
            $(this).removeAttr("title").mouseover(function(){ 
                my_tooltip.css({display:"none"}).show();
            }).mousemove(function(kmouse){
                if(position == 'right') my_tooltip.css({left:kmouse.pageX+6, top:kmouse.pageY+9});
                else if(position == 'left') my_tooltip.css({left:kmouse.pageX-$("#"+name+number).width(), top:kmouse.pageY+10});
            }).mouseout(function(){ 
                my_tooltip.hide();
            });
        });
    }

