// navigation
$(function() {
		var color = '#7E8183';
    var colorHover = '#fff';
    var duration = 444;

    var $nav = $('#nav');
    var $a = $nav.find('li a');
    var $current = $nav.find('li.current');
    var $magic = $('<div id="nav-magic"></div>');
		
		if ( !$current.size() ) return false;
		
    $magic.css({
        'width'           :$current.width()+'px',
        'height'          :$current.height()+'px',
        'position'        :'absolute',
        'left'            :$current.position().left+'px',
        'background-color':$current.find('a').css('background-color')
    })
    .data('cw', $current.width()+'px')
    .data('cl', $current.position().left+'px')
    .prependTo($nav);

    $nav.find('li:not(.current)').hover(function(){
        var $this = $(this);
        $current.find('a').css('color', color);
        $magic.stop().animate({
            'width':$this.width()+'px',
            'left' :$this.position().left+'px'
        }, {
            'complete':function() {
                $this.find('a').css('color', colorHover);
            },
            'duration':duration
        });
        
    }, function(){
        $a.css('color', color);
        $magic.stop().animate({
            'width':$magic.data('cw'),
            'left' :$magic.data('cl')
        }, {
            'complete':function() {
                $current.find('a').css('color', colorHover);
            },
            'duration':duration
        });
    });

    $nav.removeClass('js');
});

// slider
$(function() {
		$slider = $('#slider-content');
		if ( !$slider.size() ) return false;
		
    $slider.cycle({
        'pager':    '#slider-nav',
        'fx':       'fade',
        'timeout':  4000,           // milliseconds between slide transitions (0 to disable auto advance)
        'speed':    1000,           // speed of the transition (any valid fx speed value)
        'delay':    2000            // additional delay (in ms) for first transition (hint: can be negative)
    });
    $('#slider-nav a').each(function() {
        var $t = $(this);
        $t.html('<span>'+$t.text()+'</span>');
    });
});

// home page's quotes
$(function() {
		var $testimonial = $('#testimonials');
		if ( $testimonial.size() ) {
			$testimonial.quotator({
					'speed' : 5000,
					'json': "js/quotator_quotes.js"
			});
		}
});

// gallery page's flash
$(function() {
		var $gallery = $('#flash-gallery');
		if ( $gallery.size() ) {
			$gallery.media({
				'width':633,
				'height':458,
				'autoplay':true
			});
		}
});

// submit the form, and ajax send an email to bookings@domahbali.com
$(function() {
	
	var $form = $('#contact-form');
	if ( !$form.size() ) return false;
	
	// add a layer for show the notice
	$('body').append('<div id="notice-bar"></div>')
	$notice = $('#notice-bar');
	
	var noticeshow = function(html) {
		$notice.html(html).animate({height: '46px'}, 369);
		setTimeout(function() {
			$notice.animate({height:'0'}, 369, function() {
				$notice.hide();
			});
		}, 5000);
	};
	
	// do not autocomplete
	$form.attr('autocomplete', 'off');
	
	// get all the input
	var $input = [];
	$input['name'] = [];
	$input['name']['input'] = $form.find('input.name');
	$input['name']['item'] = $form.find('div.item-name');
	$input['email'] = [];
	$input['email']['input'] = $form.find('input.email');
	$input['email']['item'] = $form.find('div.item-email');
	$input['content'] = [];
	$input['content']['input'] = $form.find('textarea.content');
	$input['content']['item'] = $form.find('div.item-content');
	$input['all'] = $form.find('input, textarea');
	
	$form.submit(function() {
		$form.find('div.item-notice').removeClass('item-notice');
		$input['all'].attr('disabled', 'disabled');

		// ajax send email
		$.post('ajax.sendmail.php', {
			action: 'leavemessage',
			name: $input['name']['input'].val(),
			name_required: $input['name']['item'].hasClass('item-required'),
			email: $input['email']['input'].val(),
			email_required: $input['name']['item'].hasClass('item-required'),
			content: $input['content']['input'].val(),
			content_required: $input['content']['item'].hasClass('item-required')
		}, function(json) {
			$input['all'].removeAttr('disabled');
			
			if ( json == '' ) return false;
			var obj = eval('(' + json + ')');
			
			// mail success
			if ( obj.status == '1' ) {
				noticeshow('<p class="success">Thank you for your enquiry a represntative from d\'Omah will contact you shortly.</p>');
				$input['name']['input'].val('');
				$input['email']['input'].val('');
				$input['content']['input'].val('');
			}

			if ( obj.status == '-1' ) {
				if ( obj.error ) {
					// infomation incomplete
					for ( x in obj.error ) {
						$input[x]['item'].addClass('item-notice');
						$input[x]['item'].find('span.notice').text(obj.error[x]);
					}
				} else {
					// mail error
					noticeshow('<p class="fail">mail error, try again please.</p>');
				}
			}
		});
		
		return false;
	});
	
});