V = {
	bultenBoxOpened : '',
	Slideshow : {
		current		: -1,
		changing	: false,
		init : function() {
			// default selected image
			if ($('#list-container .selected').length) {
				curr = $('#slideshow #list-container .selected').index();
				V.Slideshow.selectedImage(curr);
			}
			// bindings
			$('#slideshow #list-container a').bind('click',V.Slideshow.thumbClicked);
			$('#slideshow').bind('mouseenter',V.Slideshow.onEnter);
			$('#slideshow').bind('mouseleave',V.Slideshow.onLeave);
			$('#slideshow #prev-list-btn,#slideshow #next-list-btn').bind('click',V.Slideshow.navigate);
		},
		thumbClicked : function() {
			curr = $(this).index();
			if(V.Slideshow.current != curr && !V.Slideshow.changing) {
				V.Slideshow.selectedImage(curr);
				V.Slideshow.changing = true;
			}
			return false;
		},
		selectedImage : function(b) {
			
			isLoaded = false;
			obj = $('#list-container a').get(b);
			imagePath = $(obj).attr('rel');
			
			// LOADER ICON		
			setTimeout(function() { 
				if(!isLoaded) {
					$(obj).append('<div class="photo-loader"></div>'); 
				}
			}, 300);

			// LOADING IMG
			var img = new Image();
			$(img).load(function () {
				
				isLoaded = true;
				
				$('#slideshow .photo-loader').fadeOut(function(){
					$(this).remove();
				});
				
				$('#slideshow .photo-before').after('<div class="photo-after" style="background-image:url('+imagePath+')"></div>');
				if(b > V.Slideshow.current) {
					topPos = '500px';
				} else {
					topPos = '-500px';
				}
				
				$('.photo-after').css({ top:topPos });
				$('#slideshow .photo-after').animate(
					{ top: '0px' },
					700,
					'swing',
					function(){ 
						$('#slideshow .photo-before').remove(); 
						$('#slideshow .photo-after').removeClass('photo-after').addClass('photo-before'); 
						V.Slideshow.current = b;
						V.Slideshow.showPhotoID(b+1);
						$('#slideshow #list-container a.selected').removeClass('selected');
						$($('#slideshow #list-container a').get(b)).addClass('selected');
						V.Slideshow.changing = false;
					});
					
				$('#download-link').attr('href',imagePath);
				
			}).attr('src', imagePath);
		},
		showPhotoID : function (no) {
			$('#slideshow #current-photo-id').html((no < 10) ? '0'+no : no);
		},
		navigate : function () {
			direction = ($(this).attr('id') == 'prev-list-btn') ? 1 : -1;
			currentPos = (direction == 1) ? Math.ceil((parseInt($('#slideshow #list-container').css('top'))-15) / 80) : Math.floor((parseInt($('#slideshow #list-container').css('top'))-15) / 80);

			if (currentPos == 0 && direction == 1) return false;
			if (((currentPos*-1)+3) == $('#slideshow #list-container a').length && direction == -1) return false;
		
			nextTop = ((direction + currentPos) * 80 + 15) + 'px';
			$('#slideshow #list-container').stop().animate({ top: nextTop },500,'swing');
			
			return false;
		},
		onEnter : function() {
			$('#slideshow #navigation').stop().animate({ right: "0px" }, 500, 'swing');
		},
		onLeave : function() {
			$('#slideshow #navigation').stop().animate({ right: "-105px" }, 500, 'swing');
		} 
	},
	initMainSlideshow : function() {
	
		if ($('.section-0').length) {
			$('.mainSlideshow img:not(.active)').css({ opacity:0 });
			$('.mainSlideshow img.active').css({ opacity:1 });
			setInterval(V.showNextSlide,5000);
			V.showNextSlide();
		}	
	},
	showNextSlide : function() {
	
		var cur = $('.mainSlideshow img.active').index();
		var nextCur = (cur+1)%$('.mainSlideshow img').length;
		var oldImg = $($('.mainSlideshow img').get(cur));
		var newImg = $($('.mainSlideshow img').get(nextCur));
		newImg.addClass('fading').animate({opacity:1},4000,'swing',function() {
			oldImg.removeClass('active');
			oldImg.css({opacity:0});
			newImg.removeClass('fading');
			newImg.addClass('active');							
		});
		
	},
	showNextNews: function() {
		$('.mainNewsBox .news-next').click(function () {
			if (($('.mainNewsBox p').length-1)*-1*123 < parseInt($('.mainNewsBox p:first').css('margin-top')))
				$('.mainNewsBox p:first').animate({'margin-top': '-=123px'}, "slow");
			else
				$('.mainNewsBox p:first').animate({'margin-top': '0px'}, "slow");
		});
		$('.mainNewsBox .news-back').click(function () {
			if (0 > parseInt($('.mainNewsBox p:first').css('margin-top')))
				$('.mainNewsBox p:first').animate({'margin-top': '+=123px'}, "slow");
			else
				$('.mainNewsBox p:first').animate({'margin-top': '+='+(-123*($('.mainNewsBox p').length-1))+'px'}, "slow");
		});
	},
	resizeWindow : function(fast) {
		_duration : 900,
		fast = (fast == 1);
		var diff = $(window).height() - $('body').height();
		var mtop = parseInt($('.footerPos').css('marginTop'));

		if ((diff+mtop) >= 50) {
			mtop += diff;
		} else if (mtop != 50) {
			mtop = 50;
		}
		
		if (fast) {
			$('.footerPos').css('marginTop', mtop+27);
			$('footer').removeClass('hide-vis');
		}
		else {
			$('.footerPos').stop().animate({marginTop : mtop}, 900, 'swing');
		}
	},
	isiPhone : function() {
	    var agent = navigator.userAgent.toLowerCase();
	    if(agent.match(/iPhone/i)) {
			window.location = 'http://www.veneris.com.tr/main.php';
		} 
	} 
	
}

$(document).ready(function(){
	
	// Custom input texts
	$('.custom-title').customInputTitle();
	
	// Mainpage
	V.initMainSlideshow();
	V.showNextNews();
	
	// Slideshow
	V.Slideshow.init();	
	
	// Footer position
	V.resizeWindow(1); // check heights & position fast
	$(window).bind("resize", V.resizeWindow); // bind onResize
	
});

$(function () {
			
	$('#contactFormBtn').formValidator({
		scope		: '#contactForm',
		errorDiv	: '#errorDiv1'
	});
	
	$('#requestFormBtn').formValidator({
		scope		: '#requestForm',
		errorDiv	: '#errorDiv2'
	});
	
	if ($('#Div3D').length) {
		swfobject.embedSWF("3d_plans.swf", "Div3D", "755", "329", "9.0.0", "js/swfobject/expressInstall.swf");
	}
});
