﻿$(document).ready(function() {

    $("#bnvideo_main").click(function() {
        var id = $(".main_news_id").attr('id');
        location.href = "/Home/News/" + id;
    });

    $("#bnvideo").click(function() {

        currentId = $(".main_news_id").attr('id');
        videolinkclick(v[currentId], currentId);

        try {
            pageTracker._trackEvent("Просмотр видео", "Главная страница (главная новость)", v[currentId].videoname, 1);
        } catch (err) {
        }

        $.get('/Home/UpdateNewsCounter/' + currentId);

        return false;
    });

    $("#bnfoto").click(function() {

        currentId = $(".main_news_id").attr('id');
        gallerylinkclick(g[currentId]);

        try {
            pageTracker._trackEvent("Просмотр галереи", "Главная страница (главная новость)", g[currentId].galleryname, 1);
        } catch (err) {
        }

        $.get('/Home/UpdateNewsCounter/' + currentId);

        return false;
    });

    $(".wbvideo").click(function() {

        currentId = $(this).attr('id').substr(2);
        videolinkclick(v[currentId], currentId);

        try {
            pageTracker._trackEvent("Просмотр видео", "Главная страница", v[currentId].videoname, 1);
        } catch (err) {
        }

        $.get('/Home/UpdateNewsCounter/' + currentId);

        return false;
    });

    $(".wbgallery").click(function() {

        currentId = $(this).attr('id').substr(2);
        gallerylinkclick(g[currentId]);

        try {
            pageTracker._trackEvent("Просмотр галереи", "Главная страница", g[currentId].galleryname, 1);
        } catch (err) {
        }

        $.get('/Home/UpdateNewsCounter/' + currentId);

        return false;
    });

    $(".rolik").click(function() {

        videolinkclick(v, null);

        try {
            pageTracker._trackEvent("Просмотр видео", "Страница новости", v.videoname, 1);
        } catch (err) {
        }

        $.get('/Home/UpdateNewsCounter/' + v.id);

        return false;
    });

    $(".photo").click(function() {

        gallerylinkclick(g);

        try {
            pageTracker._trackEvent("Просмотр галереи", "Страница новости", g.galleryname, 1);
        } catch (err) {
        }

        $.get('/Home/UpdateNewsCounter/' + g.id);

        return false;
    });

    $(".pricelst").click(function() {

        videolinkclick(v, null);

        try {
            pageTracker._trackEvent("Просмотр видео", "Прайс", v.videoname, 1);
        } catch (err) {
        }

        return false;
    });

    $(".maplink").click(function() {
        var map_type = "";
        var id = $(this).attr("id");
        if (id.substring(0, 3) == "wh_") {
            var map_type = "wh_";
            id = id.substring(3);
        }

        maplinkclick(map_type, id);

        try {
            pageTracker._trackEvent("Просмотр карты", "Страница контакты", (map_type == "wh_") ? val.Description + " (склад)" : val.Description, 1);
        } catch (err) {
        }

        return false;
    });

    $(".closebutton").click(function() {
        popupclose();
        return false;
    });

    $(document).click(function() {

        e = window.event;
        var elemClickSender = e.target || e.srcElement;      
        winpages = $("#modalpage");
        for (i = 0; i < winpages.length; i++) {
            if (winpages[i].contains(elemClickSender)) {
                return;
            }
        }

        popupclose();

    });
});

function videolinkclick(v_id, id) {

    var ext = "flv";
    if (v_id.videofile != null) {
        var val = v_id.videofile.split(".");
        var ext = val[val.length - 1];
    }
    
    if (id != null) {
        $("#alt_text_noflash").html("Для перехода к тексту новости нажмите <a href='/Home/News/" + id + "'>ссылку</a>");
        $("#alt_text_noflash").show();
    }

    $("#player_main").show();
    $("#gallery").hide();
    $("#popupMessage").hide();
    $(".stripTransmitter").hide();

    document.getElementById('popupwindow_main').style.display = 'block';

    if (ext == "swf") {
        $("#player_main").flash({
            swf: "/Content/news/videos/" + v_id.videofile,
            width: 544,
            height: 455
        });
        $("#show_sub_text").show();
    } else {
        $("#show_sub_text").show();
        $("#show_sub_text").hide();
        UFO_param = {
            movie: "/flvplayer.swf",
            width: v_id.videowidth + "&",
            height: v_id.videoheight + "&",
            majorversion: "7",
            build: "0",
            bgcolor: "#FFFFFF",
            allowfullscreen: "true",
            flashvars: "file=" + "/Content/news/videos/" + v_id.videofile + "&image=/Content/news/video/price.jpg&autostart=true"
        };
        UFO.create(UFO_param, 'player_main');
    }

    popupload();

}

function gallerylinkclick(g_id) {

    $("#svw").attr("innerHTML", g_id.galleryInner);
    
    $("#player_main").hide();
    $("#gallery").show();
    $(".stripTransmitter").show();
        
    document.getElementById('popupwindow_main').style.display = 'block';
    if (!gallery_sh) {
        $("div#gallery").slideView({ toolTip: true });
    }
    gallery_sh = true;

    popupload();
    
}

