$(
	function(){
		
		if ($('#suas_map').length) {
			loadGoogleMaps();
		}
		
		if ($('#image-carousel-inner').length) {
			homeCarousel();
		}
		
		
		$(".tweets").tweet({
          join_text: "auto",
          username: "Suas_Bar",
          avatar_size: 48,
          count: 3,
          loading_text: "loading tweets..."
        });
		
		if ($('#large_suas_map').length) {
			loadLargeGoogleMap();
		}
				
		$("#gallery-column .gallery-images a, .image-gallery-column .image-gallery a").fancybox();
		
		if ($('form#callback-form').length) {
			$.post("/wp-content/themes/suas/ajax/antispam.php",{antispam: "true"},function(data){
				$('form#callback-form').append(data);
			});
			
		}
		
		$('input#cb_name').focus(
			function(){
				if ($('input#cb_name').val() == 'Name') {
					$('input#cb_name').val('');
				}
			}
		);
		$('input#cb_name').blur(
			function(){
				if ($('input#cb_name').val() == '') {
					$('input#cb_name').val('Name');
				}
			}
		);
		
		$('input#cb_email').focus(
			function(){
				if ($('input#cb_email').val() == 'Email') {
					$('input#cb_email').val('');
				}
			}
		);
		$('input#cb_email').blur(
			function(){
				if ($('input#cb_email').val() == '') {
					$('input#cb_email').val('Email');
				}
			}
		);
		
		$('input#cb_phone').focus(
			function(){
				if ($('input#cb_phone').val() == 'Daytime tel') {
					$('input#cb_phone').val('');
				}
			}
		);
		$('input#cb_phone').blur(
			function(){
				if ($('input#cb_phone').val() == '') {
					$('input#cb_phone').val('Daytime tel');
				}
			}
		);
		
		$('form#callback-form').submit(function(){
			
			// check the fields
			error_msg = "";
			if ((jQuery.trim($('#cb_name').val()) == 'Name') || (jQuery.trim($('#cb_name').val()) == '')) {
				error_msg += '- You must enter a Name\n';
			}
			
			
			if ((jQuery.trim($('#cb_email').val()) == 'Email') || (jQuery.trim($('#cb_email').val()) == '')) {
				error_msg += '- You must enter an Email address\n';
			}
			
			
			if ((jQuery.trim($('#cb_phone').val()) == 'Daytime tel') || (jQuery.trim($('#cb_phone').val()) == '')) {
				error_msg += '- You must enter a Daytime tel\n';
			}
			
			if (error_msg == '') {
				$.post(
					"/wp-content/themes/suas/ajax/callback.php",
					{ cb_name: $('#cb_name').val(), cb_email: $('#cb_email').val(), cb_phone: $('#cb_phone').val(), cb_antispam: $('#cb_antispam').val() },
			  		function(data){
			    		$("#callback").html(data);
			  		}
				);
			} else {
				alert(error_msg);
			}
			return false;
		});
		
	}
)
		
		
function loadGoogleMaps() {
	var latlng = new google.maps.LatLng(51.898247,-8.477191);
	var myOptions = {
	      zoom: 16,
	      center: latlng,
	      mapTypeId: google.maps.MapTypeId.ROADMAP
	    };
	var map = new google.maps.Map(document.getElementById("suas_map"), myOptions);
	var marker = new google.maps.Marker({
	        position: latlng,
	        map: map
	});
	
}

function loadLargeGoogleMap() {
	var latlng = new google.maps.LatLng(51.898247,-8.477191);
	var myOptions = {
	      zoom: 17,
	      center: latlng,
	      mapTypeId: google.maps.MapTypeId.ROADMAP
	    };
	var map = new google.maps.Map(document.getElementById("large_suas_map"), myOptions);
	var marker = new google.maps.Marker({
	        position: latlng,
	        map: map
	});
	
}





function homeCarousel() {
	jQuery('#image-carousel-inner').jcarousel({
        scroll: 1,
		initCallback: suascarousel_initCallback,
		itemVisibleInCallback: {
            onBeforeAnimation: suascarousel_itemVisibleInCallbackBeforeAnimation,
            onAfterAnimation:  suascarousel_itemVisibleInCallbackAfterAnimation
        },
		itemVisibleOutCallback: {onAfterAnimation: suascarousel_itemVisibleOutCallback},
        // This tells jCarousel NOT to autobuild prev/next buttons
        buttonNextHTML: null,
        buttonPrevHTML: null,
		auto: 4,
        wrap: 'circular'
    });
}

function suascarousel_initCallback(carousel){
	jQuery('.carousel-controls .last').bind('click', function() {
		carousel.startAuto(0);
		carousel.next();
		return false;
    });

    jQuery('.carousel-controls .first').bind('click', function() {
		carousel.startAuto(0);
        carousel.prev();
        return false;
    });

	carousel.clip.hover(function() {
        carousel.stopAuto();
    }, function() {
        carousel.startAuto();
    });
}


function suascarousel_itemVisibleOutCallback(carousel, item, i, state, evt)
{
    carousel.remove(i);
};


function suascarousel_itemVisibleInCallbackBeforeAnimation(carousel, item, i, state, evt){
	var idx = carousel.index(i, suascarousel_itemList.length);
    carousel.add(i, suascarousel_itemList[idx - 1]);
}

function suascarousel_itemVisibleInCallbackAfterAnimation(carousel, item, idx, state) {
	$('.carousel-controls .item').removeClass("active");
	var tmp = idx -1;
	var tmp_length = suascarousel_itemList.length
	while(tmp >= tmp_length) {
		tmp = tmp - tmp_length;
	}
	$('.carousel-controls .item:eq(' + tmp + ')').addClass("active");
}
