var pos = 2;
var AfterLoad=true,SecondRotate=false;
var BaseLeft,BaseTop;
var iphV_left,iphV_top;
var content_top,mainV_top,content_left,OverlapVb_h,containerLeftV_top,containerRightV_top,containerLeftV_left,containerRightV_left,iphVb_top;
var iphH_left,iphH_top,iphHb_top,iphHt_top;
var HideAll_left,HideAll_top;
var arrPageSizes;
window.onresize=RenderPage;
var Bloked=true;
var Rotating=false;
var ContentLoaded=false;
var IE6,IE7=false;
var VerticalOffset=0;
var OnlyTop=false;
$(document).ready(function (){
        $('.img_link').each(function(){
            var left=parseInt($(this).children('img:first').css('left'));
            var top=parseInt($(this).children('img:first').css('top'));
            $(this).attr('real_left',left);
            $(this).attr('real_top',top);
        });
    var  browser_detect = navigator.userAgent.toLowerCase();
    //if ((browser_detect.indexOf("msie") + 1)) IE7=true;
    IE6 = false /*@cc_on || @_jscript_version < 5.7 @*/;
    if((navigator.appVersion.indexOf("MSIE")!=-1)) IE7=true;

    if(NeedRotate&&FlashDetect.installed&&!iPad){
        $('#OverFlowAll').hide();
        $('#HideAll').hide();
        $('.posH, .iph,.posV').hide();
    }
    if(ThisPageMain) $('#MainLink').hide();

     if(InitPos=='v'){
         $('.posH').hide();
         pos=1;
     }
     else{
         $('.posV').hide();
         pos=2;
     }
     RenderPage();
     if(IE6){
        $('#iphone_wrap').css('left', -1000);
        $('#iphone_wrap').css('top', -1000);
     }
     $('#OverFlowAll').width(arrPageSizes[0]);
     $('#OverFlowAll').height(arrPageSizes[1]);
        //встраиваем flash
            if(NeedRotate&&FlashDetect.installed&&!iPad){
                swfobject.embedSWF(base_url+"_res_/move.swf", //путь до ролика
                                        "iphone", //id элемента страницы в который встраиваем flash
                                        "1250", //ширина
                                        "1250", //высота
                                        "9.0.0", //версия Flash плеера необходимого для данного SWF
                                        base_url+"_res_/expressInstall.swf", //задает URL вашего express install SWF и активирует Adobe express install
                                        {								  //[http://www.adobe.com/cfusion/knowledgebase/index.cfm?id=6a253b7521]
                                                pos: 'h', //может быть v/h соответсвено позиция первоначальная вертикальная горизонтальная
                                                played: 'true', //начинать крутить при инициализации true - да, false - нет
                                                callback: 'iPhoneHandler' //имя функции callback
                                        },                                        
                                        {
                                                allowScriptAccess: 'sameDomain',
                                                wmode:  'opaque',
                                                quality: 'high'
                                        },
                                        null);
            }
            else{
               $('.iph').show();
               if(pos==1) $('.posV').show();
               else  $('.posH').show();
            }
      //  bind_click();
        $('.img_link').hover(
        function() {
             clearInterval($(this).attr('TimerID'));
            var what_hv=$(this).children('img:first');
             $(what_hv).css("z-index", "5000");
            //$(what_hv).attr('src_saved',$(what_hv).attr('src'));
            $(what_hv).attr('src',base_url+ "images/buttons/"+ $(what_hv).attr('hv_src'));
            var left=parseInt($(what_hv).css('left'));
            var top=parseInt($(what_hv).css('top'));
            if(parseInt($(what_hv).width())<150){
                var TimerID=setInterval(function (){
                        $(what_hv).width($(what_hv).width()+4);
                        $(what_hv).height($(what_hv).height()+4);
                        left-=2;
                        $(what_hv).css('left',left+'px');
                        top-=2;
                        $(what_hv).css('top',top+'px');
                        if(parseInt($(what_hv).width())>150) clearInterval(TimerID);
                   },10);
                $(this).attr('TimerID', TimerID);
            }
        },function() {
            clearInterval($(this).attr('TimerID'));
            var what_hv=$(this).children('img:first');
            $(what_hv).css("z-index", "1000");
            $(what_hv).attr('src',base_url+ "images/buttons/"+ $(what_hv).attr('base_src'));
           // var real_left=$(this).attr('real_left');
            var left=parseInt($(what_hv).css('left'));
            var top=parseInt($(what_hv).css('top'));
            if(parseInt($(what_hv).width())>75){
                    var TimerID=setInterval(function (){
                        $(what_hv).width($(what_hv).width()-4);
                        $(what_hv).height($(what_hv).height()-4);
                        //if(left<=real_left){
                            left+=2;$(what_hv).css('left',left+'px');
                            top+=2;$(what_hv).css('top',top+'px');                            
                       // }
                       if(parseInt($(what_hv).width())<75) clearInterval(TimerID);
                },10);
                $(this).attr('TimerID', TimerID);
            }
            else clearInterval($(this).attr('TimerID'));
    });

//keypress
//keydown
 //$(document).keypress (checkKey);
 if(!iPad&&!IE7){
        if (browser_detect.indexOf("opera")!=-1|| browser_detect.indexOf("firefox")!=-1) {
            $(document).keypress (checkKey);
        } else {

            $(document).keydown (checkKey);
        }

 }
                    
});



