//var scroll_v = 50;
var scroll_v = 400;
var timer = '';
var MousePageX = 0;
var all_width = 0;
var blocks_data = new Array;
var columnize_flag = true;
var img_count_temp = 0;
var img_count_temp_for_load = 0;
var wait_load_timer = 0;
var jsColumnizer;


function CreateRightArrow(){
	$("body").append("<div id='right_arrow'></div>");
	var div_height = $(window).height();
	$("#right_arrow").css('height', div_height+'px');

	var div_left = $(window).width() - 144;
	$("#right_arrow").css('left', div_left+'px');
	$("#right_arrow").append("<div id='right_arrow_content'></div>");
	$("#right_arrow_content").css('height', div_height+'px');
	$("#right_arrow_content").append("<div id='right_arrow_mark'></div>");
	$("#right_arrow_mark").css('top', Math.round((div_height-112)/2)+'px');

	$("#right_arrow").bind("mouseenter", function(e){
		if($(window).scrollLeft()+$(window).width() < all_width){
			$("#right_arrow_content").css('display', 'block');
		}
	});
	$("#right_arrow").bind("mouseleave", function(e){
		$("#right_arrow_content").css('display', 'none');
	});


//	$("#right_arrow").click(function(){
//		ScrollTimer('right');
//	});

	$("#right_arrow_mark").click(function(){
		ScrollTimer('right');
	});
}

function CreateLeftArrow(){
	$("body").append("<div id='left_arrow'></div>");
	var div_height = $(window).height();
	$("#left_arrow").css('height', div_height+'px');

	var div_left = 0;
	$("#left_arrow").css('left', div_left+'px');
 	$("#left_arrow").css('width', '0px');

	$("#left_arrow").append("<div id='left_arrow_content'></div>");
	$("#left_arrow_content").css('height', div_height+'px');
	$("#left_arrow_content").append("<div id='left_arrow_mark'></div>");
	$("#left_arrow_mark").css('top', Math.round((div_height-112)/2)+'px');

	$("#left_arrow").mouseenter(function(){
		if($(window).scrollLeft() > 0){
			$("#left_arrow_content").css('display', 'block');
		}
	});

	$("#left_arrow").mouseleave(function(){
		$("#left_arrow_content").css('display', 'none');
	});

//	$("#left_arrow").click(function(){
//		ScrollTimer('left');
//	});

	$("#left_arrow_mark").click(function(){
		ScrollTimer('left');
	});
}

function ScrollTimer(dir){
	var current_scroll = $(window).scrollLeft();
	all_width = $("#allcont").width() + 100;

	$("#img_selector").css('display', 'none');

	if(dir == "right"){
		all_width = $("#center").width() + 80;
		current_scroll = GetCurrentBlock("right", current_scroll);
		if(current_scroll == $(window).scrollLeft())
			current_scroll = current_scroll  + scroll_v;
		if(($(window).width() + current_scroll) > all_width ) current_scroll = all_width-$(window).width();

		$("html:not(:animated)"+( ! $.browser.opera ? ",body:not(:animated)" : "")).animate({scrollLeft: current_scroll}, 'slow');
		MousePageX = MousePageX +scroll_v;

		if(current_scroll < all_width-$(window).width()){
			$("#right_arrow").css('width', '144px');
		}else{
			$("#right_arrow").css('width', '0px');
		}
		$("#left_arrow").css('width', '144px'); 
	}else{

		if(current_scroll >0){
			current_scroll = GetCurrentBlock("left", current_scroll);
			if(current_scroll == $(window).scrollLeft())
				current_scroll = current_scroll  - scroll_v;
			if(current_scroll < 0 )current_scroll = 0;

			$("html:not(:animated)"+( ! $.browser.opera ? ",body:not(:animated)" : "")).animate({scrollLeft: current_scroll}, 'slow');
			MousePageX = MousePageX - scroll_v;
			if(MousePageX < 0 )MousePageX = 0;

			if(current_scroll > 0){
				$("#left_arrow").css('width', '144px');
			}else{
				$("#left_arrow").css('width', '0px');
			}
			$("#right_arrow").css('width', '144px'); 
		}
	}
}

function RepositionRight(s, scroll, err){
	var div_left = $(window).width() + scroll - 144 + err;
//	$(s).css('left', div_left+'px');

	if(div_left +144 >= all_width){
		$(s).css('display', 'none');
	}else{
		$(s).css('display', 'block');
	}
}

function RepositionLeft(s, scroll, err){
	var div_left = scroll - err;
//	$(s).css('left', div_left+'px');
	if(div_left == 0){
		$(s).css('display', 'none');
	}else{
		$(s).css('display', 'block');
	}
}

