// Mobo Studio Portfolio JS

// some global variables
var worksArr = new Array();
var worksLinks = new Array();
var currentWork = 0;
var intv;
var slideshowIntv = 15; // in seconds
var ie7;
var ie8;

// hashize urls
var siteUrl = "http://" + top.location.host.toString(),
	hash = window.location.hash.toLowerCase();


function clearTypeIE(target) {
	 if (ie7 || ie8)
		  target.style.removeAttribute('filter'); 
};
function hideScrollInfo() {
	$("#scrollformore").fadeOut();
	$(window).unbind("scroll");
};
function showScrollInfo() {
	if (($(worksArr[currentWork]).find(".img-cont img").size() > 1) && ($(window).height() > 800)) {
		$("#scrollformore").fadeIn();	
		$(window).bind("scroll", hideScrollInfo);
	} else {
		hideScrollInfo();
	};
};

function hashize() {
//	linkName = worksId[currentWork];
	linkName = worksArr[currentWork].children("h1").text();
	linkName = linkName.replace(/\s/g, "-").toLowerCase();
	
	window.location.hash = "#/" + linkName;
};

function goNext() {		  
		function nextImg() {
			hideScrollInfo();
			$(worksArr[currentWork]).animate({opacity: 0}, 400, function() {
				$(window).scrollTop(0);	
				$(this).css({
					 display: "none",
					 opacity: 1
				});
				if (currentWork == worksArr.length-1) {
					currentWork = 0;
					hashize();
					worksArr[currentWork].css('opacity', 1).fadeIn(function() {
																			   clearTypeIE(this);
																			   showScrollInfo();
																			   });
				} else {
					currentWork++;
					hashize();
					worksArr[currentWork].css('opacity', 1).fadeIn(function() {
																			   clearTypeIE(this);
																			   showScrollInfo();
																			   });
				};
			});
		};
		nextImg();
};

function goPrev() {
		function prevImg() {
			hideScrollInfo();
			$(worksArr[currentWork]).animate({opacity:0}, 400, function() {
				$(window).scrollTop(0);
				$(this).css({
					display: "none",
					opacity: 1
				});
				if (currentWork == 0) {
					currentWork = worksArr.length-1;
					hashize();
					worksArr[currentWork].css('opacity', 1).fadeIn(function() {
																			   clearTypeIE(this);
																			   showScrollInfo();
																			   });
				} else {
					currentWork--;
					hashize();
					worksArr[currentWork].css('opacity', 1).fadeIn(function() {
																			   clearTypeIE(this);
																			   showScrollInfo();
																			   });
				};
			});
		};
		prevImg();
};
		
function slideshow() {
	intv = setInterval(function(){ goNext() }, slideshowIntv*1000);
};

function showAbout() {
	$('#stripe').slideDown();
	$('#more-btn span.slidedown').removeClass('slidedown').addClass('slideup');
									  
	
};

function hideAbout() {
	$('#stripe').slideUp();
	$('#more-btn span.slideup').removeClass('slideup').addClass('slidedown');
};

function setArrowsActiveArea() {
	var windowWidth = $(window).width() ;
	if (windowWidth > 1164) {
		$("#next-cont, #prev-cont").width((windowWidth - 1040) / 2);
	} else {
		$("#next-cont, #prev-cont").width(0);
	};
};

function searchId(hashName) {
	for (i=0; i < worksLinks.length; i++) {
		if (hashName == ("#/" + worksLinks[i]) ) {
			return i;
		}; 
	};
	return 0;
};

$(document).ready(function(){
		/* if JS enabled hide presentations */						   
		$(".work-item").hide();	 						   
						   
		/* check for ie versions */
		if (jQuery.browser.msie && parseInt(jQuery.browser.version, 10) < 8 && parseInt(jQuery.browser.version, 10) > 4) {
			ie7 = true;
		} else if (jQuery.browser.msie && parseInt(jQuery.browser.version, 10) >= 8) {
			ie8 = true;
		}
		/* pngfix for ie */				   
		$(".work-item div img, .work-item h1").supersleight();
		
		/* showing first work and building Works Array */	
		$(".work-item").each(function(i) {
									  worksArr[i] = $(this);
									  worksLinks[i] = $(this).children("h1").text().replace(/\s/g, "-").toLowerCase();
									  });
		currentWork = searchId(hash);
		worksArr[currentWork].fadeIn(function() {clearTypeIE(this)});
		
		
		
		/* next/prev clicks and btns animation */
		$("#next, #next-cont").click(function() {
				$('.work-item').stop();								  
				clearInterval(intv);
				goNext();
				return false;
		});
		
		$("#prev, #prev-cont").click(function() {
				$('.work-item').stop();
				clearInterval(intv);				
				goPrev();
				return false;
		});		
		
		if (!ie7) {
			$("#next, #next-cont").hover(function() {
					$("#next").stop().animate({
								   right: 0
								   }, 300);
				}, function() {
					$("#next").stop().animate({
								   right: -10
								   }, 300);
				});
			$("#prev, #prev-cont").hover(function() {
					$("#prev").stop().animate({
								   left: 0
								   }, 300);
				}, function() {
					$("#prev").stop().animate({
								   left: -10
								   }, 300);
				});			
		}
		
		/* go with auto slideshow */
		slideshow();	
		
		/* show/hide About */
		$('#more-btn').toggle(function() {
				$('#stripe').stop();
				showAbout();
				clearInterval(intv);
			}, function () {
				$('#stripe').stop();				
				hideAbout();
				clearInterval(intv);
			}); 
		
		/* adjusting the #prev/#next active area */
		setArrowsActiveArea();
		$(window).resize(function() {
								  setArrowsActiveArea();
								  showScrollInfo();
								  });
									  
		/* inserting email address */
		$('.contact').attr('href', 'mailto:contact@mobostudio.com');
		

});
