/*
if( navigator.userAgent.match(/Android/i) ||
 navigator.userAgent.match(/webOS/i) ||
 navigator.userAgent.match(/iPhone/i) ||
 navigator.userAgent.match(/iPod/i)
 ){
	//alert('mobile device detected');
} else {
	//alert('no mobile device detected');
}
*/
$(document).ajaxComplete(function(){
    try{
        FB.XFBML.parse(); 
        gapi.plusone.go();
    }catch(ex){}
});
$(document).ready(function() {
///[ start JQuery ]/////////////////////////////////
	var ratio = 900 / 1440;	
	//var ratio = 768 / 1024;
	var heightOffset = $('.bottom').height() + $('.scroller-wrapper').height();
	var width = $(window).width();
	var height = $(window).height() - heightOffset;
	//alert(heightOffset);
	//$('body').prepend('<div class="info"></div>');
	//$('.info').html(width+'/'+height); 
	
	jQuery.easing.def = 'easeInOutExpo';
	
	stretchBG();
	centerContent(); 
	//$('#intro').css({ 'margin-top' : '-250px' });
	
	//$('.home .mainNav').hide();
	
	$('#accordion .dealer-container').hide();		
	$('#accordion .dealer-container:first').show();
	$('#accordion h3:first').addClass('selected');
	$('#accordion h3').live('click', function() {
	
	$('#accordion h3').removeClass('selected');
		$(this).addClass('selected');
		$('#accordion h3').next().slideUp('slow');
		$(this).next().slideToggle('slow', function(){
			centerContentAnimated();
		})
		return false;
	});	



///[ POSTS ]/////////////////////////////////
$('#intro').css('top', (($(window).height()-106) - $('#intro').height())/2 );

$("#newsScroller").smoothDivScroll();

$(".jThumbnailScroller").thumbnailScroller({
	scrollerType:"clickButtons",
	//scrollerType:"hoverPrecise", 
	scrollerOrientation:"horizontal", 
	scrollEasing:"easeOutCirc", 
	scrollEasingAmount:800, 
	acceleration:4, 
	scrollSpeed:800, 
	noScrollCenterSpace:10, 
	autoScrolling:0, 
	autoScrollingSpeed:2000, 
	autoScrollingEasing:"easeInOutQuad", 
	autoScrollingDelay:500 
});


$(window).resize(function() {
		width = $(window).width();
		height = $(window).height() - heightOffset;
		divPos = (height-divHeight)/2.5;
		//$('.info').html(width+'/'+height); 
		centerContent();
		stretchBG();
		$('.prev-next-img a').css('height', height);
		$('#newsScroller').css('top', divPos);
		$('#intro').css('top', (height - $('#intro').height())/2 );
	});	

var postWidth = ($('.post-container').width()+40);
var countDiv = $('.post-container').length;
var divHeight = $('.scrollableArea').innerHeight();
var divPos = (height-divHeight)/2.5;
$('.scrollableArea').width(postWidth*countDiv);
$('#newsScroller').css('top', divPos);
//alert(divHeight);

///[ AJAX GALLERY ]/////////////////////////////////
    	
	var galleryId = $('.gallery-thumb:first').attr('data-gid');
	$('.gallery-thumb:first').addClass('selected');
	//alert('projects');
	getGallery(galleryId);


///[ *address* ]///

$.address.externalChange(function(event) {   
    var baseURL = $.address.baseURL();
    var galleryURL = $.address.pathNames();
    if( baseURL == 'http://www.feek.nl/projects' || 
    	baseURL == 'http://www.feek.nl/coated-foam' || 
    	baseURL == 'http://www.feek.nl/hard-plastix' ||
    	baseURL == 'http://www.feek.nl/soft-plastix' || 
    	baseURL == 'http://www.feek.nl/wallstix'){
    	if(galleryURL == ''){
    		var newLoc = '#/'+$('.gallery-thumb:first').attr('id');
    		window.location = newLoc;
    	} else {
    		galleryId = $('#'+galleryURL).attr('data-gid');
    		//alert(galleryId);
    		getGallery(galleryId);
    		$('.gallery-thumb').removeClass('selected');
    		$('#'+galleryURL).addClass('selected');
    	}
    }
});
	
	$('.scroller-wrapper').append('<div class="main-tooltip"></div>');
	
	$('.gallery-thumb').click(function(){
		galleryId = $(this).attr('data-gid');
		$('.gallery-thumb').removeClass('selected');
		$('.galleries').fadeOut(1000, function(){
			getGallery(galleryId);
		})
		$(this).addClass('selected');
		stretchBG();
	});

	//$('.gallery-tooltip').each(function(){
		//var tipWidth = $(this).width();
		//var tipHeight = $(this).height();
		//var xPos = (80-tipWidth) / 2;
		//var yPos = (60-tipHeight) / 2;
		//var yPos = '-20px'
		//$(this).css('left', xPos).css('top', yPos);
	//});
	$('.gallery-thumb').mousemove(function(e){
	   // e.pageX - gives you X position
	   // e.pageY - gives you Y position
	   var toolTipWidth = $('.main-tooltip').width();
	   xPos = e.pageX-(toolTipWidth/2);
		$('.main-tooltip').css('left', xPos);
	});
	
	$('.gallery-thumb').hover(function(){
		
		var toolTip = $(this).children('a').children('img').attr('alt');
		var xPos = $(this).position();
		$('.main-tooltip').html(toolTip).toggle();	
	})
	
///[ eo AJAX GALLERY ]/////////////////////////////////


///[ FUNCTIONS ]/////////////////////////////////
	
	function getGallery(galleryId) {
		$.ajax({
		type: 'POST',  
		data: ({gids : galleryId}),
			url: 'http://www.feek.nl/wp-content/themes/feek/nggallery/ajax-ngg.php',
			success: function(data) {
				$('.galleries').show();
				$('.galleries').html(data);
				$('.galleries').prepend('<div class="img-loader"></div>'); 	
				$('.img-loader').height(height+10);
					
				$('.description-pane').css({
					top: '-260px',
					opacity: 0
					});
				
				$('.galleries').prepend('<div class="prev-next-img"><a class="prev" href="#"><span></span></a><a class="next" href="#"><span></span></a></div>');
				$('.prev-next-img a').css('height', height);
				
				$('.galleries').waitForImages(function() {
	    			//alert('All images are loaded.');
	    			stretchBG();
					$('.img-loader').delay(100).fadeOut(function(){
						$('.img-loader').delay(0).remove();
					})
					
					var picArr = new Array();
					
					var n = 0;
					$('.galleries img').each(function(){
						picArr[n] = $(this).attr('id');
						n = n+1;
					});
					var arrLength = picArr.length;
										
					if(arrLength <= 1){
						$('.prev-next-img').remove();
					}
					
					if(arrLength > 1){
						var curPos = 0;
						var loopInterval = setInterval(function() {							
							var curImg = $('#'+picArr[curPos]).attr('id');
							if(curPos >= arrLength-1){ curPos = 0; }
							else { curPos = curPos+1; }
							getNextImage(curImg, arrLength, picArr, 3000)
							
						}, 5000);
					}
					
					$('.next').click(function(){
						curPos = curPos;
						clearInterval(loopInterval);						
						var curImg = $('#'+picArr[curPos]).attr('id');
						getNextImage(curImg, arrLength, picArr, 1000);
						if(curPos >= arrLength-1){ curPos = 0; }
						else { curPos = curPos+1; }
						return false;
					});
				
					$('.prev').click(function(){
						curPos = curPos;
						clearInterval(loopInterval);						
						var curImg = $('#'+picArr[curPos]).attr('id');
						//alert(curImg);
						getPreviousImage(curImg, arrLength, picArr, 1000);
						if(curPos <= 0){ curPos = arrLength-1; }
						else { curPos = curPos-1; }
						return false;
					});
					
					$('.galleries img').hide();
					$('.galleries img:first').delay(200).fadeIn('slow', function(){
						$('.description-pane').delay(200).animate({
							opacity: 1,
							top: '0'
					}, 1000, function() {
						// Animation complete.	
						
						});
					});
					
				});
			}
		});
	};

	function getNextImage(curImg, arrLength, picArr, speed) {	
		var lastPos = arrLength-1;
		var curPos = jQuery.inArray(curImg, picArr);
		if(curPos >= lastPos){
			var nextImg = $('#'+picArr[0]).attr('id');
		} else {
			var nextImg = $('#'+picArr[curPos+1]).attr('id');
		}
		$('#'+curImg).fadeOut(speed);
		//$('.galleries img').fadeOut(speed);
		$('#'+nextImg).fadeIn(speed);
	};
	function getPreviousImage(curImg, arrLength, picArr, speed) {	
		var lastPos = arrLength-1;
		var curPos = jQuery.inArray(curImg, picArr);
		if(curPos <= 0){
			var nextImg = $('#'+picArr[lastPos]).attr('id');
		} else {
			var nextImg = $('#'+picArr[curPos-1]).attr('id');
		}
		$('#'+curImg).fadeOut(speed);
		//$('.galleries img').fadeOut(speed);
		$('#'+nextImg).fadeIn(speed);
	};
	
    function stretchBG() {	
    	if ((height/width) > ratio){
		    $('.bg-img').height(height);
		    $('.bg-img').width(height / ratio);
		} else {
		    $('.bg-img').width(width);
		    $('.bg-img').height(width * ratio);
		}
		
		$('.bg-img').css('left', (width - $('.bg-img').width())/2);
		$('.bg-img').css('top', (height - $('.bg-img').height())/2);
	};    
	
	function centerNextPrev() {			
		$('.next-prev-img').css('height', height);
	};  
	
	function centerContent() {	
		$('.content').each(function(){
			var elementHeight = $(this).height();
			var windowHeight = $(window).height();
			var offset = ($('.bottom').height()) / 1.45;
			var y = ((windowHeight - elementHeight) / 2) - offset;
			$(this).css({
			  'top' : y
			});
		}); 
	};
	function centerContentAnimated() {	
		$('.content').each(function(){
			var elementHeight = $(this).height();
			var windowHeight = $(window).height();
			var offset = ($('.bottom').height()) / 1.45;
			var y = ((windowHeight - elementHeight) / 2) - offset;
			$(this).animate({
			    top: y
			  }, 200, function() {
			    // Animation complete.
			  });
		}); 
	};
///[ eo FUNCTIONS ]/////////////////////////////////


///[ eo JQuery ]/////////////////////////////////	
});
