fc = function() {};

fc.timeout = 200;
fc.timeoutStart = 1000;
fc.currentarea = 0;
fc.interval = 15;
fc.firstRun = true;
fc.closetimer1 = new Array();
fc.closetimer2 = new Array();
fc.showtiker = new Array();
fc.apics = new Array();
fc.d = document;
fc.b = document.body;

function detectIE6(){
  var browser = navigator.appName;
  if (browser == "Microsoft Internet Explorer"){
    var b_version = navigator.appVersion;
    var re = /\MSIE\s+(\d\.\d\b)/;
    var res = b_version.match(re);
    if (res[1] <= 6){
      return true;
    }
  }
  return false;
}

fc.getPos = function(el) {
    if (typeof window[jQuery(el).offset] == 'function') {
        offset = jQuery(el).offset();
        return {
            x: offset.left,
            y: offset.top
        };
    }
    else {
        if (!detectIE6()) return fc.getPos2(el);
    }
}

fc.getPos2 = function(el, scrollOff) {
    var SL = 0, ST = 0;
    if (!scrollOff) {
        var is_div = /^div$/i.test(el.tagName);
        if (is_div && el.scrollLeft)
            SL = el.scrollLeft;
        if (is_div && el.scrollTop)
            ST = el.scrollTop;
    }
    var r = { x: el.offsetLeft - SL, y: el.offsetTop - ST };
    if (el.offsetParent) {
        var tmp = fc.getPos(el.offsetParent);
        r.x += tmp.x;
        r.y += tmp.y;
    }
    return r;
}

fc.getCSS = function getCSS() {
var cssTpl = '<style><!-- \
.fotocontext {\
z-index:999;\
position:absolute;\
top:0px;\
left:0px;\
width:298px;\
color:#fff;\
line-height:normal;\
text-align:left;\
background:url("http://fotocontext.ru/img/bg2_1.gif") 0 0 no-repeat;\
filter:progid:DXImageTransform.Microsoft.Alpha(opacity=85);\
-moz-opacity:0.95;\
-khtml-opacity:0.95;\
opacity:0.95;\
margin:0;\
padding:7px 0 0 0;\
}\
.subfotocontext {\
background:url("http://fotocontext.ru/img/bg2_2.gif") 0 0 no-repeat;\
height:7px;\
width:298px;\
margin:0;\
padding:0;\
}\
.fc_container {\
background:url("http://fotocontext.ru/img/bg2_3.gif") 0 0 repeat-y;\
opacity:0.8;\
width:298px;\
text-align:left;\
margin:0;\
padding: 5px 20px;\
}\
.fc_ico {\
display:inline;\
}\
.fc_logo {\
display:block;\
float:left;\
width:85px;\
padding:6px 5px 5px 0;\
}\
.fc_logo img{\
border:none;\
}\
.fc_title {\
display:inline;\
color:#000;\
font-weight:bold;\
font-family:tahoma;\
font-size:14px;\
margin:0;\
padding:0;\
text-decoration:underline;\
}\
.fc_text, .fc_text a, .fc_text a:visited {\
color:#000;\
font-family:tahoma;\
font-size:12px;\
margin:0;\
padding:0;\
width:260px;\
}\
.fc_ico {\
z-index:998;\
position:absolute;\
top:0px;\
left:0px;\
width:35px;\
height:44px;\
background:url("http://fotocontext.ru/img/fc1.png") 0 0 no-repeat;\
}\
* html .fc_ico {\
background:url("http://fotocontext.ru/img/fc1.gif") 0 0 no-repeat;\
}\
//--></style>';
return cssTpl;
}

