ListBlock = function() {
	this.init();
};

ListBlock.prototype = {

	init: function() {
		var t = this;
		
		if (window.ActiveXObject) {
			listBlock.get(0).ondrag = function(){return false};
			listBlock.get(0).onselectstart = function(){return false};
		}
		
		$('.block-project, .block-project-detail').mousedown(function() {
			$(this).addClass('cursor-move');
		});
		
		$(document.body).mouseup(function() {
			$('.block-project').removeClass('cursor-move');
			$('.block-project-detail').removeClass('cursor-move');
			$('.casefull').removeClass('cursor-move');
			//$('#page').removeClass('cursor-move-h');
			//$('.active-bp').removeClass('cursor-move-h');
		});
		
		
		// menu click
		$('#menu a').live('click', function() {
			var curLi = $(this).parent();
			if (!curLi.hasClass('active')) {
				var selectElement = $(this).attr('class');
				var selectBlock = listBlock.find('.' + selectElement).find('.block-project');
				if (selectBlock.length > 0 ) {
					//if ($('#menu').hasClass('movedtop')) curLi.find('span').show();
					//t.scrollTop();
					
					// close currently open
					t.closeProject(500, 150);
		
					//open new blocks
					selectBlock.parent().each(function() {
						t.openBlock($(this));
						t.sortList($(this));
					});
					$('#menu li').removeClass('active');
					curLi.addClass('active');
					t.moveList(selectElement);					
					t.moveMenu();
				
				} else return false;
			} else return false;
		});

		//menu close button show
/* 		$('#menu li').live('mouseover mouseout', function(event) {
			if ($(this).hasClass('active')) {
				//var curPadding = 57;
				//if($('#menu').hasClass('movedtop')) curPadding = 19;
				if (event.type == 'mouseover') {
					$(this).find('span').css({'display':'block'});
					return false;
				} else {
					$(this).find('span').hide();
					return false;
				}
			}
		});
*/
		// menu close button click
/* 		$('#menu .close').live('click', function() {
			window.location.hash = "index";
			var curLi = $(this).parent();
			var curA = curLi.find('a');
			var unselectElement = curA.attr('class');
			var unselectBlock = listBlock.find('.' + unselectElement).find('.block-project');

			//t.scrollTop();

			// close currently open
			t.closeProject(0, 0);

			curLi.removeClass('active');
			$(this).hide();

			var c1 = 0; //counter for all visible blocks
			var c2 = 0; //counter for current blocks (those will be closed)
			$('.elements-block').each(function() {
				if($(this).is(':visible')) {
					if($(this).hasClass(unselectElement)) {
						t.closeBlock($(this));
						c2++;
					}
					c1++;
				}
			});
			
			// return position if all are closed
			if((c1-c2 == 0) && !$('#menu li').hasClass('active')) {
				t.moveList(1);
				t.moveMenu(1);
			} else {
				t.moveList();
				t.moveMenu();
			}

		}); */

		// project open
		$('.block-project').live('click', function() {
			//t.scrollTop();
		
			// close currently open
			t.closeProject(500, 150);

			// open this
			curBlock = $(this).parent().find('.block-project-detail');
			//var curBlockid = curBlock.attr('class').replace('block-project-detail ','');
			//var curBlockid = $(this).parent().attr('class').replace('elements-block ','').replace(' start-show','');
			var curBlockid = $(this).parent().attr('id').replace('_project','');
			
			$(this).fadeOut(50).queue( function(){
				curBlock.parent().css({'width':'auto'});
				curBlock.css({'display':'block','width':'182px'});
				curBlock.find('.bpd-border').css({'overflow':'hidden'});
				curBlock.find('.bpd-wrapper').css({'overflow':'hidden'});
				curBlock.animate({width: 894 + 'px'}, 500, function() {
					curBlock.find('.bpd-border').css({'overflow':'visible'});
					curBlock.find('.bpd-wrapper').css({'overflow':'visible'});
				});
				curBlock.addClass('active-bp');
				t.moveList(curBlockid);
				$(this).dequeue(); 
			});
			$('#menu li').removeClass('active');
			t.moveMenu();

			setTimeout(function() {
				window.location.hash = curBlockid;
			}, 650);
			return false;
		});

		// project close button over
/* 		$('.bpd-close').live('mouseover', function() {
			$(this).css('opacity', 0.2).animate({opacity: 1.0}, 500);
		});
 */
		// project link over
/* 		$('.bpd-link').live('mouseover', function() {
			$(this).css('opacity', 0.2).animate({opacity: 1.0}, 500);
		});
 */
		// project details open
		$('.bpd-link').live('click', function() {
			// load content from case.php
			var url = $(this).find('a').attr('href');//'/case.php?hash=' + curBlockid;
			var patt = new RegExp('http://');
			if (url != "#" && !patt.test(url)) {
				var curBlock = $(this).closest('.block-project-detail');
				var curProject = curBlock.find('.casewide');
				var curProjectHeight = curProject.height();
				curProject.fadeOut(200, function() {
					curProject = curBlock.find('.casefull');
					curProject.css({'height':curProjectHeight + 'px','overflow':'hidden'});
					var curProjectAjax = curProject.find('.ajax-content');
					if (curProjectAjax.length == 0) {
						//curProject.css({'background':'url(images/ajax-loader.gif) no-repeat center'});
						$.ajax({
							url: url,
							timeout: 5000,
							success: function(data){
								curProject.html(data);
								curProjectAjax = curProject.find('.ajax-content');
								curProjectHeight = curProjectAjax.height();
								curProject.css({'background':'none','overflow':'visible'});
								curProject.animate({height:curProjectHeight+'px'}, 'slow');
								curProjectAjax.css({'height':curProjectHeight+'px'});

								var video = curProject.find('.video-container').html();
								curProject.append('<div class="video-container-copy" style="display:none;"></div>');
								curProject.find('.video-container-copy').html(video);	
								//updateContainer(data,'#ff9');
							},
							error: function(req,error){
								curProject.html('<div class="error">(error!)</div>');
								if(error === 'error'){error = req.statusText;}
								var errormsg = 'There was a communication error: '+error;
								curProject.html('<div class="error">'+errormsg+'</div>','#c00').css({'background':'none'});
							},
							beforeSend: function(data){
								curProject.css({'background':'url(images/ajax-loader.gif) no-repeat center'});
							}
						/* curProject.load(url + ' .ajax-content', function() {
							curProjectAjax = curProject.find('.ajax-content');
							curProjectHeight = curProjectAjax.height();
							curProject.css({'background-image':'none','overflow':'visible'});
							curProject.animate({height:curProjectHeight+'px'}, 'slow');
							curProjectAjax.css({'height':curProjectHeight+'px'});
						}); */
						});
					} else {
						curProjectHeight = curProjectAjax.css('height');
						curProject.animate({height:curProjectHeight}, 'slow', function() {
							curProject.css({'overflow':'visible'});
						});
						
						var video = curProject.find('.video-container-copy').html();
						curProject.find('.video-container').html(video);						
					}
				});
				
				/* $('#page').mousedown(function() {
					$(this).addClass('cursor-move-h');
				}); */
				
				/* listBlock.draggable({
					disabled: true
				}); 
				$('#page').clickNScroll({
					allowThrowing: false
				});*/
				return false;
			}
		});
		
		// project details close
		$('.collapse').live('click', function() {
			//t.scrollTop();
			t.closeProjectDetail($(this).closest('.casefull'), 300);
		});

		// project close button click
		$('.bpd-close').live('click', function() {
			var curProject = $(this).closest('.elements-block').attr('id').replace('_project','');	
			//t.scrollTop();
			t.closeProject(500, 150);
			t.moveList(curProject);
			t.moveMenu();
			if($('#menu li').hasClass('active')) {
				window.location.hash = $('#menu li.active a').attr('class');
			} else window.location.hash = "index";
		});

 		//logo click
		$('#logo').live('click', function() {
			if(window.location.hash != "") window.location.hash = "index";
			
			//t.scrollTop();
			t.closeProject(300, 150);

			$('#menu li').each(function() {
				if ($(this).hasClass('active')) {
					$(this).removeClass('active');
				}
			});

			$('.elements-block').each(function() {
				if(!$(this).hasClass('start-show')) {
					$(this).animate({width:'0'}, 0, function() {
						$(this).css({'display':'none'});
						return false;
					});
				} else {
					t.openBlock($(this));
				}
			});

			t.moveMenu(1);
	
			var widthChange = $('#header').offset().left;
			var curPosList = widthChange + 778;
			listBlock.animate({left: curPosList +'px'});
		});

		$(window).load(function (){			
			var widthChange = $('#header').offset().left;

			$(function(){
				listBlock.draggable({
					axis: 'x',
					cancel: 'input, option',
					cursor: 'e-resize',
					delay: '100',
					distance: 3,
					drag: function(){
						var leftPositionList = $(this).offset().left;
						countList = widthChange + 270;
						if (leftPositionList <= countList) {
							$('#menu').addClass('movedtop');
							
						} else {
							$('#menu').removeClass('movedtop');
						}
					},
					scroll: false/* ,
					stop: function(){
						//bringing back from offscreen
						var w = document.documentElement.clientWidth || document.body.clientWidth;
						var listLeftPosition = $(this).offset().left;
						var listWidth = 0;
						$('.elements-block').each(function() {
							if($(this).is(':visible')) listWidth += $(this).width();
						});
						var listLeftOffscreen = listLeftPosition + listWidth;
		
						if (listLeftPosition > (w - 100)) {
							if (w < 980) {
								listLeftPosition = 880;
							} else listLeftPosition = w - 100;
						} else if (listLeftOffscreen < 100) {
							listLeftPosition = -(listWidth - 100);
						}
						$(this).animate({'left': listLeftPosition + 'px'});
					} */
				});
			});
			//t.listWidth();

		});
		
		// $('#page').scroll(function() {		
		$('#page').bind('scrollstop', function(){ 
			if ($('.ajax-content').is(':visible')) {
				var openedCase = window.location.hash.replace('#', '');
				$('.elements-block').each(function() {
					if($(this).is(':visible') && ($(this).attr('id').replace('_project','') != openedCase)) {
						$(this).stop().animate({'top': ($('#page').scrollTop()) + 'px'}, 'slow', 'easeOutBack');
					}
				});
				//$('#page').addClass('cursor-move-h');
				//$('.active-bp').addClass('cursor-move-h');
			}
		});

	},
	
	moveList: function(s) {
		//listBlock.stop();
		var w = document.documentElement.clientWidth || document.body.clientWidth;
		var widthChange = $('#header').offset().left;
		var curPosList = widthChange + 778;
		var prevBlocksNum = 0; // counter for blocks standing before current
		var blockWidth = 183; // one block width
		// finding out the count of previous blocks
		$('.elements-block').each(function(){
			if($(this).is(':visible')) {
				if(($(this).attr('id').replace('_project','') == s) || $(this).hasClass(s)) return false;
				prevBlocksNum++;
			}
		});
		if (s) {
			if(listBlock.find('#' + s + '_project').length > 0) {
				// position of open project
				widthChange = (w - 904)/2;
				//alert(widthChange);
				//widthChange = (prevBlocksNum * blockWidth) - widthChange;
			} //else {
				// change position to current blocks
				widthChange = widthChange - (prevBlocksNum * blockWidth);
			//}
		}
		listBlock.animate({left: widthChange +'px'}, 500);
		return false;
	},
	
	moveMenu: function(s) {
		var m = $('#menu');
		var c = 'movedtop';
		var t = 500;
		if (s == 1) {
			m.removeClass(c, t);
			return false;
		} else if (!m.hasClass(c)) {
			m.addClass(c, t);
			return false;
		}
		else return false;
	},
	
	openBlock: function(t) {
		if(!t.is(':visible')) {
			t.animate({width:'182px'}, 400);
		}
	},
	
	sortList: function(t){
		t.hide().prependTo(listBlock).fadeIn(400);
	
	},
	
	closeBlock: function(t) {
		t.animate({width:'0'}, 400).queue( function() {
			t.css({'display':'none'});
			t.dequeue();
		});
	},

	closeProject: function(t1, t2) {
		if ($('.active-bp').is(':visible')) {
			
			if($('.ajax-content').is(':visible')) {
				this.closeProjectDetail($('.active-bp').find('.casefull'), 0);
			}
			$('.active-bp').find('.bpd-border').css({'overflow':'hidden'});
			$('.active-bp').find('.bpd-wrapper').css({'overflow':'hidden'});
			$('.active-bp').animate({width: 182}, t1).queue( function () {
				$(this).removeClass('active-bp');
				$(this).hide().queue( function () {
					$(this).parent().css({'width':'182px'});
					$(this).parent().find('.block-project').fadeIn(t2);
					$(this).dequeue(); 
				});
				$(this).dequeue(); 
			});
		}
		return false;
	},
	
	closeProjectDetail: function(t, t1) {
		t.css({'overflow':'hidden'}).queue(function () {
			$(this).animate({height:'336px'}, t1, function() {
				$(this).fadeOut(200, function() {
					$(this).closest('.bpd-wrapper').find('.casewide').fadeIn(200);
				});
			});
			$(this).dequeue();
			//t.scrollTop();
			$('.elements-block').each(function() {
				if($(this).is(':visible') && ($(this).css('top') != '0px')) {
					$(this).stop().css({'top':0});
				}
			});
			return false;
		});

		t.find('.video-container').html('');
		
		/* listBlock.draggable({
			disabled: false
		}); */
	},
	
	scrollTop: function() {
		alert('test');
		$('.elements-block').each(function() {
			if($(this).is(':visible') && ($(this).css('top') != '0px')) {
				$(this).stop().css({'top':0});
			}
		});
	},
	
	listWidth: function() {
		var w = 0;
		$('.elements-block').each(function() {
			if($(this).is(':visible')) {
				w += ($(this).width() + 1);
			}
		});
		listBlock.css({'width': w + 'px'});
	}

};