function maplinkclick(map_type, counter) {

    val = v[map_type + counter];

    document.getElementById("popupwindow_main").style.display = 'block';

    $("#googlemap1").show();
    $("#googlemap2").show();
    $("#switch_view").attr("innerHTML", "Переключить на статическую карту проезда");

    if (val.Longitude == 0.0) {
        $("#googlemap1").hide();
    }
    else {
        $("#googlemap2").hide();
    }

    if (val.MapFile.length < 1) {
        $("#googlemap2").hide();
    }

    if (val.MapFile.length > 1 && !val.Longitude == 0.0) {
        $("#switch_view").attr("style", "display: visible;");
    } else {
        $("#switch_view").attr("style", "display: none;");
    }

    $("#popup_description").attr("innerHTML", val.Description);
    $("#popup_address").attr("innerHTML", val.Address);

    if (val.Phone.length > 1) {
        $("#popup_phone").attr("innerHTML", val.Phone);
        $("#popup_phone_container").attr("style", "display: visible;");
    } else {
        $("#popup_phone_container").attr("style", "display: none;");
    }

    if (val.Fax.length > 1) {
        $("#popup_fax").attr("innerHTML", val.Fax);
        $("#popup_fax_container").attr("style", "display: visible;");
    } else {
        $("#popup_fax_container").attr("style", "display: none;");
    }

    $("#jpgmap").attr("src", "/Content/" + val.MapFile);
    var map_ = document.getElementById("map");
    if (val.MapProvider == "1") {

        var map = new GMap2(map_);

        map.setCenter(new GLatLng(val.Latitude, val.Longitude), 14);
        map.setUIToDefault();
        map.setMapType(G_NORMAL_MAP);

        var point = new GLatLng(val.Latitude, val.Longitude);
        var GMarker_ = new GMarker(point);

        GEvent.addListener(GMarker_, "click", function() { GMarker_.openInfoWindowHtml("<br>" + val.Address); });

        map.addOverlay(GMarker_);
        map.checkResize();

    } else {

        var map = new YMaps.Map(map_);

        map.setCenter(new YMaps.GeoPoint(val.Longitude, val.Latitude), 14);
        map.enableScrollZoom();
        map.addControl(new YMaps.TypeControl());
        map.addControl(new YMaps.Zoom());
        map.addControl(new YMaps.ScaleLine());

        var point = new YMaps.GeoPoint(val.Longitude, val.Latitude);
        var placemark = new YMaps.Placemark(point)
        placemark.description = val.Address;
        map.addOverlay(placemark);

    }

    popupload();

}

function popupmessage(Subject, Text) {
    
    $("#popupMessage").attr("innerHTML", "<div class='hcontent'><h1>" + Subject + "</h1><p>" + Text + "</p></div>");
    $("#player_main").hide();
    $("#popupMessage").show();

    document.getElementById('popupwindow_main').style.display = 'block';

    popupload();
    
}

function popuposition() {
  cW=document.documentElement.clientWidth;
  cH=document.documentElement.clientHeight;

  if(ppwin&&ppage){
    if (ppwin.clientWidth>cW){
      ppwin.style.left="0px";
      ppage.style.width=cW-96+"px";
    }else{
      ppage.style.width = "auto";
      setTimeout("ppwin.style.left=(cW-ppwin.clientWidth)/2+'px'",100);
    };

    if ((ippage+96)>cH){
      ppwin.style.top="0px";
      if(ppdop){
        ppage.style.height=cH-96-ppdop.offsetHeight+"px";
      }else{
        ppage.style.height=cH-96+"px";
      };
    }else{
      ppage.style.height = "auto";
      setTimeout("ppwin.style.top=(cH-ppwin.clientHeight)/2+'px'", 100);
      //ppwin.style.top=(cH-ppwin.clientHeight)/2+"px";
    };

  };
};

function popupload() {
    ppwin=document.getElementById('modalwindow');
    ppage=document.getElementById('modalpage'); if (ppage){ippage=ppage.offsetHeight;};
    ppdop=document.getElementById('modalpagedop'); if (ppdop){ippage=ippage+ppdop.offsetHeight;alert(ippage);};
    popuposition();
}

function popupclose() {
    $("#sub_text").hide();
    $("#alt_text").hide();
    $("#player_main").show();
    $("#show_sub_text").show();

    $("#alt_text").html('');
    var popupwindow_main = document.getElementById('popupwindow_main');
    if (popupwindow_main != null) {
        popupwindow_main.style.display = 'none';
    }
    var gallery_popup = document.getElementById('gallery_popup');
    if (gallery_popup != null) {
        gallery_popup.style.display = 'none';
    }

    $('#player_main object').remove();
}

window.onresize = function() { popuposition(); };

gallery_sh = false;
ppwin = document.getElementById('modalwindow');
ppage = document.getElementById('modalpage'); if (ppage) { ippage = ppage.offsetHeight; };
ppdop = document.getElementById('modalpagedop'); if (ppdop) { ippage = ippage + ppdop.offsetHeight; alert(ippage); };