var map = [];
var currentAnchor = null;


jQuery.preloadImages = function()
{
  for(var i = 0; i<arguments.length; i++)
  {
    jQuery("<img>").attr("src", arguments[i]);
  }
}


$.preloadImages("/images/window/corners.png", "/images/window/title_bg.png", "/images/window/close_out.gif", "/images/window/bg_l.png", "/images/window/bg_r.png", "/images/window/resize_l.png", "/images/window/resize.png", "/images/window/bottom_bg.png", "/images/folder_icons/icon_name_l.png", "/images/folder_icons/icon_name_r.png");


function _(str) {
	return false;
	//if(typeof console != 'undefined')
	//		console.log(str);
}

$(document).ready(function(){


//Запрет выделения текста и изображений
//onselectstart = function() {return false}
//onmousedown = function(e) {return false}


	//Счетчик глубины иконок
	zindexCounter = 100;
	currentSelectedIcon = null;
	ziCount = 1;
	
	canOpenWindow = true;

	//Делаем наши иконки перетягиваемыми
	$("a[id*='navIcon']").draggable({containment: 'window'});
	
	//Создаем контроль глубины иконок
	$("div[id*='navIcon']").mousedown(function(){
		zindexCounter += 1;
		$(this).css("z-index", zindexCounter);
	});
	
	//Выделяем иконку при нажатии на нее
	$("div[id*='navIcon']").mousedown(function(){
										   
		if(currentSelectedIcon != null){
			
			$("#navIcon"+currentSelectedIcon).find(".icon_name_l").css("background", "url(../images/folder_icons/icon_name_l.png) left no-repeat");
			$("#navIcon"+currentSelectedIcon).find(".icon_name_r").css("background", "url(../images/folder_icons/icon_name_r.png) right no-repeat");

			$("#navIcon"+currentSelectedIcon).find("a").css({"background": "#e2e7ed", 
															"border-bottom": "0px", 
															"border-top": "0px", 
															"height": "auto", 
															"line-height": "17px"});
						
			$("#navIcon"+currentSelectedIcon).find(".icon_name_l").ifixpng();
			$("#navIcon"+currentSelectedIcon).find(".icon_name_r").ifixpng();
		}
		currentSelectedIcon = $(this).attr("id").substr(7, 2);
										   
		$(this).find(".icon_name_l").css("background", "url(../images/folder_icons/icon_name_l_over.png) left no-repeat");
		$(this).find(".icon_name_r").css("background", "url(../images/folder_icons/icon_name_r_over.png) right no-repeat");
		$(this).find("a").css({"background": "#e2e7ed", 
							"border-bottom": "2px solid #b51b25", 
							"border-top": "2px solid #b51b25", 
							"line-height": "13px"});
		
		$(this).find(".icon_name_l").ifixpng();
		$(this).find(".icon_name_r").ifixpng();

		
	});
	
	$("div[id*='navIcon']").hover(function(){
		$(this).find("a").css("color", "#000000");
	},function(){
		$(this).find("a").css("color", "#666666");
	});

	

	$("div[id*='navIcon']").click(function(){
										   
		var currentWindowOptions = $(this).attr("attrs");
		currentWindowOptions = currentWindowOptions.split(",");
		var winSRC = currentWindowOptions[2];
		
		if(winSRC == "/main/very_other/"){
			
			var alertWin = $("#alertWin").dialog({
								title:"Вам исполнилось 18 лет ?", 
								show: 'slide',
								width:420,
								height:180,
								minWidth: 420,
								minHeight: 160,
								closeOnEscape: false,
								modal: false
								});
			
		
		ziCount += 1;
		document.getElementById("alertWin").parentNode.parentNode.parentNode.style.zIndex = $.ui.dialog.overlay.maxZ = ++$.ui.dialog.maxZ;
		
		$(".banner").each(function(){
			$(this).css("z-index", $.ui.dialog.overlay.maxZ = ++$.ui.dialog.maxZ);
		});

		
		$("#ui-dialog-title-alertWin").empty();
		$("#ui-dialog-title-alertWin").prepend("<div class='main-bg'>Сообщение</div>");

		}
		
			
	});


	$.each($("div[id*='navIcon']"), function() {
		var currentWindowOptions = $(this).attr("attrs");
		currentWindowOptions = currentWindowOptions.split(",");
		
		var winSRC = currentWindowOptions[2];
		
		map[winSRC] = $(this).attr('id'); // filling up map
		
	})
	
	
	$("div[title='gallery']").each(function(){
		$(this).lightBox();
	});
	
	
	$('img').ifixpng(); 
	$('.icon_name_l').ifixpng(); 
	$('.icon_name_r').ifixpng(); 
	
	setInterval("runHash()", 300);

});


