// document ready
$(function() {
	
	// blinking fields
	$('input[type="text"], textarea')
		.focus(function() { if(this.title==this.value) { this.value = ''; } })
		.blur(function(){ if(this.value=='') {this.value = this.title; } });
		
	var s_slides;
	if($('body').hasClass('tr')){
		s_slides = [
			{ image: 'css/images/image01.jpg' },
 	 		{ image: 'css/images/image02.jpg' }
		] 
	}else{
		s_slides = [
			{ image: '../css/images/image01.jpg' },
 	 		{ image: '../css/images/image02.jpg' }
		] 
	}

	// fullscreen slideshow
	if($('body').hasClass("home")){
		$.supersized({
	 	 	slideshow: 1,
	 	 	autoplay: 1,
	 	 	stop_loop: 0,
	 	 	random: 0,
	 	 	slide_interval: 5000,
	 	 	transition: 1,
	 	 	transition_speed: 900,
	 	 	vertical_center: 0,
	 	 	horizontal_center: 1,
	 	 	fit_always: 0,
	 	 	thumb_links: 0,
	 	 	thumbnail_navigation: 0,
	 	 	fit_portrait: 1,	
	 	 	fit_landscape: 0,
	 	 	image_protect: 0,
	 	 	slide_links: 'empty',
	 	 	keyboard_nav: 0,
	 	 	slides: s_slides
	 	 });
	}
 	 
 	 // home slideshow
 	 $('.homeslider .holder > ul').jcarousel({
 	 	auto: 6,
 	 	animation: 0,
 	 	scroll: 1,
 	 	wrap: 'both',
 	 	buttonPrevHTML: null,
 	 	buttonNextHTML: null,
 	 	initCallback: function(carousel){
 	 		$('.homeslider .nav a').click(function(){
 	 			carousel.scroll( parseInt($(this).index()) + 1);
 	 			return false;
 	 		})
 	 	},
 	 	itemFirstInCallback: function(carousel, li, idx){
 	 		carousel.container.find('.jcarousel-item').css('opacity', 0);
 	 		carousel.container.find('.jcarousel-item ul').css('margin-left', '180px');
 	 		$(li).animate({ opacity: 1 }, 500);
 	 		$('ul', li).animate({ 'margin-left': '205px'}, 600);
 	 		$('.homeslider .nav a').removeClass('active').eq(idx-1).addClass('active');
 	 	}
 	 });
 	 
 	 // add some table classes
 	 $('.detailed-reasons table').each(function(){
 	 	$('tr:even').addClass('even');
 	 });
 	 
 	 // change panels
 	 $('.detailed-reasons .next').click(function(){
 	 	$(this).parents('.panel').hide().next().show();
 	 	return false;
 	 });
 	 
 	  $('.detailed-reasons .prev').click(function(){
 	 	$(this).parents('.panel').hide().prev().show();
 	 	return false;
 	 });
 	 
 	 $('.homeslider .details').click(function(){
 	 	$('#main, #supersized').hide();
 	 	$('.detailed-reasons').fadeIn();
 	 	return false;
 	 });
 	 
 	 $('.detailed-reasons .aside .close').click(function(){
 	 	$('.detailed-reasons').hide();
 	 	$('#main, #supersized').fadeIn();
 	 	return false;
 	 });
 	 
 	 // small fader
 	$('.small-fader img').eq(0).addClass('current');
 	fader();
 	
 	$('.consultants').jScrollPane({
 		showArrows: true,
 		autoReinitialise: true
 	});
 	$('.investor .scrollable').jScrollPane({
 		showArrows: true,
 		autoReinitialise: true,
 		contentWidth: '655px'
 	});
 	
 	// show full post
 	$('.news .posts-list a.details').click(function(){
 		var idx = $(this).parents('li').index();
 		$('.posts-list').hide();
 		$('.full-posts li').eq(idx).show();
 		$('.aside').show();
 		return false;
 	});
 	
 	$('.close-post').click(function(){
 		$('.aside').hide();
 		$('.full-posts li').hide();
 		$('.posts-list').show();
 		return false;
 	});
 	
 	// open features details
 	$('.open-fdetails').click(function(){
 		$('#main').hide();
 		$('.features-details').show();
 		return false;
 	});
 	
 	$('.features-details .close').click(function(){
 		$('.features-details').hide();
 		$('#main').show();
 		return false;
 	});
 	
 	// show big images
 	$('.capacity .images .image a').click(function(){
 		var href = $(this).attr('href');
 		$('.capacity').hide();
 		$(href).show();
 		return false;
 	});
 	
 	$('.capacity-big-image .close').click(function(){
 		$('.capacity-big-image').hide();
 		$('.capacity').show();
 		return false;
 	});
 	
 	// plan map hover
 	$('.plan-map a').hover(function(){
 		var idx = parseInt($(this).index()) + 1;
 		$('.plan-map').css('background-position', '0 ' + -1*idx*265 +'px');
 	},function(){
 		$('.plan-map').attr('style', '');
 	});
 	
 	// open location map
 	$('.location .thumb').click(function(){
 		var href = $(this).attr('href');
 		$('.location, .main-bottom').hide();
 		$(href).show();
 		return false;
 	});
 	
 	$('.loc-popup .close').click(function(){
 		$('.loc-popup').hide();
 		$('.location, .main-bottom').show();
 		return false;
 	});
 	
 	// gallery sliders
 	$('.gallery .thumbs').jcarousel({
 		auto: 0,
 		animation: 0,
 		scroll: 1,
 		buttonPrevHTML: null,
 		buttonNextHTML: null,
 		initCallback: function(carousel){
 			$('span.total').text(parseInt(carousel.container.find('li').length));
 			$('.controls a').click(function(){
 				if( $(this).hasClass('disabled') ){
 					return false;
 				}
 			})
 			$('.controls .prev').click(function(){
 				carousel.prev();
 				return false;
 			})
 			$('.controls .next').click(function(){
 				carousel.next();
 				return false;
 			});
 			
 			$('.controls .first').click(function(){
 				carousel.scroll(1);
 				return false;
 			})
 			$('.controls .last').click(function(){
 				carousel.scroll(parseInt(carousel.container.find('li').length));
 				return false;
 			})
 		},
 		itemFirstInCallback: function(carousel, li, idx){
 			$('span.current').text(idx);
 			if( parseInt(idx) == 1){
 				$('.controls .first, .controls .prev').addClass('disabled');
 				$('.controls .last, .controls .next').removeClass('disabled');
 			}
 			else if ( parseInt(idx) == parseInt(carousel.container.find('li').length) ){
 				$('.controls .last, .controls .next').addClass('disabled');
 				$('.controls .first, .controls .prev').removeClass('disabled');
 			}else{
 				$('.controls a').removeClass('disabled');
 			}
 		}
 	})
 	
 	$('.gallery .main img').css('opacity', 0).parent().find('.current').css('opacity', 1);
 	
 	$('.gallery .thumbs ul a').click(function(){
		if( $('.main img').is('animated') ){
 			return false;
 		}
 		$('.gallery .thumbs ul a').removeClass('active');
 		$(this).addClass('active');
 		var idx = parseInt($(this).index()) + (parseInt($(this).parents('li').index())*4) 
 		$('.main .current').animate({ 'z-index': 1, 'opacity': 0}, 500, function(){ $(this).removeClass('current') });
 		$('.main img').eq(idx).animate({ 'z-index': 2, 'opacity': 1}, 500, function(){ $(this).addClass('current') })
 		return false;
 	});
 	
 	// show map
 	$('.loc-popup .directions .btn').click(function(){
 		var src = $(this).attr('href');
 		$('.loc-popup .map-holder').html('').append('<img src="' + src + '" alt="" /><p style="width: 100%; text-align: center; font-size: 14px; color: #fff; padding-top: 20px;">Loading map...</p>');
 		$('.loc-popup .map-holder img').hide().load(function(){
 			$(this).fadeIn();
 			$('.loc-popup .map-holder p').remove();
 		})
 		return false;
 	})
	
});

function fader() {
	setTimeout(function() {
		var current = $('.small-fader img.current');
		var next = undefined;
		current.next().length ? next = current.next() : next = current.parent().find(':first');
		current.removeClass('current').fadeOut(500);
		next.addClass('current').fadeIn(500);
		fader();
	}, 1500);
}