function RepositionArrows(){
	var div_height = $(window).height();
	var div_left = $(window).width() - 144;
	$("#right_arrow").css('left', div_left+'px');

	$("#right_arrow").css('height', div_height+'px');
	$("#right_arrow_content").css('height', div_height+'px');
	$("#right_arrow_mark").css('top', Math.round((div_height-112)/2)+'px');

	$("#left_arrow").css('height', div_height+'px');
	$("#left_arrow_content").css('height', div_height+'px');
	$("#left_arrow_mark").css('top', Math.round((div_height-112)/2)+'px');
}

function SetCopyrights(){
	$("#copyright").css('top', ($(window).height()-48)+'px');
}

$(document).ready(function () {
	if(jQuery.isFunction(jsColumnizer)){
		jsColumnizer();
	}
	SetCopyrights();
	bodyLines();
	setOnRelImage();
	CreateRightArrow();
	CreateLeftArrow();

	$(window).resize(function(){
		RepositionArrows();
		SetCopyrights();
	});

	$(window).scroll(function(){
//		RepositionRight("#right_arrow", $(window).scrollLeft(), 0);
//		RepositionLeft("#left_arrow", $(window).scrollLeft(), 0);
		if($(window).scrollTop() != 0 ){ $(window).scrollTop(0); return false;}
	});

	$('img').each(function(i){
		$(this).load(function(){
			img_count_temp++;
		});
	});

	$().mousemove(function(e){
		MousePageX = e.pageX ;
	}); 
});

$(window).load(function () {
	all_width = $("#main").width() + 100;

	if($("#allcont").width() < $(window).width()){
		$("#main").width($(window).width() + 80);
	}else{
		$("#main").width($("#allcont").width() + 80);
	}
	$("#main").height($(window).height());
	GetBlocksData( $("#center") );

});

function bodyLines(){
	var line1 = Math.ceil(Math.random()*6);

	$("#main").wrapInner("<div id='bgline1' class='bgline"+line1+"'></div>");
	$("#bgline1").css('background-position', 'center top');
	$("#bgline1").height($("#main").height());
	
}

function setOnRelImage(){
	$("body").append("<div id='img_selector'></div>");
	$("a[rel='viewimg']").each(function(i){
		$(this).mouseover(function(){
			var href = $(this).attr('href');
			var img_obj = $(this).children().get(0);
			var width = $(img_obj).attr('width');
			var height = $(img_obj).attr('height');
			var left = $(img_obj).offset().left;
			var top = $(img_obj).offset().top;
			$("#img_selector").css('width', width+'px');
			$("#img_selector").css('height', height+'px');

			$("#img_selector").css('left', left+'px');
			$("#img_selector").css('top', top +'px');
			$("#img_selector").css('display', 'block');

			$("#img_selector").click(function(){
				location.href=href;
			});
			$("#img_selector").mouseout(function(){
				$(this).css('display', 'none');
			})
		});

	});

	$("body").append("<div id='author_selector'></div>");
	$("a[rel='viewauthor']").each(function(i){

		$(this).mouseenter(function(){

			var href = $(this).attr('href');
			var img_obj = $(this).children().get(0);
			var width = $(img_obj).attr('width');
			var height = $(img_obj).attr('height');
			var left = $(img_obj).offset().left;
			var top = $(img_obj).offset().top;

			$("#author_selector").css('width', width+'px');
			$("#author_selector").css('height', height+'px');
			$("#author_selector").css('left', left+'px');
			$("#author_selector").css('top', top+'px');
			$("#author_selector").css('display', 'block');

			$("#author_selector").click(function(){
				location.href=href;
			});
			$("#author_selector").mouseleave(function(){
				$("#author_selector").css('display', 'none');
			});
		});

	});

}

function GetBlocksData(obj){
	obj.children().each(function(i){
		if(this.tagName == 'DIV' || this.tagName == 'IMG' ){
			var emp1 = new Object( );
			emp1.tagName = this.tagName;
			emp1.leftBorder = $(this).offset().left;
			emp1.rightBorder = $(this).offset().left + $(this).width();

			blocks_data[blocks_data.length] = emp1;
//			alert(emp1.tagName +' '+ emp1.leftBorder +' '+ emp1.rightBorder);
			GetBlocksData($(this));
		}
	});

}

function GetCurrentBlock(param, current_scroll_value){
	var diff = 0;
	var temp_diff = 0;
	var current_value = current_scroll_value + $(window).width();
	for(i=0; i<blocks_data.length; i++){
		if(param == 'right'){
			temp_diff = blocks_data[i].rightBorder - current_value;
		}else{
			temp_diff = current_scroll_value - blocks_data[i].leftBorder;
		}
		if(temp_diff > 0 && ( diff == 0 || (temp_diff < diff && temp_diff > 100) )) diff = temp_diff;
	}
	if(diff > 0) diff = diff +40;
	if(param == 'right'){
		return current_scroll_value + diff;
	}else{
		return current_scroll_value - diff;
	}
}

function SetColumnFlag(){
	columnize_flag = true;
}