/**
 * @Copyright CSSJockey - Web Design and Development
 * @Website: http://www.cssjockey.com
 * @Terms of Use: http://www.cssjockey.com/terms-of-use
 * If you change the contents below sky will fall on your head!
 */
$(document).ready(function(){
	
	$('a, .ttip, abbr, .entry img').tooltip({ 
	   track: true, 
		delay: 0, 
		showURL: false, 
		showBody: " # ", 
		fade: 250 
	});

//Homepage portfolio cycle plugin
	$('#homeportfolio') 
	.before('<div id="pnav">') 
	.cycle({ 
	    fx:     'fade', 
	    speed:  500, 
	    timeout: 3000, 
	    pager:  '#pnav' 
	});
	$("#pnav a").html('&nbsp;');

$(".cjgallery").cycle({ 
    fx:     'fade', 
    speed:  '500', 
    timeout: 0, 
    next:   'a.gnext', 
    prev:   'a.gprev' 
});

// Survey Questions
$('.survey').cycle({ 
    fx:     'fade', 
    speed:  '1000', 
    timeout: 0, 
    next:   'a#snext', 
    prev:   'a#sprev' 
});
	
//Post Screenshots Cycle
$('.cycle').cycle({
    fx: 'fade',
    speed: 500,
    timeout: 3000
});


$("#testimonials").cycle({
    fx: 'fade',
    speed: 200,
    timeout: 10000,
    next:   '#tnext', 
    prev:   '#tprev' 
		
})

//Homepage Form
$('.number').val(0);
$(".checkbox input[type='checkbox']").click(function(){
	if($(this).attr("checked") ==  true){
		$(this).parent().parent().find('.number').val(1);	
	}else{
		$(this).parent().parent().find('.number').val(0);
	}    
});


	$(".plus").click(function(){
		var oldValue = $(this).parent().find('.number').val();
		var newValue = parseFloat(oldValue) + 1;
		$(this).parent().find('.number').val(newValue);
	})
	$(".minus").click(function(){
		if($(this).parent().find('.number').val() > 0){
			var oldValue = $(this).parent().find('.number').val();
			var newValue = parseFloat(oldValue) - 1;
			$(this).parent().find('.number').val(newValue);			
		}		
	})
	
//Trackbacks

	var trackback = $("ol#trackbacks li:last").text();
	if(trackback == "No Trackbacks"){
		$("div.trackbacks").hide();
	}

//Comments 
	$('.error').click(function(){
		$(this).fadeOut(200);
		$(this).parent().find('input').focus();
	})
	$("#author, #email, #comment").blur(function(){
		$('.error').fadeOut(200);
	})
	$("#commentform").submit(function(){
		var author = $("#author");
		var email = $("#email");
		var comment = $("#comment");
		var emailFormat = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
        
        if (author.val() == "Your Name" || author.val() == "") {
            $('.author').fadeIn(200);
            author.focus();
            return false;
        }
        
        if (email.val() == "Your Email" || email.val() == "") {
            $('.email').fadeIn(200);
            email.focus();
            return false;

        }
        
        if (email.val().search(emailFormat) == -1) {
            alert("Please enter valid email address!");
            email.focus();
            return false;
        }
        
        if (comment.val() == "Your Comments" || comment.val() == "") {
            $('.commentbox').fadeIn(200);
            comment.focus();
            return false;
        }	
	}) // End Function

// CONTACT FORM
$("#formloading").ajaxStart(function(){
	$(this).removeClass('hidden');
});
$("#contact").hide();
$("#contact").submit(function(){
    var fullname = $(this).find("#fullname");
    var emailid = $(this).find("#emailid");
    var message = $(this).find("#message");
    var spam = $(this).find("#spam");
    var emailFormat = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
    $("span.formerror").addClass('hidden');
    
    if (fullname.val() == "") {
        fullname.parent().find('span.formerror').html("This field is required.").removeClass('hidden');
        fullname.focus();
        return false;
    }
    else 
        if (emailid.val() == "") {
            emailid.parent().find('span.formerror').html("This field is required.").removeClass('hidden');
            emailid.focus();
            return false;
        }
        else 
            if (emailid.val().search(emailFormat) == -1) {
                emailid.parent().find('span.formerror').html("Not a valid email address.").removeClass('hidden');
                emailid.focus();
                return false;
            }
            else 
                if (spam.val() != '12') {
                    spam.parent().find('span.formerror').html("Incorrect Answer!!").removeClass('hidden');
                    spam.focus();
                    return false;
                }
                else 
                    if (message.val() == '') {
                        message.parent().find('span.formerror').html("Got nothing to say?").removeClass('hidden');
                        message.focus();
                        return false;
                    }
                    else {
                    
                        var url = $(this).attr('action');
                        var dataString = $('.aform').serialize();
                        var updatecontainer = url.split("#")[1];
                        newurl = url.split("#")[0];
                        newurl = newurl + "?random=" + Math.random() + "#" + updatecontainer;
                        var update = url.split("#")[1];
                        var update = update.split("&")[0];
                        $.ajax({
                            type: "POST",
                            url: "" + newurl + "",
                            data: dataString,
                            success: function(response){
                                $('#' + update).html(response);
                            }
                        })
                    }
    return false;
})

$("#survey").submit(function(){
    var url = $(this).attr('action');
    var dataString = $('.aform').serialize();
    var updatecontainer = url.split("#")[1];
    newurl = url.split("#")[0];
    newurl = newurl + "?random=" + Math.random() + "#" + updatecontainer;
    var update = url.split("#")[1];
    var update = update.split("&")[0];
    $.ajax({
        type: "POST",
        url: "" + newurl + "",
        data: dataString,
        success: function(response){
            $('#' + update).html(response);
        }
    })
    return false;
})

// Live Chat
$("#livechat").click(function(){
	$("#lightbox").fadeIn(500);
	$("#livechatwindow").fadeIn(300);
	return false;
})

$("#closelightbox").click(function(){
	$("#lightbox").fadeOut(500);
	$("#livechatwindow").fadeOut(300);
	return false;
})


})
$(window).ready(function(){
	$("#loading").fadeOut(500);
	$("#contact").show();
})
DD_roundies.addRule('.round', 10, true);
