$(function () {
    initLinks();
    initInputs();
    initFooter();
    initDefaultInputTxt();
    initDialog();
    initSlider();
    initCapslide();
     
    
});


var initInputs = function () {
 $('input[type=text]').each( function(i) {
		var t = $(this);
		var thisval = t.val();
		t.blur( function() {
			if (t.val() == '') t.val(thisval);
		});
		t.focus( function() {
			if (t.val() == thisval) t.val('');
		});
	}); 

}

var initLinks = function () {
    $('body').delegate('a', 'click', function (e) {
        switch (this.rel) {
        case 'external':
            e.preventDefault();
            window.open(this.href);
            break;
        } 
    });
}

var initFooter = function () {

    $('.fadeThis').hover(function () {
        var fade = $('> span', this);
        fade.stop().fadeTo(500, 1);
    }, function () {
        var fade = $('> span', this);
        fade.fadeOut(500);

    });
}



var initDefaultInputTxt = function () {

    $('.defaultTxt').each(function () {
        var default_value = $(this).val();
        $(this).focus(function () {
            if ($(this).val() == default_value) $(this).val('');
        });
        $(this).blur(function () {
            if ($(this).val() == '') $(this).val(default_value);
        });
    });

}

var initDialog = function () {
    $('body').delegate('a.dialog', 'click', function (e) {
        e.preventDefault();
        scrollTo(0,0);
        
        var requestUrl = $(this).attr('href');
        $('#dialog').load(requestUrl + ' div[id^="dialog"]', initForm);
       
        $('#dialog').dialog({
            modal: true,
            draggable: false,
            resizable: false,
            title: $(this).attr('title'),
            width: '800',
            overlay: {
                opacity: 0.8,
                background: 'white'
            },
            close: function(){
              $('#dialog > div').remove();
            }

        });


    });


}


var initForm = function () {
        var formID = 'contactform';    
        var ajaxResponse = $('#'+formID+'').find('.response');
        var formAction = $('#'+formID+'').attr('action');

            $('#'+formID).validate({
                rules: {
                    Type_of_Service: 'required',
                    How_did_they_hear_about_us: 'required',
                    Name: 'required',
                    HumanValidation: 'required',
                    Email: {
                        required: true,
                        email: true
                    }
                },
                messages: {
                    Type_of_Service: 'Please tell us what type of service you\'re interested in.',
                    How_did_they_hear_about_us: 'Please tell us where you heard about Makibie.',
                    Name: '',
                    Email: '',
                    HumanValidation: ''
                },
                submitHandler: function (form) {
                 ajaxResponse.hide();
                   var dataString = $('#'+formID+'').serialize();   
                    $.ajax({
                        type: 'POST',
                        cache: false,
                        url: formAction,
                        data: dataString,
                        dataType: 'json',
                        beforeSend: function () {
                            $('#' + formID + ' .spinner').show();
                            $('#' + formID + ' .submit-btn').hide();
                        },
                        success: function (data) {
                            $('#' + formID + ' .spinner').hide();
                            $('#' + formID + ' .submit-btn').show();

                            ajaxResponse.html('<p class="' + data.STATUS + '">' + data.MESSAGE + '</p>').fadeIn();

                            if (data.STATUS == 'success') {
                                $('#' + formID + ' table').hide();
                            }

                        }
                    });
                 }

            });
}

var initSlider = function () {


    $('.paging').show();
    $('.paging a:first').addClass('active');

    var imageWidth = $('.window').width();
    var imagesum = $('.image-reel .slide-container').size();
    var imageReelWidth = imageWidth * imagesum;

    $('.image-reel').css({
        'width': imageReelWidth
    });

    rotate = function () {
        var triggerID = $active.attr('rel') - 1;
        var image_reelPosition = triggerID * imageWidth;
        $('.paging a').removeClass('active'); 
        $active.addClass('active'); 
        $('.image-reel').animate({
            left: -image_reelPosition
        }, 1500, 'easeInOutQuint');

    };

    rotateSwitch = function () {
        play = setInterval(function () { 
            $active = $('.paging a.active').next();
            if ($active.length === 0) { 
                $active = $('.paging a:first');             }
            rotate();
        }, 7000);
    };

    rotateSwitch();

    $('.image-reel a').hover(function () {
        clearInterval(play);
    }, function () {
        rotateSwitch();
    });

    $('.paging a').click(function (e) {
        e.preventDefault();
        $active = $(this); 
        clearInterval(play); 
        rotate();
        rotateSwitch();
     });

}


var initCapslide = function(){



 $("#capslide-img-cont-1").capslide({
      caption_color	: '#fff',
      overlay_bgcolor : '#000',
      showcaption	    : false
  });
   $("#capslide-img-cont-2").capslide({
      caption_color	: '#fff',
      overlay_bgcolor : '#000',
      showcaption	    : false
  });
   $("#capslide-img-cont-3").capslide({
      caption_color	: '#fff',
      overlay_bgcolor : '#000',
      showcaption	    : false
  });
}