function checkKey(event){

   // if(Bloked) return false;
    switch (event.keyCode) {
        case 38:
            VerticalOffset+=15;
            OnlyTop=true;
            RenderPage();
            OnlyTop=false;
            return false;
            break;
        case 40:
            VerticalOffset-=15;
            OnlyTop=true;
            RenderPage();
            OnlyTop=false;
            return false;
            break;
        default:
            return true;
            break;
    }

}


function bind_click(){
        $('a').bind('click',
        function (){
            if(Bloked) return false;
            var new_pos;
            if($(this).hasClass('External')) return true;
            Bloked=true;
            if($(this).hasClass('img_link')){
                clearInterval($(this).attr('TimerID'));
                var what_hv=$(this).children('img:first');
                $(what_hv).attr('src',base_url+ "images/buttons/"+ $(what_hv).attr('base_src'));
                $(what_hv).width(75);
                $(what_hv).height(75);
                $(what_hv).css('left',$(this).attr('real_left')+'px');
                $(what_hv).css('top',$(this).attr('real_top')+'px');
            }

            if($(this).hasClass('toH')) new_pos=2;
            else  new_pos=1;
            if($(this).hasClass('toMain')) ThisPageMain=1;
            else ThisPageMain=0;
            if(new_pos!=pos){
                $('.posH, .iph,.posV').hide();
                Rotating=true;
                ContentLoaded=false;
                $('#OverFlowAll').hide();
                 if(/msie|MSIE 6/.test(navigator.userAgent)){
                     $('#iphone_wrap').css('left', BaseLeft);
                     $('#iphone_wrap').css('top', BaseTop);
                 }
                document.getElementById('iphone').turn(pos);
            }
            else{
                $('#HideAll').show();
            }
    $('#content').text("");
    var url=$(this).attr('href');
    $.ajax({
           type: "POST",
           dataType:"json",
           url: url,
           data:{'_ajax_':''},
           success: function(data){
               $('#content').html(data.text
                +'<div id="counters"> <a href="/mapsite/">карта  сайта</a> <br>'+"<a href='http://www.liveinternet.ru/click' "+
                    "target=_blank><img src='http://counter.yadro.ru/hit?t18.1;r"+
                    escape(document.referrer)+((typeof(screen)=="undefined")?"":
                    ";s"+screen.width+"*"+screen.height+"*"+(screen.colorDepth?
                    screen.colorDepth:screen.pixelDepth))+";u"+escape(url)+
                    ";"+Math.random()+
                    "' alt='' title='LiveInternet: показано число просмотров за 24"+
                    " часа, посетителей за 24 часа и за сегодня' "+
                    "border=0 width=88 height=31><\/a></div>");
               bind_click();
               document.title=data.title;
               Bloked=false;
               $('#HideAll').hide();
               ContentLoaded=true;
               if(!Rotating){
                   $('.iph').show();
                   if(pos==1) $('.posV').show();
                   else  $('.posH').show();
                   if(ThisPageMain) $('#MainLink').hide();
                   else $('#MainLink').show();
                  /*  if(IE6)
                    {
                        if(pos==1) $('#TopLinksV').css('left','3px');
                        else $('#TopLinksH').css('left','33px');
                    }
                    if(IE7)
                    {
                        if(pos==1) $('#TopLinksV').css('left','73px');
                        else $('#TopLinksH').css('left','121px');
                    }*/

               }
             },
           error: function(XMLHttpRequest, textStatus, errorThrown){
            $('#content').html("Ошибка!<br/>"+XMLHttpRequest.responseText);
            }
           });
            return false;
        });    
    
}