fc.getarea = function() {
var areatpl = '<div class="fotocontext" id="fotocontext" style="display:none;"></div>\
<div class="subfotocontext" id="subfotocontext" style="display:none;"></div>\
<div id="fc_cont" class="fc_container" style="display:none;"></div>\
<div id="fc_subcont" class="fc_subcontainer" style="display:none;"></div>\
<div id="fc_title" class="fc_title" style="display:none;"></div>\
<div id="fc_text" class="fc_text" style="display:none;"></div>\
<div id="fc_logo" class="fc_logo" style="display:none;"></div>\
<div id="fc_ico" class="fc_ico" style="display:none;"></div>';
return areatpl;
}

function fcRun() {
    if (fc.firstRun) {
        var IE='\v'=='v';
        if (!IE) {
            s = fc.d.createElement("SCRIPT");
            fc.sendShow = function() {
                s.type = "text/javascript";
                if (s.setAttribute) { s.setAttribute("src", fc.showUrl); } else { s.src = fc.showUrl; }
                fc.b.insertBefore(s, fc.b.lastChild);
            };
        } else {
            s = fc.d.createElement("SPAN");
            s.style.display = "none";
            s.setAttribute("id", 'showUrl');
            fc.b.insertBefore(s,fc.b.lastChild);
            fc.sendShow = function() {
                s = fc.d.getElementById('showUrl');
                s.innerHTML = "Workaround for IE.<s" + "cript></" + "script>";
                setTimeout(function() { s = s.getElementsByTagName("SCRIPT")[0]; if (s.setAttribute) { s.setAttribute("type", '"text/javascript"'); } else { s.type = '"text/javascript"'; } if (s.setAttribute) { s.setAttribute("src", fc.showUrl); } else { s.src = fc.showUrl; } },10);
            };
        }

        var pics = fc.d.getElementsByTagName('img');
        var npics = pics.length;
        var j=0;
        for (var i=0; i<npics; i++) {
            var tmp = pics[i]; 
            if (key = fc.in_array(tmp.src, getPics)) {
                fc.showtiker[key] = new Array();
                fc.closetimer1[key] = new Array();
                tmp.onmouseover = new Function('fc.showIcons('+key+')');
                tmp.onmouseout = new Function('fc.hideIcons('+key+')');

                tmpid = tmp.getAttribute('id');
                if (tmpid === null || tmpid == '') {
                    tmp.setAttribute('id', 'fc_image'+j);
                    fc.apics[key] = 'fc_image'+j;                                
                } else
                    fc.apics[key] = tmpid;

                for (key2 in getPics[key].zones) {
                    getPics[key].zones[key2].y = +getPics[key].zones[key2].y;
                    getPics[key].zones[key2].x = +getPics[key].zones[key2].x;
                    if (typeof getPics[key].zones[key2].ad != 'undefined') {
                        node = fc.d.getElementById('fotocontext').cloneNode(true);
                        node.setAttribute('id', 'area' + key + '_' + key2);
                        node.onmouseover = new Function('fc.openedarea('+key+','+key2+')');
                        node.onmouseout = new Function('fc.closearea('+key+','+key2+')');
                        fc.b.appendChild(node);
                        node = fc.d.getElementById('fc_cont').cloneNode(true);
                        node.setAttribute('id', 'fc_cont' + key + '_' + key2);
                        node.style.display = 'block';
                        fc.d.getElementById('area' + key + '_' + key2).appendChild(node);
                        node = fc.d.getElementById('subfotocontext').cloneNode(true);
                        node.setAttribute('id', 'subfotocontext' + key + '_' + key2);
                        node.style.display = 'block';
                        fc.d.getElementById('area' + key + '_' + key2).appendChild(node);
                        if (getPics[key].zones[key2].ad['0'].flv === null && (getPics[key].zones[key2].ad['0'].swf === null)) {
                            node = fc.d.getElementById('fc_title').cloneNode(true);
                            node.setAttribute('id', 'fc_title' + key + '_' + key2);
                            node.style.display = 'block';
                            node.innerHTML = '<a href="http://fotocontext.ru/count/' + getPics[key].zones[key2].ad['0'].hash + '?id=' + getPics[key].zones[key2].id + '" target="_blank">' + getPics[key].zones[key2].ad['0'].title + '</a>';
                            fc.d.getElementById('fc_cont' + key + '_' + key2).appendChild(node);
                            if (getPics[key].zones[key2].ad['0'].pic != null) {
                                node = fc.d.getElementById('fc_logo').cloneNode(true);
                                node.setAttribute('id', 'fc_logo' + key + '_' + key2);
                                node.style.display = 'block';
                                node.innerHTML = '<a href="http://fotocontext.ru/count/' + getPics[key].zones[key2].ad['0'].hash + '?id=' + getPics[key].zones[key2].id + '" target="_blank"><img src="http://fotocontext.ru/upload/' + getPics[key].zones[key2].ad['0'].pic + '" alt="" width="80" /></a>';
                                fc.d.getElementById('fc_cont' + key + '_' + key2).appendChild(node);
                            }
                            node = fc.d.getElementById('fc_text').cloneNode(true);
                            node.setAttribute('id', 'fc_text' + key + '_' + key2);
                            node.style.display = 'block';
                            if (getPics[key].zones[key2].ad['0'].pic != null && IE) node.style.width = '175px';
                            node.innerHTML = getPics[key].zones[key2].ad[0].desc1 + ' ' + getPics[key].zones[key2].ad[0].desc2 + '<br />' + getPics[key].zones[key2].ad[0].url2;
                            fc.d.getElementById('fc_cont' + key + '_' + key2).appendChild(node);                                
                        }
                        node = fc.d.getElementById('fc_ico').cloneNode(true);
                        node.onmouseover = new Function('fc.openarea('+key+','+key2+');fc.showIcons('+key+')');
                        node.onmouseout = new Function('fc.closearea('+key+','+key2+');fc.hideIcons('+key+')');
                        node.setAttribute('id', 'fc_ico'+key+'_'+key2);
                        fc.b.appendChild(node);
                        fc.showtiker[key][key2] = 0;
                        fc.closetimer1[key][key2] = null;
                    }
                }
            if (fc.isHidden(tmp)) fc.showIconsStart(key);
            j++;
            }
        }
    }
    fc.firstRun = false;
}