$(window).load(function(){

//loadTimeout = setTimeout('$("#wait_loading").fadeOut("slow", destroyWaitLoading);',1000);
loadTimeout = setTimeout('$("#wait_loading").fadeOut("slow", showContent);',1000);

});

function getSettings(url) {
	for(var i=0; i<settings.length;i++) {
		if(settings[i].alias == url)
			return settings[i];
	}
}

function runWindow(url) {

	    if(!isContentVisible) {
	    	setTimeout('runWindow("' + url + '")', 500);
	    } else {
		
			var currentWindowOptions = getSettings(url);
			
			var winW = currentWindowOptions['win_w'];
			var winH = currentWindowOptions['win_h'];
			var winSRC = currentWindowOptions['module_url'];
			var winID = currentWindowOptions['id'];
			var winTitle = currentWindowOptions['name_ru'];
			var intAlias = currentWindowOptions['alias'];		
			
			if(String(winH).substring(String(winH).length-1, String(winH).length+1) == "%"){
			
				var Pnum = String(winH).substring(0, String(winH).length-1);
				winH = $(window).height() / 100 * Pnum;
			
			}
			
			if(String(winW).substring(String(winW).length-1, String(winW).length+1) == "%"){
				
				var Pnum = String(winW).substring(0, String(winW).length-1);
				winW = $(window).width() / 100 * Pnum;
				
			}
			
			winW = Number(winW);
			winH = Number(winH);
	
	
			if(canOpenWindow && winSRC != "/main/very_other/"){
				
				canOpenWindow = false;
			
				if($("#"+winID).text() == ""){
					$('body').append('<div id="'+winID+'"></div>');
				}else{
					$("#"+winID).empty();
				}
				
				currbodyContent = $.ajax({
						url: winSRC,
						type: "POST",
						dataType: "text",
						success: function(msg){
							$("#"+winID).append(msg);
							$(".ui-dialog-loader").css("display", "none");
							canOpenWindow = true;
						}
					});
				 
				
				_(winW);
				var currWin = $("#"+winID).dialog({
									show: 'slide',
									title:winTitle, 
									width:winW,
									height:winH,
									resizable: false,
									minWidth: 730,
									minHeight: 300,
									maxWidth: 900,
									closeOnEscape: false,
									resize: function(event, ui){
										
											_(currWin.width());
											$("#"+winID).css("height", currWin.height()+"px");
											$("#"+winID).css("width", currWin.width()+"px");
										
										}
									});
				
				
				ziCount += 1;
				document.getElementById(winID).parentNode.parentNode.parentNode.style.zIndex = $.ui.dialog.overlay.maxZ = ++$.ui.dialog.maxZ;
				
				$(".banner").each(function(){
										   
					$(this).css("z-index", $.ui.dialog.overlay.maxZ = ++$.ui.dialog.maxZ);
										   
				});
				
				
				$("#ui-dialog-title-"+winID).empty();
				$("#ui-dialog-title-"+winID).prepend("<div class='main-bg'>"+winTitle+"</div>");
				
				
			}
	
	// конец обработки клика по иконке
	
	}
}

function runHash() {
	//Check if it has changes
	if(currentAnchor != document.location.hash){
		currentAnchor = document.location.hash;
		//if there is not anchor, the loads the default section
		if(!currentAnchor) {
			//query = "section=home";
		} else {
			//Get the section
			var section = currentAnchor.substring(1);
			closeAllWindows();
			runWindow(section)
		}
	}
}


function closeAllWindows(){
	$(".ui-dialog").remove();
}