function RenderPage(){
     arrPageSizes = ___getPageSize();
     if(!OnlyTop) BaseLeft=Math.round((arrPageSizes[2]-1300)/2);
     if(arrPageSizes[3]<694) BaseTop= Math.round((arrPageSizes[3]-1280)/2-(arrPageSizes[3]-1280)*0.1);
     else BaseTop=Math.round((arrPageSizes[3]-1280)/2);
    BaseTop+=VerticalOffset;
    
    if(!OnlyTop) iphV_left=BaseLeft+266;
    iphV_top=BaseTop+211;
    mainV_top=BaseTop+237;
    iphVb_top=BaseTop+970;

    if(!OnlyTop) iphH_left=BaseLeft+155;
    iphH_top=BaseTop+243;
    iphHb_top=BaseTop+810;
    iphHt_top=BaseTop+302;

    if(!IE6&&!IE7){
         if(!OnlyTop) $('#iphone_wrap').css('left', BaseLeft);
         $('#iphone_wrap').css('top', BaseTop);

         if(!OnlyTop)  $('#iphV,#iphVt,#iphVb').css('left', iphV_left);
         $('#iphV').css('top', iphV_top);
         $('#iphVt').css('top', mainV_top);
         $('#iphVb').css('top', iphVb_top);

         if(!OnlyTop) $('#iphH,#iphHt,#iphHb').css('left',iphH_left);
         $('#iphH').css('top', iphH_top);
         $('#iphHt').css('top', iphHt_top);
         $('#iphHb').css('top', iphHb_top);
     }

    RenderOrient();

}


