var bBlockOpenPhotoWindow = false;
var PhotoWindowCreated = false;
var top = 0;
// смещение относительно видимой клиентской области
var off_top = 15;

var html_ajax_loader = '<div class="ajax-loader"><div style="text-align:center"><img src="/images/ajax-loader.gif" /><br />пожалуйста, подождите...</div></div>';

var portFolioObj = null;

function ShowPhoto(strUrl, width, height, obj) {
	// Checking if we can open this window

	if (bBlockOpenPhotoWindow) {
		return;
	}
	top = (document.documentElement.scrollTop == undefined) ? window.scrollY : document.documentElement.scrollTop;

	bBlockOpenPhotoWindow = true;
	
	if ($('popup_viewphoto')) {
		$('popup_viewphoto').innerHTML = html_ajax_loader;
		
		var pokryvalo = $('uber_alles');
		if (pokryvalo) {
			pokryvalo.style.display = 'block';
			pokryvalo.style.height = document.getElementById('wrapper').offsetHeight + 'px';
		}

		var img_el = obj.getElementsByTagName("IMG");
		var aResult = '';
		if (img_el.length) {
		      aResult = /.+([\d]{6})[s_\.].+/.exec(img_el[0].src);
		} else {
		    aResult = /.+([\d]{6})?from.+/.exec(strUrl);
		}
		ajax_updater = new Ajax.Request(strUrl + '&by_ajax=1&max_height=' + (document.documentElement.clientHeight - 153) + '&img_cur=' + aResult[1], {
			method: 'get',
			onSuccess: function(transport) {
				var response = transport.responseText || '';
				onGetContent(response, obj, aResult[1]);
			}
		});
	}
}
/*
    flag = 1 - показать все slect`ы на странице
    flag = 0 - скрыть
    позволяет избежать их отображения в ie6
*/
function ShowHideSelect (flag) {
    var nodes = document.getElementsByTagName("SELECT");
    var vis = (flag) ? 'visible' : 'hidden';
    if (nodes != null && nodes.length) {
        for (var i=0; i<nodes.length; i++) {
            nodes[i].style.visibility = vis;
        }
    }
}

/*
    flag = 1 - Показать панель
    falg = 0 - скрыть
    */
function PrepareStylesForSeePanel (flag) {

    var vis = (flag) ? "hidden" : "auto";
    document.body.scroll = (flag) ? "no" : "yes";
    //top = top + off_top;
    window.scroll(0, (flag) ? 0 : top);
    if (ie) {
        document.getElementsByTagName("HTML")[0].style.overflow = vis;
        if (!flag) {
            document.getElementsByTagName("HTML")[0].style.overflowX = "hidden";
        }
        ShowHideSelect(flag);
    } else {
        document.body.style.overflow = vis;
        document.body.scrollTop = (flag) ? 0 : top;
    }

}

function onGetContent (strResponse, obj, ids) {
    $('popup_viewphoto').innerHTML = strResponse.substr(1245, strResponse.length - 1269);
    //$('popup_viewphoto').innerHTML = strResponse.substr(1239, strResponse.length - 1269);
   
    if ($('img_height')) {
	v_h = $('img_height').value;
    }
    else if($('img_height_' + ids)) {
	v_h =  $('img_height_' + ids).value;
    }
   
    var height = parseInt(v_h) + 30;
    var height_of_panel =  height;
    if (ie) {
        document.getElementById('nav_column_id').style.height = height + 'px';
    }
 
    height = height + 40;
    if (ie) {
        height = height + 30;
    }
    document.getElementById('popup_viewphoto').style.height = height + 'px';
    
    
    off_top = (document.documentElement.clientHeight < height_of_panel + 150) ? 15 : Math.ceil((document.documentElement.clientHeight - height_of_panel - 120) / 2);
    off_top = off_top - 25;
    
  
    $('popup_box').style.top = off_top + 'px';
    if (!ie) {
        if ( $('nav_col_block')) 
        	$('nav_col_block').style.margin = '20px 0 0 0px';
        else if ( $('nav_col_block_' + ids)) {
        	$('nav_col_block_' + ids).style.margin = '20px 0 0 0px';
        }
    }
    $('popup_viewphoto').style.display = 'block';
	if ($('popup_box')) {
		$('popup_box').style.display = 'block';
	}

    PrepareStylesForSeePanel(1);
    try {
    	portFolioObj = new OnLoadPortfolio(ids);
    }
    catch (e) {
    	
    }
    
}

function closePhotoWindow () {
	if ($('popup_viewphoto')) {
		$('popup_viewphoto').innerHTML = '';
	    $('popup_viewphoto').style.display = 'none';
	}
	if ($('popup_box')) {
	    $('popup_box').style.display = 'none';
	}
    PrepareStylesForSeePanel(0);

    var pokryvalo = $('uber_alles');

	if (pokryvalo) {
		pokryvalo = document.getElementById('uber_alles');
		pokryvalo.style.display = 'none';
	}
	top = 1000;

	bBlockOpenPhotoWindow = false;
}
