// slide menu
function initSlideMenu(){
	$('.case-holder').each(function(){
		var list_h1 = $(this).find('.card-menu-holder').children();
		var btn_prev = $(this).find('.prev > a');
		var btn_next = $(this).find('.next > a');
		var _step1 = list_h1.parent().width();
		var list_w1 = list_h1.children().length*list_h1.children().outerWidth();

		var list_h2 = $('.card-holder > div');
		if(list_h2.length == 0) list_h2 = false;
		if(list_h2){
			var _step2 = list_h2.parent().width();
			var list_w2 = list_h2.children().length*list_h2.children().outerWidth();
		}
		btn_prev.click(function(){
			moveList(false);
			return false;
		});
		btn_next.click(function(){
			moveList(true);
			return false;
		});
		var _m1 = 0;
		var _m2 = 0;
		function moveList(_f){
			if(_f){
				if(_m1 + _step1 <= list_w1 - _step1){
					_m1 += _step1;
					if(list_h2) _m2 += _step2;
				}
				else{
					_m1 = 0;
					if(list_h2) _m2 = 0;
				}
			}
			else{
				if(_m1 - _step1 >= 0){
					_m1 -= _step1;
					if(list_h2) _m2 -= _step2;
				}
				else{
					_m1 = list_w1 - _step1;
					if(list_h2) _m2 = list_w2 - _step2;
				}
			}
			list_h1.animate({marginLeft: -_m1}, {queue:false, duration:600});
			if(list_h2) list_h2.animate({marginLeft: -_m2}, {queue:false, duration:600});
		}
	});

	// resize menu to fit content
	$('.case-holder').each(function(){
		var _holder = $(this);
		var _cardHolder = _holder.find('.card-menu-holder');
		var _lists = _cardHolder.find('ul');

		_lists.each(function(){
			var _list = $(this);
			var _lWidth = _list.width() - 10;
			var _items = _list.find('li').css('padding',0);
			var _iCount = _items.length;
			var _iWidth = 0;
			_items.each(function(){_iWidth+=$(this).innerWidth(false);});

			var _dist = Math.floor((_lWidth - _iWidth)/(_iCount-1));
			_items.css('padding-right',_dist);
			_items.eq(_iCount-1).css('padding-right',0);
		});
	});
}

// content slide function
function initContentSlide() {
	$('.slide-controller').each(function(){
		var _this = $(this);
		var slist = $('.cases-column-box').children();
		var btn_prev = $(this).find('.back > a');
		var btn_next = $(this).find('.next > a');

		var _sstep = slist.parent().width();
		var list_width = slist.children().length*slist.children().outerWidth();

		btn_prev.click(function(){
			switchLinkState();
			moveSlideset(false);
			return false;
		});
		btn_next.click(function(){
			switchLinkState();
			moveSlideset(true);
			return false;
		});
		// switch toggle links function
		function switchLinkState() {
			var l1 = _this.find(".back");
			var l2 = _this.find(".next");
			if(l1.hasClass("active")) {
				l1.removeClass("active");
				l2.addClass("active");
			}
			else {
				l2.removeClass("active");
				l1.addClass("active");
			}
		}		
		// slide function
 		var _curoffset = 0;
		function moveSlideset(_f){
			if(_f){
				if(_curoffset + _sstep <= list_width - _sstep){
					_curoffset += _sstep;
				}
				else{
					_curoffset = 0;
				}
			}
			else{
				if(_curoffset - _sstep >= 0){
					_curoffset -= _sstep;
				}
				else{
					_curoffset = list_width - _sstep;
				}
			}
			slist.animate({marginLeft: -_curoffset}, {queue:false, duration:600});
		}
	});
}