$(document).ready(function(){

	listBlock = $('#draggable');

	var w = document.documentElement.clientWidth || document.body.clientWidth;

	$(document.body).css({
	});
	$('#page').css({
		'margin':'0',
		/* 'min-height':'530px',
		'min-width':'980px', */
		'overflow':'hidden',
		'overflow-y':'auto',
		'width':'100%'
	});
	//var cto = $('#content').height() + 60;
	$('#cases').css({
		/* 'overflow':'visible', 
		'margin-top': '-' + cto + 'px',
		'min-height': cto + 'px',*/
		'overflow':'visible',
		'position':'relative'
	});
	listBlock.css({
		'height':'0',
		'left': w + 'px',
		'position':'absolute',
		//'top':'-' + cto + 'px',
		'top':'76px',
		'width':'10000px'
	});
	$('.elements-block').css({
		'display':'none',
		'width':0
	});
	$('.block-project').css({
		'cursor':'pointer',
		'display':'block',
		'margin-top':'40px'
	});
	/* $('.start-show').css({
		'display':'block',
		'width':'182px'
	}); */ 
	$('.block-project-detail').css({
		'cursor':'url("/images/hand.cur"), w-resize',
		'display':'none'
	});
	/* $('.casesocial').hide(); */
	
	/* 
	var sliderCnt = 0;
	$('.bpd-slider').each(function(){
		sliderCnt = 0;
		$(this).find('.slide').each(function(){
			sliderCnt ++;
		});
		$(this).find('.overlay').css({'width':sliderCnt*671 + 'px'});
	});
	$('.bpd-buttons').css({'display':'block'});*/

	new Resizer({
		minWidth: 980,
		maxWidth: 1200,
		data: resizerData
	});
	
	new ListBlock();

	//open project defined in window hash
	var selectedBlock = window.location.hash.replace('#', '');
	var widthChange = $('#header').offset().left;
	var curPosList = widthChange + 778;
	var openBlock = $('.elements-block');
	var prevBlocksNum = 0;
	var blockWidth = 183;

	if (selectedBlock != "" && selectedBlock != "index"){
		var openBlockPos = $('.' + selectedBlock);
		//$('.block-project').css({'display':'none'});
		openBlock.each(function() {
			if ($(this).attr('id').replace('_project','') == selectedBlock) {
				$(this).css({'display':'block','width':''});
				$(this).find('.block-project').css({'display':'none'});
				$(this).find('.block-project-detail').css({'display':'block','width':'894px'}).addClass('active-bp');
				//$(this).find('.block-project-detail');
				var url = $(this).find('.project-detail').attr('href');
				//$(this).find('.bpd-wrapper').load(url + ' .iii');
				//selectedBlock = $(this).parents('.elements-list').attr('class').replace('elements-list ', '');
				//openBlockPos = $('#' + selectedBlock);
				widthChange = (w - 904)/2;
				//alert(prevBlocksNum);
				widthChange = widthChange - (prevBlocksNum * blockWidth);
			} else if ($(this).hasClass(selectedBlock)) {
				$(this).css({'display':'block','width':'182px'});
				$('#menu a.' + selectedBlock).parent().addClass('active');
				$(this).prependTo(listBlock);
			} else {
				//$(this).css({'display':'none','width':'0'});
				$(this).css({'display':'block','width':'182px'});
			}
			prevBlocksNum++;
		});
		/*if (openBlock.attr('id').replace('_project','') == selectedBlock) {
		
		} else if (openBlock.hasClass(selectedBlock)) {
			openBlock.each(function() {
				if($(this).hasClass(selectedBlock)) {
					$(this).css({'display':'block','width':'182px'});
				} else {
					$(this).css({'display':'none','width':'0'});
				}
			});
			var openBlockPos = $('.' + selectedBlock + ' div:first-child');
			$('#menu a.' + selectedBlock).parent().addClass('active');
		}*/
		//var cl = Math.round(openBlockPos.offset().left);
		//var ll = Math.round(listBlock.offset().left);
		//widthChange = widthChange - (cl - ll);
		//alert(widthChange);
		listBlock.animate({left: widthChange + 'px'}, 0);
		$('#menu').addClass('movedtop');
		/*var openBlock = $('#draggable #' + selectedBlock);
		if (openBlock.length) {
			if(openBlock.hasClass('elements-block')) {
				openBlock.find('.block-project').css({'display':'none'});
				openBlock.parent().find('.elements-block').css({'display':'block', 'width':'182px'});
				openBlock.css({'display':'block', 'width':'auto'});
				openBlock.find('.block-project-detail').css({'display':'block','width':'894px'});
				openBlock.find('.block-project-detail').addClass('active-bp');
				var url = openBlock.find('.project-detail').attr('href');
				openBlock.find('.bpd-wrapper').load(url + ' .iii');
				selectedBlock = openBlock.parents('.elements-list').attr('class').replace('elements-list ', '');
				var openBlockPos = openBlock;
			} else {
			}
		}*/
	} else {
		openBlock.each(function() {
			//if ($(this).hasClass('start-show')) {
				$(this).css({'display':'block','width':'182px'});
			//}
		});
		listBlock.animate({left: curPosList + 'px'});
	}
	
	$('.map a').live('click', function(){
		var url = $(this).attr('href');
		var container = $('.popup-content');
		$('.popup-overlay').fadeIn(500, function() {
			$('.popup').fadeIn(300, function() {
				if (container.html() === '') {
					$.ajax({
						url: url,
						timeout: 5000,
						success: function(data){
							container.css({'background-image':'none'});
							container.html(data);
						},
						error: function(req,error){
							container.html('<div class="error">(error!)</div>');
							if(error === 'error'){error = req.statusText;}
							var errormsg = 'There was a communication error: '+error;
							container.html('<div class="error">'+errormsg+'</div>','#c00').css({'background-image':'none'});
						},
						beforeSend: function(data){
							container.css({'background-image':'url(/images/ajax-loader.gif)'});
						}
					});
				}
			});
		});
		
		return false;
	});
	
	$('.popup-overlay').live('click', function(){
		popupClose();
	});
	$('.popup .close').live('click', function(){
		popupClose();
	});
	
/*
	$('.bpd-border').hover(
		function(){
			$(this).find('.casesocial').show();
		}, 
		function(){
			$(this).find('.casesocial').hide();
		}
	);
*/

});