fc.in_array = function (needle, haystack, strict) {
    var found = false, key, strict = !!strict;
    for (key in haystack) {
        if (typeof haystack[key]['filename'] != 'undefined') {
            nl=needle.length; hl=haystack[key]['filename'].length;
            if (nl>hl) needle2 = needle.substr(nl - hl); else needle2 = needle;
        } else needle2=needle;
        if ((strict && haystack[key]['filename'] === needle2) || (!strict && haystack[key]['filename'] == needle2)) {
            found = key;
            break;
        }
    }
    return found;
}

fc.mclose = function (index1, index2) {
    if (typeof index1 != 'undefined' || typeof index2 != 'undefined') {
        if (getPics[index1].zones[index2].ad['0'].swf != null) 
            swfobject.removeSWF('fc_subcont' + index1 + '_' + index2);
        if (getPics[index1].zones[index2].ad['0'].flv != null) 
            swfobject.removeSWF('fc_subcont' + index1 + '_' + index2);
        fc.d.getElementById('area' + index1 + '_' + index2).style.display = 'none';
    }
	if (fc.closetimer1[index1][index2])	{
		window.clearTimeout(fc.closetimer1[index1][index2]);
		fc.closetimer1[index1][index2] = null;
	}
}

fc.openarea = function (index1, index2) {
    if (typeof index1 != 'undefined' || typeof index2 != 'undefined') {
        if (fc.closetimer1[index1][index2]) {
            window.clearTimeout(fc.closetimer1[index1][index2]);
            fc.closetimer1[index1][index2] = null;
        } else {
            fc.iclose(index1);
            if (obj1 = fc.d.getElementById('area' + index1 + '_' + index2)) {
                obj1.style.display = 'block';
                newPos = fc.getPos(fc.d.getElementById(fc.apics[index1]));
                obj1.style.top = (newPos.y + getPics[index1].zones[index2].y) + 'px';
                if ((newPos.x + getPics[index1].zones[index2].x + 310) > document.body.clientWidth) {
                    obj1.style.left = (document.body.clientWidth - 320) + 'px';
                } else {
                    obj1.style.left = (newPos.x + getPics[index1].zones[index2].x) + 'px';
                }
            }
            if (getPics[index1].zones[index2].ad['0'].fwidth === null) var fwidth = 260; else var fwidth = getPics[index1].zones[index2].ad['0'].fwidth;
            if (getPics[index1].zones[index2].ad['0'].fheight === null) var fheight = 100; else var fheight = getPics[index1].zones[index2].ad['0'].fheight;
            if (getPics[index1].zones[index2].ad['0'].flv != null) {
                newSC = fc.d.getElementById('fc_subcont').cloneNode(true);
                newSC.setAttribute('id', 'fc_subcont' + index1 + '_' + index2);
                newSC.style.display = 'block';
                fc.d.getElementById('fc_cont' + index1 + '_' + index2).appendChild(newSC);
			    var attrs = {wmode: "window", allowscriptaccess: "always"};
                var filePath = 'http://fotocontext.ru/upload/' + getPics[index1].zones[index2].ad['0'].flv;
                var linkPath = 'http://fotocontext.ru/count/' + getPics[index1].zones[index2].ad['0'].hash + '?id=' + getPics[index1].zones[index2].id;
				var flashvars = {file: filePath, screencolor: "DFEAF9", autostart: true, controlbar: 'none',displayclick: 'link', link: linkPath};
				swfobject.embedSWF("http://fotocontext.ru/player.swf", 'fc_subcont' + index1 + '_' + index2, fwidth, fheight, "9", "http://fotocontext.ru/js/expressInstall.swf", flashvars, null, attrs);
            }
            if (getPics[index1].zones[index2].ad['0'].swf != null) {
                newSC = fc.d.getElementById('fc_subcont').cloneNode(true);
                newSC.setAttribute('id', 'fc_subcont' + index1 + '_' + index2);
                newSC.style.display = 'block';
                fc.d.getElementById('area' + index1 + '_' + index2).style.background = 'none';
                fc.d.getElementById('subfotocontext' + index1 + '_' + index2).style.background = 'none';
                fc.d.getElementById('fc_cont' + index1 + '_' + index2).style.background = 'none';
                fc.d.getElementById('fc_cont' + index1 + '_' + index2).appendChild(newSC);
//			    var attrs = {wmode: "transparent"};
                var linkPath = 'http://fotocontext.ru/count/' + getPics[index1].zones[index2].ad['0'].hash + '?id=' + getPics[index1].zones[index2].id;
				var flashvars = {clickTag: linkPath};
                var filePath = 'http://fotocontext.ru/upload/'+getPics[index1].zones[index2].ad['0'].swf;
				swfobject.embedSWF(filePath, 'fc_subcont' + index1 + '_' + index2, fwidth, fheight, "9", "http://fotocontext.ru/js/expressInstall.swf", flashvars, null, attrs);                                
            }
            if (fc.showtiker[index1][index2]==0) {
                fc.showUrl = 'http://fotocontext.ru/context/show/?';
                var params = { 'lmt': Date.parse(fc.d.lastModified) / 1000, 'hash': getPics[index1].zones[index2].ad['0'].hash, 'id': getPics[index1].zones[index2].id }
                fc.getShowURL(params);
                fc.sendShow();
            }
            fc.showtiker[index1][index2]++;
        }
    }
}

