$(document).ready(function(){

	var page = new Page();
	page.PreparePage();	
	
	$('body').resize(function(){
		page.SetSize();
	});
	
	$('body').load(function(){
		page.SetSize();
	});
	
	$("#works img").load(function(){
		page.SetSize();
	});
	
	var info_window = $('#info_window');
	var info_window_name = $('#info_window_name');
	var info_window_content = $('#info_window_content');
	// anti spamer
	var sabaka = '@';
	$('#email').text( "info" + sabaka + $('#email').text() );
	$('#email').attr( 'href', 'mailto:' + $('#email').text() );
	
	//info_window
	
	info_window_name.click(function(){
		$(info_window).css('visibility', 'hidden');
	});
	
	$('*').live('click', function(e){
		if( $(this).hasClass('iwc') )
		{
			return ;
		}
		if( typeof($(this).parents(".iwc").get(0)) == 'undefined' )
		{
			$(info_window).css('visibility', 'hidden');
		}
	});
	
	var info_window_html = info_window.get(0).outerHTML;
	
	$("#contacts .info").each(function(){
		var href = $(this).attr("href");
		href = href + "?ajax=1";
		
		$(this).attr("href", href);
	});
	
	$("#contacts .info").click(function(e){
		e.preventDefault();
		var a = $(this);
		var ie6 = ($.browser.msie && ($.browser.version == '6.0'));
		
		info_window_name.get(0).anker = a;
		
		info_window_name.attr('class', a.attr('class'));
		info_window_name.html(a.html().replace(' ', '&nbsp;'));
		
		info_window_content.html("");
		$("<img/>").attr('src', '/i/i/dotf/load.gif').css('margin', '0px 40px').appendTo(info_window_content);
		
		$(info_window).css('left', a.offset().left + a.width() - $(info_window).width() + 34 - $.leftScroll());
		$(info_window).css('top', a.offset().top + a.height() - $(info_window).height() + 51 - $.topScroll());
		
		$(info_window).css('visibility', 'visible');
		
		$.ajax({
			url: a.attr('href') + '?clear',
			dataType : "html",
			success: function (data, textStatus){
				
				$(info_window).css('visibility', 'hidden');
				
				$(info_window).css('left', 0);
				$(info_window).css('top', 0);
				
				info_window_content.html(data);
				
				if(ie6)
				{
					info_window.remove();
					info_window = $(info_window_html).appendTo('body:first');
					
					info_window = $('#info_window');
					info_window_content = $('#info_window_content');
					info_window_name = $('#info_window_name');
					
					info_window_name.get(0).anker = a;
				}

				$(info_window).css('left', a.offset().left + a.width() - $(info_window).width() + 34 - $.leftScroll());
				$(info_window).css('top', a.offset().top + a.height() - $(info_window).height() + 51 - $.topScroll());
				
				$(info_window).css('visibility', 'visible');
			}
        });
	});

	$("a.section").click(function(){
		
		$("#works").children("*").remove();
		$("#works").css("text-align", "center");
		$("#works").attr("align", "center");
		$("<img src='/i/icons/loader.gif' border='0' style='position: relative; top: 70px;' />").appendTo($("#works"));
		var section = $(this).attr("rel");
		
		$.ajax({
			type: "GET",
			url: "/ajax.user.php?action=getsection&section=" + section,
			cache: false,
			success: function(data){
				
				$("#works").children("*").remove();
				$("#works").html(data);
			},
			error: function() { }
		});
		
	});
	
	$(document).everyTime(200, 'lightbox', function(){
		
		var overlay = $("#overlay");
		if (typeof($(overlay).get(0)) != "object") return;
		
		$(overlay).css("position", "fixed");
		$("#lightbox").css("position", "fixed");
		$(document).stopTime('lightbox');
		
	}, 0);
	
	var overpopup = null;
			
	$(window).resize(function(){
			
		var page = new Page();
		page.SetSize();	
		page.SetPopupPosition();
		
		var height = $(window).height();
		var overlay = $("#overlay");
		if (typeof($(overlay).get(0)) == "object") 
		{
			$(overlay).css("height", height+"px");
		}
	});

	/*function GetWindowLeftScroll()
	{
		var scrollLeft = null;
		scrollLeft = window.pageXOffset ? window.pageXOffset : 0;
		if (scrollLeft != 0) return scrollLeft;
		
		scrollLeft = document.documentElement ? document.documentElement.scrollLeft : 0;
		if (scrollLeft != 0) return scrollLeft;
		
		scrollLeft = document.body ? document.body.scrollLeft : 0;
		return scrollLeft;
	}*/
	
	$("#popup_left").mouseover(function(){$(this).attr("src", "/i/i/dotf/popup/left_over.png");});
	$("#popup_left").mouseout(function(){$(this).attr("src", "/i/i/dotf/popup/left.png");});
	
	$("#popup_right").mouseover(function(){$(this).attr("src", "/i/i/dotf/popup/right_over.png");});
	$("#popup_right").mouseout(function(){$(this).attr("src", "/i/i/dotf/popup/right.png");});
	
	$("#popup_left").click(function(){
		
		var popup = new Popup();
		popup.LoadLeft();
		
	});
	
	$("#popup_right").click(function(){
		
		var popup = new Popup();
		popup.LoadRight();
		
	});
	
	$("a.portfolio_anchor").live('click', function(){

		var popup = new Popup($(this));
		popup.ShowPopup();
		
		return false;		
	});
	
	$("#popup").everyTime(10, 'popup', function(){

		$("#popup").each(function(){
			$(this).css('left', ($(window).width() / 2 - 368) + "px");
		});
		
	}, 0);
	
	$("#close").live('click', function(){
		
		$(window).stopTime('scrollTo');
		$("#popup").css("display", "none");
		$("#popup_left").css("visibility", "hidden");
		$("#popup_right").css("visibility", "hidden");
		
	});
	
});