var popupClose = function() {
	$('.popup').fadeOut(300, function() {
		$('.popup-overlay').fadeOut(300);
	});
}

var newClass = function() {
	return function() {
		return this.init.apply(this, arguments);
	}
}

var resizerData = {
	/* '#pageBorder': {
		'width': [15, 220, 'px']
	} */
	'#header': {
		'margin-left':[15, 70, 'px'],
		'margin-right':[15, 70, 'px']
	},
	'#content': {
		'margin-left':[15, 70, 'px']
	},
	'#footer': {
		'margin-left':[15, 70, 'px'],
		'margin-right':[15, 70, 'px']
	}
	
};

var Resizer = newClass();

Resizer.prototype = {
	init: function(p) {
		var t = this;
		t.data = p.data;
		t.minWidth = p.minWidth;
		t.maxWidth = p.maxWidth;
		t.elements = {};
		for(selector in t.data) {
			t.elements[selector] = $(selector);
		}
		t.windowResize();
		window.onresize = function() {
			//draggable position on resize
			var widthChange = $('#header').offset().left;
			//var listBlock = $('#draggable');
			var leftOffsetPosition = listBlock.offset().left - widthChange;
			t.windowResize();
			var widthChange = $('#header').offset().left;
			listBlock.css({'left':leftOffsetPosition + widthChange + 'px'});
			return false;
		}
	},
	windowResize: function() {
		var t = this;
		var h = document.documentElement.clientHeight || document.body.clientHeight;
		var w = document.documentElement.clientWidth || document.body.clientWidth;

		// content top offset
		var mcHeight = $('#wrapper').height();
		var tOffset = Math.round((h - mcHeight)/2);
		if(tOffset >= 10) $('#wrapper').css({'margin': tOffset + 'px 0 10px'});
		
		var pHeight = $('.popup').height() + 20;
		var pOffset = Math.round((h - pHeight)/2);
		if(pOffset >= 10) $('.popup').css({'top': pOffset + 'px'});
		//else $('#page').css({'margin':'0'});

		// draggable top offset
		//var mc = $("#content");
		//alert(topOffsetPosition);
		//var topOffsetPosition = mc.offset().top;
		//$('#draggable').css({'top':topOffsetPosition + 'px'});

		// cases top offcet
		//alert($("#cases").offset().top);
		//var topOffsetPosition = $("#cases").offset().top - $('#content').offset().top + 40;
		//$('#cases').css({'margin-top':'-' + topOffsetPosition + 'px'});
		
		
	
		var t0 = t.minWidth - t.maxWidth;
		var t1 = t.minWidth - w;
		var t2 = w - t.maxWidth;
		for (var selector in t.data) {
			var el = t.elements[selector];
			if (el.size() != 0) {
				var props = t.data[selector];
				for (var prop in props) {
					if (prop == 'background-position') {
						var pX = props[prop][0];
						var pY = props[prop][1];
						var valX = t.eval(w, t0, t1, t2, pX[0], pX[1]);
						var valY = t.eval(w, t0, t1, t2, pY[0], pY[1]);
						el.css(prop, valX + pX[2] + ' ' + valY + pY[2]);
					} else {
						var val = t.eval(w, t0, t1, t2, props[prop][0], props[prop][1]);
						el.css(prop, val + props[prop][2]);
					}
				}
			}
		}
	},	
	eval: function(w, t0, t1, t2, a, b) {
		var t = this;
	
		if (w <= t.minWidth) {
			x = a;
		} else if (w >= t.maxWidth) {
			x = b;
		} else {
			var x = Math.round((b*t1 + a*t2) / t0);
		}
		return x;
	}
}
