function writeTop()
{
    var so = new SWFObject("/i/tsum.swf", "mymovie", "708", "100", "7", "#ffffff");
    so.write("flash");
}
var so;
var current;
var setup = false;

function writeTitle(txt, w)
{
    var arr = txt.split("<br>");
    var h = (arr.length > 1) ? (arr.length) * 36 : 36;
    
    for (var i = 0; i < (arr.length - 1); i++) {
        txt = txt.replace("<br>", "\n");
    }
    
    so = new SWFObject("/title.swf", "mytitle", "660", "72", "8", "#808080");
    so.addParam("wmode", "transparent");
	so.addParam("menu", "false");
    so.addVariable("txt", encodeURIComponent(txt));
    so.write("title");
    document.getElementById('title').style.width = w + "px";
    document.getElementById('title').style.height = h + "px";
}

function openWin(url, id, w, h) {
   NewWin = window.open('', id, 'location=no,status=no,resizable=yes,toolbar=no,scrollbars=no,menubar=no,top=100,left=100,width='+ w +',height='+ h);
   NewWin.document.writeln('<html><head><title>Картинка</title></head><body style="margin:0px; padding:0px;"><a href="javascript:window.close()"><img src="'+url+'" border="0" width="'+w+'" height="'+h+'" alt="Закрыть окно"></a></body></html>');
   NewWin.document.close();
   NewWin.focus();
   return false;
}

function rollover(img, url) {
   var preload = new Image();
   preload.src = url;
   img.onmouseover = function () {
      var _src = this.src;
      this.src = url;
      this.onmouseout = function() {
          this.src = _src;
      }
   }
}

window.onload = function() {
    imgs = document.getElementsByTagName('img')
    for(i = 0; i < imgs.length; i++) {
        value = imgs[i].attributes.rollover ? imgs[i].attributes.rollover.value : imgs[i].rollover || 0;
        if (value) {
            rollover(imgs[i], value);
        }
    }
}

function handleChange(event) {
    if(!setup) {
        setup= true;
        return false;
    }
    
    var arr = (event.value).split("/");
    if (arr.length < 4) {
        arr.pop(); arr.shift();
        var exp = "#menu a[rel='" + (arr[0] == undefined ? "" : arr[0]) + "']";
        var link = $(exp)[0];
        $.ajax({
            type: "GET",
            url: link.href,
            cache: false,
            success: function(html){
                if (current.href != "undefined" && link.href == $(current)[0].href) {
                    $(current).addClass("inline");
                    $(current).removeClass("current");
                } else {
                    $(link).addClass("inline");
                    $(current).removeClass("inline");
                    $(current).removeClass("current");
                    current = $(link);
                }
                writeTitle("Наши Клиенты", 500);
                $("#body").html(html).find("a").click(function() {
                    var link = this;
                    $(link).blur();
                    SWFAddress.setValue(link.rel);
                    return false;
                });
            }
        });
        
    } else {
        arr.pop(); arr.shift();
        var exp = "#body a[rel='" + (arr.join("/")) + "']";
        var link = $(exp)[0];
        $.ajax({
            type: "GET",
            url: link.href,
            cache: false,
            success: function(html){
                var arr = html.split("|");
                writeTitle(arr[0], 500);
                $('#body').html($(arr[1]));
                $(current).removeClass("inline");
                $(current).addClass("current");
            }
        });
    }
}

function getWorkDetail(link) {
    SWFAddress.setValue(link.rel);
    $(link).blur();
    $.ajax({
        type: "GET",
        url: link.href,
        cache: false,
        success: function(html){
            var arr = html.split("|");
            writeTitle(arr[0], 500);
            $('#body').html($(arr[1]));
            $(current).removeClass("inline");
            $(current).addClass("current");
        }
    });
    return false;
}

$(document).ready(function() {
    $("#content").before("<div id='loading'>Идет загрузка...</div>");
    $('#loading').hide();
    
    $('#loading').ajaxStart(function() {
        $(this).show();
    }).ajaxStop(function() {
        $(this).hide();
    });
    
    current = $("#menu a.inline");
    
	$("#menu a").click(function() {
        var link = this;
        $(link).blur();

        if (current.href != "undefined" && link.href == $(current)[0].href && !current.hasClass("current")) {
            return false;
        }
    
        SWFAddress.setValue(link.rel);
		return false;
	});
    
    $("#body a").click(function() {
        var link = this;
        $(link).blur();
        SWFAddress.setValue(link.rel);
        return false;
    });
    
    SWFAddress.addEventListener(SWFAddressEvent.CHANGE, handleChange);
});