// init infoblocks slide
function initInfoBlocks() {
	$("div.boxes-block").each(function(){
		var _this = $(this);
		var _step = _this.parent().width();
		var _box1 = _this.find(".boxes-text").eq(0);
		var _box2 = _this.find(".boxes-text").eq(1);
		var _box1height = _box1.outerHeight();
		var _box2height = _box2.css("display","none").outerHeight();
		var _maxheight = (_box1height > _box2height ? _box1height : _box2height);
		var _minheight = (_box1height < _box2height ? _box1height : _box2height);
		var _slideSpeed = 500;

		_this.find("a.slide-left").each(function(){
			$(this).click(function(){
				_this.stop().show().animate({height:_maxheight}, _slideSpeed/2, false, function(){
					_box1.css("display","block");
					_box2.css("display","block");
					$(this).animate({marginLeft: -_step}, _slideSpeed, false, function(){
						// post actions
					});
				});

				return false;
			});
		});

		_this.find("a.slide-right").each(function(){
			$(this).click(function(){
				_this.stop().show().animate({marginLeft: 0}, _slideSpeed, false, function(){
					_box1.css("display","block");
					_box2.css("display","block");
					//$(this).animate({height:_minheight}, _slideSpeed, false, function(){
					$(this).animate({height:_maxheight}, _slideSpeed, false, function(){
						// post actions
					});
				});

				return false;
			});
		});

	})
}

// homepage gallery
function initHomeGallery() {
	// latest posts url
	/*var _casesurl = '_posts.html';
	var _ajaxdata = '';
	$.ajax({
		type: "GET",
		url: _casesurl,
		success: function(msg){*/
			// recieve msg
			//_ajaxdata = msg;
			//$(".gallery .slides-holder").eq(0).html(_ajaxdata);
			// set maxheight
			var _maxheight = 0;
			$('.slides-holder div.card').each(function(){
				if($(this).height()>_maxheight) _maxheight = $(this).height();
			});
			$(".gallery").css("height",_maxheight);
			// init slideshow
			$('.gallery').slideShow({
				slideEl:'.slides-holder div.card',
				linkNext:'a.next',
				linkPrev:'a.prev',
				autoSlideShow:true,
				switchTime:2000,
				duration:650
			});
	//	}
	//});
}

// photo area fader
function initFaderList(){
	jQuery("ul.fader-list").each(function()
	{
		var _i = 0;
		var _this = jQuery(this);
		jQuery(this).find("li").css("opacity", "0").eq(_i).css("opacity", "1");
		setTimeout(function() {
			changeEl(_this, _i );
		},2000);
	});
}

function changeEl(_obj, c) {
	jQuery(_obj).find("li").eq(c).css("zIndex","0").animate({opacity: 0}, 500);
	if (c<jQuery(_obj).find("li").length-1) c++;
	else c=0;

	jQuery(_obj).find("li").eq(c).css("zIndex","1").animate({opacity: 1}, 500);

	setTimeout(function() {
		changeEl(_obj, c );
	},2000);
}

// layout fix function
function fixLayout() {
	var content = document.getElementById("main");
	if(content) {
		content.style.minHeight = "0";
		content.style.height = "auto";

		var ch = content.offsetHeight;
		var mainHeight = document.documentElement.clientHeight;
		if (mainHeight > ch )
		{
			// if IE6
			if (document.uniqueID && document.compatMode && !window.XMLHttpRequest) {
				content.style.overflow = "visible";
				content.style.height = mainHeight - 160 + "px";
			}
			// other browsers
			else {
				content.style.minHeight = mainHeight - 160 + "px";
			}
		}
	}
}
function initLayout() {
	fixLayout();
	if (window.addEventListener)
		window.addEventListener("resize", fixLayout, false);
	else if (window.attachEvent)
		window.attachEvent("onresize", fixLayout);
}

// page init code
function initPage() {
	initLayout();
	initFaderList();
	initHomeGallery();
	initSlideMenu();
	initContentSlide();
	initInfoBlocks();
}

if (window.addEventListener) window.addEventListener("load", initPage, false);
else if (window.attachEvent) window.attachEvent("onload", initPage);