// количество рисунков
var count = 0;
// номер первого рисунка
var first_img = 1;
var current_num = 1;
// количество отображаемых ссылок справа и слева от активной
var amount_img = 8;

function AutoLoadWindow () {
	var el = document.getElementById('load_window_id');
	if (!el || !el.value.length) {
		return;
	}
	LoadWindowOfObject(el.value);
}


function LoadWindowOfObject (strUrl) {
	if (!strUrl.length) {
		return;
	}
	top = 0;
	strUrl = strUrl + ((strUrl.indexOf('?') > 0) ? '&' : '?') + 'by_ajax=1';
	var ar_id = document.getElementById('array_of_id');
	if (ar_id != null) {
		strUrl = strUrl + '&ar_id='	+ ar_id.value;
	}

	if (first_load) {
		clientWidth = document.documentElement.clientWidth;
		first_load = 0;
	}

	var el = document.getElementById('popup_viewphoto');
	if (el) {
		el.style.display = 'none';
	}
    el = document.getElementById('pokryvalo');
    
    if (el) {
        el.style.display = "block";
        var fixx1 = document.getElementById('pokryvalo');
        var fixx2 = document.getElementById('obertka').offsetHeight + 100 + 'px';
        if (window.opera) {
            fixx1.style.height = fixx2;
        }
    }
	OnSeeContent();
    prepareScroll(1);
	SetVisibleLoadingProgress();

	ajax_updater = new Ajax.Request(strUrl, {
		method: 'get',
		onSuccess: function(transport) {
			var response = transport.responseText || '';
			onGetContentObject(response);
		}
	});
}

function OnSeeContent () {
	opacity = opacity + interval_opacity;

	if (opacity > max_opacity) {
		opacity = max_opacity;
	}
	var opacity_ie = opacity * 100;
	if (ie) {
		document.getElementById('pokryvalo').style.filter = 'alpha(opacity=' + opacity_ie + ')';
	} else {
		document.getElementById('pokryvalo').style.opacity = opacity;
	}

	if (opacity < max_opacity) {
		setTimeout(OnSeeContent, 500 / (max_opacity / interval_opacity));
	}
	prepareNavObjects();
}

function prepareNavObjects () {
	var nav = document.getElementById('object_select');
	if (nav == null) {
		return;
	}
	nav.style.display = 'inline';
}

function OnCloseContent () {
	opacity = opacity - interval_opacity;
	if (opacity < 0) {
		opacity = 0;
	}
	var opacity_ie = opacity * 100;
	if (ie) {
		document.getElementById('pokryvalo').style.filter = 'alpha(opacity=' + opacity_ie + ')';
	} else {
		document.getElementById('pokryvalo').style.opacity = opacity;
	}

	if (opacity > 0) {
		setTimeout(OnCloseContent, 500 / (max_opacity / interval_opacity));
	} else {
		document.body.style.width = clientWidth + 'px';
		document.getElementById('pokryvalo').style.display = "none";
		prepareScroll(0);
	}
	document.getElementById('popup_viewphoto').innerHTML = '';
}

function onGetContentObject (strResponse) {
	var loader = document.getElementById('popup_loader');
	loader.style.display = 'none';
	if (!strResponse.length) {
        document.getElementById('pokryvalo').style.display = "none";
		return;
    }
    document.getElementById('popup_viewphoto').style.visibility = 'hidden';
    document.getElementById('popup_viewphoto').innerHTML = strResponse;
    document.getElementById('popup_viewphoto').style.display = 'block';

	var link = document.getElementById('link_for_applet');
	var new_link = document.getElementById('link_for_applet_ajax');
	if (link != null && link.href.length) {
		link.href = new_link.value;
	}

    // подготовка стилей
    prepareNavigationPanel();
    document.getElementById('closer').style.visibility = "visible";
		if (window.opera || /webkit/i.test(navigator.userAgent)) {
			document.getElementById('obertka').style.height = 'auto';// document.getElementsByTagName('BODY')[0].offsetHeight + 'px';
			//document.getElementById('obertka').style.overflow = 'hidden';
			document.getElementById('dno').style.display = 'none';
			//document.getElementsByTagName('BODY')[0].style.overflow = 'hidden';
			//alert(fixx);
		}
    var popup = document.getElementById('popup_viewphoto');
    popup.style.visibility = 'visible';
}