fc.openedarea = function (index1, index2) {
    if (fc.closetimer1[index1][index2]) {
		window.clearTimeout(fc.closetimer1[index1][index2]);
		fc.closetimer1[index1][index2] = null;
    } else {
        if (obj = fc.d.getElementById('area' + index1 + '_' + index2))
            obj.style.display = 'block';
    }
}

fc.closearea = function(index1, index2) {
    if (typeof index1 != 'undefined' || typeof index2 != 'undefined') {
        if (!fc.closetimer1[index1][index2])
            fc.closetimer1[index1][index2] = window.setTimeout('fc.mclose('+index1+','+index2+')', fc.timeout);
    }
}

fc.showIconsStart = function(index1) {
    newPos = fc.getPos(fc.d.getElementById(fc.apics[index1]));
    if (newPos.y!=0 && newPos.x!=0)
        for (key in fc.showtiker[index1]) 
            if (obj = fc.d.getElementById('fc_ico' + index1 + '_' + key)) {
                obj.style.top = newPos.y + getPics[index1].zones[key].y + 'px';
                obj.style.left = newPos.x + getPics[index1].zones[key].x + 'px';
                obj.style.display = 'block';
            }
    fc.closetimer2[index1] = window.setTimeout('fc.iclose('+index1+')', fc.timeoutStart);
}