function openBannerWindows(banId){
	
	var currentWindowOptions = $("#"+banId).attr("attrs");
	currentWindowOptions = currentWindowOptions.split(",");
	
	var winW = currentWindowOptions[0];
	var winH = currentWindowOptions[1];
	var winSRC = currentWindowOptions[2];
	var winID = currentWindowOptions[3];
	var winTitle = currentWindowOptions[4];
	var intId = currentWindowOptions[5];


	if(String(winH).substring(String(winH).length-1, String(winH).length+1) == "%"){
	
		var Pnum = String(winH).substring(0, String(winH).length-1);
		winH = $(window).height() / 100 * Pnum;
	
	}
	
	if(String(winW).substring(String(winW).length-1, String(winW).length+1) == "%"){
		
		var Pnum = String(winW).substring(0, String(winW).length-1);
		winW = $(window).width() / 100 * Pnum;
		
	}
	
	winW = Number(winW);
	winH = Number(winH);



	if(canOpenWindow){
		
		canOpenWindow = false;
	
		if($("#"+winID).text() == ""){
			$('body').append('<div id="'+winID+'"></div>');
		}else{
			$("#"+winID).empty();
		}
		
		currbodyContent = $.ajax({
				url: winSRC,
				type: "POST",
				dataType: "text",
				success: function(msg){
					$("#"+winID).append(msg);
					$(".ui-dialog-loader").css("display", "none");
					canOpenWindow = true;
					
					setTimeout('$("#'+winID+'").scrollTo($("#view_'+intId+'"), 0, {offset:-40});', 300);
					
				}
			});
		 
		
		_(winW);
		var currWin = $("#"+winID).dialog({
							show: 'slide',
							title:winTitle, 
							width:winW,
							height:winH,
							resizable: true,
							minWidth: 730,
							minHeight: 300,
							maxWidth: 900,
							closeOnEscape: false,
							resize: function(event, ui){
								
									//$("#"+winID).css("height", currWin.height()+"px");
									//$("#"+winID).css("width", currWin.width()+"px");
								
								}
							});
		
				
		ziCount += 1;
		document.getElementById(winID).parentNode.parentNode.parentNode.style.zIndex = $.ui.dialog.overlay.maxZ = ++$.ui.dialog.maxZ;
		
		$(".banner").each(function(){
								   
			$(this).css("z-index", $.ui.dialog.overlay.maxZ = ++$.ui.dialog.maxZ);
								   
		});
		
		
		$("#ui-dialog-title-"+winID).empty();
		$("#ui-dialog-title-"+winID).prepend("<div class='main-bg'>"+winTitle+"</div>");
		
	}
	
}





function show_very_other(is_year){
	
	if(!is_year){
		$("div[aria-labelledby='ui-dialog-title-alertWin']").empty();
	}else{
		$("div[aria-labelledby='ui-dialog-title-alertWin']").empty();
		
		var currentWindowOptions = $("#navIcon12").attr("attrs");
		currentWindowOptions = currentWindowOptions.split(",");
		
		var winW = Number(currentWindowOptions[0]);
		var winH = Number(currentWindowOptions[1]);
		var winSRC = currentWindowOptions[2];
		var winID = currentWindowOptions[3];
		var winTitle = currentWindowOptions[4];

		canOpenWindow = false;
	
		if($("#"+winID).text() == ""){
			$('body').append('<div id="'+winID+'"></div>');
		}else{
			$("#"+winID).empty();
		}
		
		currbodyContent = $.ajax({
				url: winSRC,
				type: "POST",
				dataType: "text",
				success: function(msg){
					$("#"+winID).append(msg);
					$(".ui-dialog-loader").css("display", "none");
					canOpenWindow = true;
				}
			});
		 
		
		_(winW);
		var currWin = $("#"+winID).dialog({
							show: 'slide',
							title:winTitle, 
							width:winW,
							height:winH,
							resizable: true,
							minWidth: 730,
							minHeight: 300,
							maxWidth: 900,
							closeOnEscape: false,
							resize: function(event, ui){
								
									//$("#"+winID).css("height", currWin.height()+"px");
									//$("#"+winID).css("width", currWin.width()+"px");
								
								}
							});
		
			ziCount += 1;
			document.getElementById(winID).parentNode.parentNode.parentNode.style.zIndex = $.ui.dialog.overlay.maxZ = ++$.ui.dialog.maxZ;
			
			$(".banner").each(function(){
									   
				$(this).css("z-index", $.ui.dialog.overlay.maxZ = ++$.ui.dialog.maxZ);
									   
			});
			
			
			$("#ui-dialog-title-"+winID).empty();
			$("#ui-dialog-title-"+winID).prepend("<div class='main-bg'>"+winTitle+"</div>");

	
		
	}
	
}

// фикс серго блока некоторых окон
function _getWidth(id) {
	return $("#ntn" + id).parent().get(0).offsetWidth-36;
}

function _setWidth(id) {
	$("#ntn" + id).css("width", _getWidth(id));
}