function OnCloseWindowOfObject () {
	document.getElementById('popup_viewphoto').style.display = "none";
	OnCloseContent();
	if (window.opera || /webkit/i.test(navigator.userAgent)) {
			//document.getElementById('obertka').style.height = 'auto';
			//document.getElementById('obertka').style.overflow = 'auto';
			document.getElementById('dno').style.display = 'block';
			document.getElementsByTagName('BODY')[0].style.overflow = 'auto';
			//alert('');
	}
}
// подготовка скроллинга
// flag = 0 - при скрытии окна
// falg = 1 - при показе
function prepareScroll (flag) {
    var scroll_to = (flag) ? 0 : top;
    if (!top) {
        top = document.documentElement.scrollTop;
    }
    if (ie) {
	   ShowHideSelect(flag);
	   document.body.scroll = (flag) ? "no" : "yes";
	   document.getElementById('pokryvalo').style.top = top + 'px';
	   document.getElementById('popup_viewphoto').style.top = top + 'px';
	} else {
	    document.body.scrollTop = scroll_to + 'px';
	    window.scroll(0, scroll_to);
	}
	if (!window.opera) {
  	document.getElementsByTagName("HTML")[0].style.overflow = (flag) ? 'hidden' : 'auto';
  }
}
// подготовка данных и стилей панели навигации
function prepareNavigationPanel () {
	first_img = 1;
	if (document.getElementById('l_shadow') == null) {
		return;
	}
	document.getElementById('l_shadow').style.visibility = 'visible';
	// сбор данных из html кода
	count = parseInt(document.getElementById('count').value);
	if (!count) {
		return;
	}

	for (var i = 1; i <= count; i++) {
		if (i < 4) {
			document.getElementById('img_href_' + i).href = "JavaScript:void(0);";
		} else {
			document.getElementById('item_' + i).style.display = "none";
		}
	}
	if (count > 3) {
		document.getElementById('but_next').className = "act inn_sign";
		document.getElementById('but_next_span').style.visibility = "visible";
		document.getElementById('but_prev_span').style.visibility = "visible";
	}

	var nav = document.getElementById('img_nav');
	if (nav == null) {
		return;
	}
	var el_a = nav.getElementsByTagName('A');
	for (var i = 0; i < el_a.length; i++) {
		el_a[i].href = 'JavaScript:void(0);';
	}
}
// выбор нового рисунка
function selectImg (num, flag) {
	var id = (flag) ? first_img - 1 + num : num;
	current_num = id;
	var link = document.getElementById('img_' + id).value;
	var code = document.getElementById('img_medium_' + id).value;
	if (link != "#") {
		code = '<a href="' + link + '" target="_blank">' + code + '</a>';
	}
	document.getElementById('medium_img').innerHTML = code;
	document.getElementById('desc').innerHTML = document.getElementById('img_dsc_' + id).value;

	if (!flag && count > 3) {
		first_img = id - 1;
		if (first_img > count - 2) {
			first_img = count - 2;
		} else if (first_img < 1) {
			first_img = 1;
		}
		document.getElementById('but_prev').className = (first_img < 2) ? "inn_sign" : "act inn_sign";
		document.getElementById('but_next').className = (first_img == count - 2) ? "inn_sign" : "act inn_sign";
		fillNewImage();
	}
	if (count > 3) {
		reloadNavPanel();
	}
}
// замена панели навигации по рисункам
function reloadNavPanel () {
	var first = current_num - amount_img;
    if (first < 1) {
    	first = 1;
    }
    var last = current_num + amount_img;
    if (last > count) {
        last = count;
    }

    var text = '';
    if (current_num > 2) {
        text = '<a href="JavaScript:void(0);" onclick="selectImg(1, 0); return false;" class="sign"><img src="/images/blank.gif" alt="" class="first"></a>';
    } else {
    	text = '<span class="sign"><img src="/images/blank.gif" alt="" class="first"></span>';
    }
    if (current_num > 1) {
    	text = text + '<a href="JavaScript:void(0);" onclick="selectImg(' + (current_num - 1) + ', 0); return false;" class="sign"><img src="/images/blank.gif" alt="" class="previous"></a>';
    } else {
		text = text + '<span class="sign"><img src="/images/blank.gif" alt="" class="previous"></span>';
    }

    for (var i = first; i <= last; i++) {
    	if (i == current_num) {
    		text = text + '<span class="curr_page">' + i + '</span>';
    	} else {
    		text = text + '<a href="JavaScript:void(0);" onclick="selectImg(' + i + ', 0); return false;">' + i + '</a>';
    	}
    }

    if (current_num < count) {
    	text = text + '<a href="JavaScript:void(0);" onclick="selectImg(' + (current_num + 1) + ', 0); return false;" class="sign"><img src="/images/blank.gif" alt="" class="next"></a>';
    } else {
    	text = text + '<span class="sign"><img src="/images/blank.gif" alt="" class="next"></span>';
    }
    if (current_num < count - 1) {
		text = text + '<a href="JavaScript:void(0);" onclick="selectImg(' + count + ', 0); return false;" class="sign"><img src="/images/blank.gif" alt="" class="last"></a>';
    } else {
    	text = text + '<span class="sign"><img src="/images/blank.gif" alt="" class="last"></span>';
    }

    document.getElementById('img_nav').innerHTML = text;

}
// замена рисунков при переключении навигации
function fillNewImage () {
	var el;
	var i2 = 1;
	for (var i = first_img; i <= first_img + 2; i++) {
		el = document.getElementById('img_href_' + i2);
		i2 = i2 + 1;
		if (el == null) {
			continue;
		}
		el.style.background = "url('" + document.getElementById('img_small_' + i).value + "') 50% no-repeat";
	}
}
// следующий рисунок
function nextImg () {
	first_img = first_img + 1;
	if (first_img > count - 2) {
		first_img = count - 2;
		return;
	}
	if (first_img == count - 2) {
		document.getElementById('but_next').className = "inn_sign";
	}
	document.getElementById('but_prev').className = "act inn_sign";
	fillNewImage();
}
// предыдущий рисунок
function prevImg () {
	first_img = first_img - 1;
	if (!first_img) {
		first_img = 1;
		return;
	}
	if (first_img == 1) {
		document.getElementById('but_prev').className = "inn_sign";
	}
	document.getElementById('but_next').className = "act inn_sign";
	fillNewImage();
}