fc.showIcons = function(index1) {
    if (fc.closetimer2[index1]) {
        window.clearTimeout(fc.closetimer2[index1]);
        fc.closetimer2[index1] = null;
    } else {        
        newPos = fc.getPos(fc.d.getElementById(fc.apics[index1]));
        for (key in fc.showtiker[index1]) 
            if (obj = fc.d.getElementById('fc_ico' + index1 + '_' + key)) {
                obj.style.top = newPos.y + getPics[index1].zones[key].y + 'px';
                obj.style.left = newPos.x + getPics[index1].zones[key].x + 'px';
                obj.style.display = 'block';
            }
    }
}

fc.isHidden = function(el) {
    var p=el;
    while (p && p != fc.b && p.style.display != 'none')
        p = p.parentNode;
    if (p != fc.b) { return false; } else { return true; }
}

fc.hideIcons = function(index1) {
    if (typeof index1 != 'undefined') {
        if (!fc.closetimer2[index1]) {
            fc.closetimer2[index1] = window.setTimeout('fc.iclose('+index1+')', fc.timeout);
        }
    }
}

fc.iclose = function (index1) {
    if (typeof index1 != 'undefined') {
        for (key in fc.showtiker[index1]) 
            if (obj = fc.d.getElementById('fc_ico' + index1 + '_' + key)) 
                obj.style.display = 'none';
    }
	if (fc.closetimer2[index1])	{
		window.clearTimeout(fc.closetimer2[index1]);
		fc.closetimer2[index1] = null;
	}
}

fc.getPicsURL = function(params) {
    for (var key in params) 
        if (params[key] && params.hasOwnProperty && params.hasOwnProperty(key)) 
            fc.picUrl += '&' + key + '=' + escape(params[key]);
}

fc.getShowURL = function(params) {
    for (var key in params) 
        if (params[key] && params.hasOwnProperty && params.hasOwnProperty(key)) 
            fc.showUrl += '&' + key + '=' + escape(params[key]);
}

fc.include = function(file) {
    fc.d.write('<scr'+'ipt type="text/javascript" src="' + file + '" charset="utf-8"></scr'+'ipt>');
}

fc.d.write(fc.getCSS());
fc.d.write(fc.getarea());

fc.picUrl = 'http://fotocontext.ru/context/?';
var params = { 'lmt': Date.parse(fc.d.lastModified) / 1000, 'ref1': fc.d.referrer, 'ref2': fc.d.location }

fc.getPicsURL(params);
if (typeof jQuery == 'undefined' ) 
    fc.include('http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js');

fc.include('http://fotocontext.ru/js/swfobject.js');
//alert(fc.picUrl);
fc.include(fc.picUrl);
