$(document).ready(function() {
	
	// Varriables
	$bigImg 			= $('#gallery-outer_container #big-img');
	$big_container 		= $('#gallery-outer_container .bigCont');
	$visibleWindowCont 	= $(window);
	$easingTime 		= 300;
	$easingType 		= "easeOutCirc";
	$sizeBoolean		= true;
	globalData 			= null;
	$galleryMouseMoveEvent = null;
	// Functions

	function calculate(val) {
		ss += val;

		if (ss > 0) {
			ss = 0;
			return;
		}

		if (ss < ($("#gallery-outer_container .slider-inner ul li").length - 6) * (-1)) {
			ss = ($("#gallery-outer_container .slider-inner ul li").length - 6) * (-1);
			return;
		}

		$("#gallery-outer_container .slider-inner").stop()
				.animate({
					left : ss * 90
				}, {
					duration : 500
				});
	}
	
	var ss = 0;
	var selectedThumb = null;	
	function addChildren(imgArr) {
		
		removeAllChildren('#gallery-outer_container .img-cont');
		
		var old = null;
		$.each(imgArr, function(i, v) {
			var imgItem = '<li><img width="73" height="53" style="float:left" src="'
					+ v + '"/></li>';
			
			if(old != v){
				$('.img-cont').append(imgItem);
			}
			old = v;
		});

		$('#gallery-outer_container .img-cont li').each(
				function(i, v) {
					$(this).fadeOut(0);
					$(this).delay((i / imgArr.length) * 800)
							.fadeIn(1000);
				});

		$('#gallery-outer_container .img-cont li img').click(function() {

			$('#img-loader').fadeIn(10);
			var imgName = $(this).attr("src").replace('/73x53','');
			
			if(selectedThumb != null){
				selectedThumb.removeClass('active');
			}
			
			selectedThumb = $(this).parent();
			selectedThumb.addClass('active');
						
			$('.bigCont #big-img').stop(0, 1).fadeOut(0);
			$('.bigCont #big-img').attr('src',imgName);
			
			$visibleWindowCont.unbind('scroll',$galleryMouseMoveEvent);
		});		
		
		clickFirsItem('#gallery-outer_container .img-cont li img');
		
		resize();

	}
	
	$('.bigCont #big-img').load(function (){
		
		$(this).fadeIn("slow");
		$('#img-loader').fadeOut(10);
		
//		$(this).width('');
		$(this).width('100%');
		
		$big_container.css({
			top: '0px',
			left: '0px'
		});
		
									
//		if($(this).width()<800){					
//			$big_container.css({
//				left: ($(window).width() - $(this).width())/2,
//				top : ($(window).height() - $(this).height())/2 + 100
//			});
//			
//		}else{
//			$big_container.css({
//				top: '0px',
//				left: '0px'
//			});
//			$(this).width('100%');
//		}	
		
		$visibleWindowCont.mousemove($galleryMouseMoveEvent);
	});

	var selectedTab = null;	
	
	function addTab(tabArr) {

		removeAllChildren('#gallery-outer_container .tab-menu ul');

		$.each(tabArr, function(i, v) {
			var tabItem = '<li class="cufon-GothamMedium">' + v	+ '</li>';
			$('#gallery-outer_container .tab-menu ul').append(tabItem);			
		});
				
		$('#gallery-outer_container .tab-menu ul li').click(function() {
			var itemIndex = $(this).index() + 1;
			addChildren(globalData.images[itemIndex]);
						
			if(selectedTab != null){
				selectedTab.removeClass('active');
			}
			
			selectedTab = $(this);
			selectedTab.addClass('active');
			
		});
		
		clickFirsItem('#gallery-outer_container .tab-menu ul li');
		
		$('.tab-menu').navigation({
			slideButtons : '.menu-navigation',
			slideDiv : '.tab-menu-inner',
			easing : 'easeOutQuint',
			viewItem : 6
		});
				
		resize();

	}
	function removeAllChildren(obj) {
		$(obj).html('');
	}
	
	function resize() {

		ss = 0;
		var totalTabWidth = 0;

		$('#gallery-outer_container .tab-menu ul li').each(
				function(i, v) {
					totalTabWidth += $(this).width() + 25;
				});

		$('#gallery-outer_container .tab-menu ul').css('width',
				totalTabWidth + 5);

		var totalItem = $('#gallery-outer_container .slider-inner ul li').length;
		$('#gallery-outer_container .slider-inner').width(
				totalItem * 90);
		$('#gallery-outer_container .slider-inner').css('left',	0);

		if (totalItem < 6) {
			$('#gallery-outer_container .slider-inner').css('left',	((6 * 90) - (totalItem * 90) - 5) / 2);
		}

		$big_container.css('top', 0);
	}
	
	function openAnim(obj, show) {
				
		$('.bigCont').css({
			'display':'block',
			'opacity':1
		});		
		
		$(obj).slideDown(500);
		$('#gallery-outer_container .gallery-navigation').slideDown(600);
		
		$('body').css('overflow','hidden');
		
		document.onmousewheel = function(){ stopWheel(); }; /* IE7, IE8 */
		if(document.addEventListener){ /* Chrome, Safari, Firefox */
		    document.addEventListener('DOMMouseScroll', stopWheel, false);
		}
	}	
	 
	function stopWheel(e){
	    if(!e){ e = window.event; } /* IE7, IE8, Chrome, Safari */
	    if(e.preventDefault) { e.preventDefault(); } /* Chrome, Safari, Firefox */
	    e.returnValue = false; /* IE7, IE8 */
	}

	function closeAnim(obj, show) {
		$visibleWindowCont.unbind('scroll',$galleryMouseMoveEvent);
		$('#gallery-outer_container .bigCont').fadeOut(500);
		$(obj).delay(800).slideUp(500);
		$('#gallery-outer_container .gallery-navigation').delay(900).slideUp('200',function(){
			$('body').css('overflow-y','visible');
			$('.bigCont #big-img').attr('src','');
			removeAllChildren('#gallery-outer_container .tab-menu ul');
			removeAllChildren('#gallery-outer_container .img-cont');
			selectedTab = null;
		});	
		
		document.onmousewheel = null;  /* IE7, IE8 */
		if(document.addEventListener){ /* Chrome, Safari, Firefox */
		    document.removeEventListener('DOMMouseScroll', stopWheel, false);
		};

	}
	
	function isLarge_(cont, img) {
		return (img.height() > cont.height());
	}
	
	function clickFirsItem(obj) {
		$(obj + ':first').click();
	}

	// Triggers
	
	/*
	 * $('#gallery-outer_container .main-tab-cont
	 * li').click(function() { console.log( $(this).html()); });
	 */
	
	$('#gallery-outer_container .l-arrow').click(function() {
		calculate(-1);
	});

	$('#gallery-outer_container .r-arrow').click(function() {
		calculate(1);
	});

	$('#gallery-outer_container .close-gallery a').click(function() {
		closeAnim('#gallery-outer_container', true);
	});

	$('.rixosGalleryTrigger').click(function(){
		params = ($('26').val() ? $('26').val() : 0)+',0';
		
		if($(this).attr('rixosGallery'))
		{
			params = $(this).attr('rixosGallery');
		}
		
		params 	= params.split(',');
		owner 	= params[0];
		type 	= (params[1] != 0 ? params[1] : '' );
		$.getJSON('ajax/gallery?lang=tr&owner=26&type='+type,function(data) {
			globalData = data;
			addTab(data.tabs);
		});
		
		$(window).scrollTop(0);
		
		$('#gallery-outer_container').css({
			'top': $(window).scrollTop()
		});
		
		$('#gallery-outer_container .open-bg').height($(window).height());
		openAnim('#gallery-outer_container', true);
				
		return false;
	});
	
	$('body').keyup(function(e) {
		  if (e.keyCode == '13') {
		     
		   }
	});


	$galleryMouseMoveEvent = (function(e) {

		if (!e)	return false;

		$bigBoolean = isLarge_($visibleWindowCont,
				$big_container);

		$mouseX = e.pageX;
		$mouseY = e.pageY;

		$mouseOran = (1 / $visibleWindowCont.height())* e.pageY;

		if ($bigBoolean) {
			$big_container.stop().animate(
					{
						top : ($mouseOran * ($bigImg.height() - $(window).height()) * -1)
					}, $easingTime, $easingType);
		}

	});		
		
	$visibleWindowCont.resize(function(e) {
		resize();
	});

});