function RenderOrient(){

    if(pos==1){

        if(!OnlyTop) content_left=BaseLeft+455;
        containerLeftV_top=containerRightV_top=BaseTop+420;
        if(!OnlyTop) containerLeftV_left=BaseLeft+327;
        if(!OnlyTop) containerRightV_left=BaseLeft+780;
        $('#content,#OverlapVt,#OverlapVb').width(390);
        if(iPad){ $('#content').width(330);
            $('#ArrowUP').css('left', "12px");
            $('#ArrowDOWN').css('left', "12px").css('bottom', "0px");
        }

         if(BaseTop+237>0) $('#OverlapVt').css('height', BaseTop+237);
         else  $('#OverlapVt').css('height', 0);
         if(IE6||IE7) OverlapVb_h=arrPageSizes[1]-BaseTop-1000;
         else{
            OverlapVb_h=arrPageSizes[3]-BaseTop-300;
            $('#OverlapVb').css('top', BaseTop+980);
         }
         if(OverlapVb_h>0) $('#OverlapVb').css('height', OverlapVb_h);
         else  $('#OverlapVb').css('height', 0);
        if(BaseTop+483>0) $('#content').css('padding-top', BaseTop+483);
        if(arrPageSizes[3]-BaseTop-850>0) $('#content').css('padding-bottom', arrPageSizes[3]-BaseTop-850);

        if(!OnlyTop) HideAll_left=BaseLeft+395;
        HideAll_top=BaseTop+395;
         $('#HideAll').removeClass('HideAllh').addClass('HideAllv');

    }
    else{

        if(!OnlyTop) content_left=BaseLeft+405;
        containerLeftV_top=BaseTop+420;
        if(!OnlyTop) containerLeftV_left=BaseLeft+257;
        containerRightV_top=BaseTop+420;
        if(!OnlyTop) containerRightV_left=BaseLeft+848;


        $('#content,#OverlapVt,#OverlapVb').width(495);
        if(iPad) $('#content').width(440);
         if(BaseTop+330>0) $('#OverlapVt').css('height', BaseTop+330);
         else  $('#OverlapVt').css('height', 0);
         if(IE6||IE7) OverlapVb_h=arrPageSizes[1]-BaseTop-900;
         else{
             OverlapVb_h=arrPageSizes[3]-BaseTop-200;
            $('#OverlapVb').css('top', BaseTop+980);
             
         }
         //alert(arrPageSizes[3]);
         if(OverlapVb_h>0) $('#OverlapVb').css('height', OverlapVb_h);
         else $('#OverlapVb').css('height', 0);
         
        if(BaseTop+483>0) $('#content').css('padding-top', BaseTop+483);
        if(arrPageSizes[3]-BaseTop-750>0) $('#content').css('padding-bottom', arrPageSizes[3]-BaseTop-750);

        if(!OnlyTop) HideAll_left=BaseLeft+317;
        HideAll_top=BaseTop+455;
         $('#HideAll').removeClass('HideAllv').addClass('HideAllh');

    }


    if(!IE6&&!IE7){
        if(!OnlyTop)  $('#OverlapVt,#OverlapVb').css('left', content_left);

         if(!OnlyTop) $('#containerLeftV').css('left', containerLeftV_left);
         if(!OnlyTop) $('#containerRightV').css('left', containerRightV_left);
         $('#containerLeftV').css('top', containerLeftV_top);
         $('#containerRightV').css('top', containerRightV_top);

         if(!OnlyTop) $('#HideAll').css('left', HideAll_left);
         $('#HideAll').css('top', HideAll_top);
    }

     if(!OnlyTop) $('#content').css('left', content_left);

}

function ContentUP(){
    var move=200;
    if(parseInt($('#content').css("top"))>-200)
        move=-parseInt($('#content').css("top"));
    if(move<3) return;
  $('#content').animate({
    top: '+='+move
  }, 1500);
}

function ContentDown(){
    var move=200;
    if(parseInt($('#content').css("height"))+parseInt($('#content').css("top"))<900)
        move=parseInt($('#content').css("height"))+parseInt($('#content').css("top"))-640;
    if(move<5) return;
  $('#content').animate({
    top: '-='+move
  }, 1500);
}


function iPhoneHandler(data){ //функция обратного вызова
        pos = data;
        if(AfterLoad){
            AfterLoad=false;
            if(InitPos=='h'){
                SecondRotate=true;                
                document.getElementById('iphone').turn(pos);
                return;
            }
            else{                
                 Bloked=false;
                 $('#HideAll').hide();
                   $('.iph').show();
                   if(pos==1) $('.posV').show();
                   else  $('.posH').show();
                   if(ThisPageMain) $('#MainLink').hide();
                   else $('#MainLink').show();
                 return;
            }
        }
        if(SecondRotate){
                 SecondRotate=false;
                 $('#HideAll').hide();
                   $('.iph').show();
                   if(pos==1) $('.posV').show();
                   else  $('.posH').show();
                   if(ThisPageMain) $('#MainLink').hide();
                   else $('#MainLink').show();
                 Bloked=false;
                 return;
        }
        RenderOrient();
         $('#OverFlowAll').show();
         if(IE6||IE7){
            $('#iphone_wrap').css('left', -1000);
            $('#iphone_wrap').css('top', -1000);
         }
        if(!ContentLoaded){
            $('#HideAll').show();
        }
        Rotating=false;
       $('.iph').show();
       if(pos==1) $('.posV').show();
       else  $('.posH').show();
       if(ThisPageMain) $('#MainLink').hide();
       else $('#MainLink').show();
      /*  if(IE6)
        {
            if(pos==1) $('#TopLinksV').css('left','3px');
            else $('#TopLinksH').css('left','33px');
        }
                    if(IE7)
                    {
                        if(pos==1) $('#TopLinksV').css('left','73px');
                        else $('#TopLinksH').css('left','121px');
                    }*/
}

