/* jTweetsAnywhere settings - enter your username. */
/*---------------------------------------------------------------------*/
var twitter_user_name = 'KitchenArtFL';


/* Fancybox settings */
/*---------------------------------------------------------------------*/
function fancybox_settings(){
    jQuery(".single_image, .iframe").fancybox({
        'transitionIn'	: 'elastic',
        'transitionOut'	: 'elastic',
        'easingIn'      : 'easeOutBack',
        'easingOut'     : 'easeInBack',
        'speedIn'		: 300, 
        'speedOut'		: 200,
        'titlePosition'	: 'inside',
        'overlayShow'	: true,
        'overlayColor'	: '#000',
        'overlayOpacity': 0.2
    });
}


/* Cufon Font replacement */
/*---------------------------------------------------------------------*/
Cufon.replace("h2", {fontFamily: "ChunkFive"});
Cufon.replace('#bottom_header, #top_footer p', {
    fontFamily: "Helsinki",
    textShadow:"0px 1px 1px rgba(0,0,0,0.5)"
});


/* Regular expression to validate email address */
/*---------------------------------------------------------------------*/
function validateEmail(emailValue){  
	var emailPattern = /^[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/;
	return emailPattern.test(emailValue); 
}


/* Validate Form fields */
/*---------------------------------------------------------------------*/
function validateForm(){
	var fields_valid = true;
    
    if (jQuery("input#name").val().length < 2) {
		jQuery("label#name_error").fadeIn('slow');
		jQuery("input#name").focus();
		fields_valid = false;
	}
	if (!validateEmail(jQuery("input#email").val())) {
		jQuery("label#email_error").fadeIn('slow');
		if(fields_valid) jQuery("input#email").focus();
		fields_valid = false;
    }
	return fields_valid;
}


/* Sliding... DIV: header_features, UL: header_buttons and IMG in .panel */
/*-----------------------------------------------------------------------*/
function change_slide(out_id, in_id){
    jQuery("ul#panel li:nth-child(" + out_id + ") .header_features").animate({ top: '+=300' }, {
                duration: 800,
                easing: 'easeInQuart',
                complete: function(){
                    jQuery(this).hide(0, function(){
                        jQuery("ul#panel li:nth-child(" + in_id + ") .header_features").show().animate({ top: '-=300' }, {
                            duration: 'slow',
                            easing: 'easeInQuart'
                        })
                    })
                }
            });
    jQuery("ul#panel li:nth-child(" + out_id + ") .header_buttons").animate({ left: '-=500' }, {
                duration: 500,
                easing: 'easeInBack',
                complete: function(){
                    jQuery(this).hide(0, function(){
                        jQuery("ul#panel li:nth-child(" + in_id + ") .header_buttons").show().animate({ left: '+=500' }, {
                            duration: 1200,
                            easing: 'easeInBack'
                        })
                    })
                }
            });
    jQuery("ul#panel li:nth-child(" + out_id + ") .header_image").animate({ right: '-=400' }, {
                    duration: 'slow',
                    easing: 'easeOutExpo',
                    complete: function(){
                        jQuery(this).hide(0, function(){
                        jQuery("ul#panel li:nth-child(" + in_id + ") .header_image").show().animate({ right: '+=400' }, {
                            duration: 'slow',
                            easing: 'easeOutExpo'
                        })
                    })
                    }
            });
    selection_change(out_id, in_id);
    return false;
};


/* Change Slider buttons selection */
/*-----------------------------------------------------------------------*/
function selection_change(out_id, in_id){
    jQuery("#slider_buttons li a.selected").removeClass('selected');
    var temp = in_id - 1;
    jQuery("#slider_buttons li").eq(temp).children('a').addClass('selected');
}


/* Auto rotate slides on every 7 secs */
/*-----------------------------------------------------------------------*/
function auto_rotate(){
    rotate = setInterval(function(){
        var out_id = jQuery("#slider_buttons li a.selected").attr("data-id");
        var in_id = parseInt(out_id) + 1;
        var last_slide_id = jQuery("#slider_buttons li:last a").attr("data-id");
        
        if(out_id == last_slide_id){ in_id = 1 };
        
        change_slide(out_id, in_id);
    }, 7000);
}


jQuery(function(){
    
    /* Call auto_rotate function on launch */
    /*-----------------------------------------------------------------------*/
    auto_rotate();
    
    
    /* jTweetsAnywhere */
    /*---------------------------------------------------------------------*/
	jQuery(".tweetsFeed").jTweetsAnywhere({
		username: twitter_user_name,
		count: 1
	});
    
    
    /* On Panel hover stop auto rotation of slides */
    /*---------------------------------------------------------------------*/
    jQuery("ul#panel").hover(
        function(){ clearInterval(rotate); }, 
        function(){ auto_rotate(); }
    );
    
    
    /* Move all slides (except first) at proper X, Y positions*/
    /*------------------------------------------------------------------------------------*/
    jQuery("ul#panel li.slide:not(:first) .header_features").css('top','300px').hide();
    jQuery("ul#panel li.slide:not(:first) .header_buttons").css('left','-465px').hide();
    jQuery("ul#panel li.slide:not(:first) .header_image").css('right','-365px').hide();
    
    
    /* Change slides on slider buttons click */
    /*---------------------------------------------------------------------*/
    jQuery("#slider_buttons li a").click(function(){
        clearInterval(rotate);
        var out_id = jQuery("#slider_buttons li a.selected").attr("data-id");
        var in_id = jQuery(this).attr("data-id");
        
        if(!jQuery(this).hasClass("selected") && !jQuery("ul#panel li .header_buttons").is(':animated')){
            change_slide(out_id, in_id);
        }
        auto_rotate();
        return false;
    })
    
    
    /* Move up Android and iPhone icons */
    /*---------------------------------------------------------------------*/
    jQuery(".buy_now_android, .buy_now_iphone, .try_it_free").hover(
        function () {
            jQuery(".move_up").not(':animated').animate({top: '-2px'}, 100);
        }, 
        function () {
            jQuery(".move_up").animate({top: '0px'}, 100);
        }
    );
    
    
    /* Calling Fancybox settings function */
    /*---------------------------------------------------------------------*/
    fancybox_settings();
    
    
    /* Screenshots Carousel */
    /*---------------------------------------------------------------------*/
    var carousel_length = jQuery(".carousel ul").children().length * 174;
    jQuery(".carousel ul").css('width', carousel_length);
    
    jQuery(".carousel").hover(
        function () {
            jQuery(".carousel_btn_left").not(':animated').show();
            jQuery(".carousel_btn_right").not(':animated').show();
        }, 
        function () {
            jQuery(".carousel_btn_left").not(':animated').hide();
            jQuery(".carousel_btn_right").not(':animated').hide();
        }
    );
    
    jQuery(".carousel_btn_left").click(function(){
        var ul_position = jQuery(".carousel ul").position();
        
        if(ul_position.left < 0){
            jQuery(".carousel ul").not(':animated').animate({ left: '+=174' }, {
                            duration: 'slow',
                            easing: 'easeOutExpo'});
        }
        return false;
    });
    
    jQuery(".carousel_btn_right").click(function(){
        var ul_position = jQuery(".carousel ul").position();
        var ul_width = jQuery(".carousel ul").width();
        var count_position = ul_width - (-ul_position.left) - 870;
        
        if(count_position > 0){
            jQuery(".carousel ul").not(':animated').animate({ left: '-=174' }, {
                            duration: 'slow',
                            easing: 'easeOutExpo'});
        }
        return false;
    });
    
    
    /* Input box placeholder */
    /*---------------------------------------------------------------------*/
    jQuery(".placeholder.name, input#name").bind("click keyup", function(){
        jQuery("input#name").focus();
        jQuery(".placeholder.name").not(':animated').fadeOut('fast');
    });
    
    jQuery(".placeholder.email, input#email").bind("click keyup", function(){
        jQuery("input#email").focus();
        jQuery(".placeholder.email").not(':animated').fadeOut('fast');
    });
    
    
    /* Contact Form input boxes on fly validation */
    /*---------------------------------------------------------------------*/
    jQuery(".error").hide();

	jQuery("input#name").bind("keyup focusout", function(){
		if(jQuery(this).val().length > 1){
			jQuery("label#name_error").not(':animated').fadeOut('fast');
        } else if(jQuery(this).val()){
			jQuery("label#name_error").fadeIn('slow');
        } else {
            jQuery(".placeholder.name").fadeIn('slow');
        }
	});
    
    jQuery("input#email").bind("keyup focusout", function(){
		if(validateEmail(jQuery(this).val())){
			jQuery("label#email_error").not(':animated').fadeOut('fast');
        } else if(jQuery(this).val()){
			jQuery("label#email_error").fadeIn('slow'); 
        } else {
            jQuery(".placeholder.email").fadeIn('slow');
        }
	});
    
    
    /* Submitting Contact Form using AJAX */
    /*---------------------------------------------------------------------*/
	jQuery("form#subscribe").submit(function() {
        
		var dataString = jQuery(this).serialize();
        
		if(validateForm()){
			jQuery.ajax({
			type: "POST",
			url: "php/subscribe.php",
			data: dataString,
			success: function() {
				jQuery('#subscribe').slideUp('slow', function(){
                    jQuery(this).html("<div id='confirmation'></div>");
                    jQuery('#confirmation').html("<h2>Thank You for subscription</h2>")
				    .append("<h3>Request Submitted Successfully!</h3>")
                    .append("<p>You will receive a great newsletters directly in your mailbox. Your Email address will not be published anywhere, and of course, you can unsubscribe at any moment.</p>")
                    .append("<p>We will be in touch soon...</p>");
					Cufon.refresh();
                    jQuery(this).slideDown('slow');
                })
			}
			});
		}
		return false;
	});
    
    
    /* Avatar Tip */
    /*---------------------------------------------------------------------*/
    jQuery('.tip_trigger').each(function() {        
        var tip = jQuery(this).find('.avatar_tip');

        jQuery(this).hover(
            function() { tip.not(':animated').fadeIn(); },
            function() { tip.hide(); }
        ).mousemove(function(e) {
            var avatar_x = e.pageX + 15,
                avatar_y = e.pageY + 15,
                avatar_width = tip.width(),
                avatar_height = tip.height(),
                distance_x = jQuery(window).width() - (avatar_x + avatar_width),
                distance_y = jQuery(window).height() - (avatar_y + avatar_height);

            if ( distance_x < 15 ) avatar_x = e.pageX - avatar_width - 15;
            if ( distance_y < 15 ) avatar_y = e.pageY - avatar_height - 15;

            tip.css({ left: avatar_x, top: avatar_y });
        });         
    });
})
