/***************************************************************************
 * @Author: NetChillies - PepUp The Web!
 * @Website: http://www.netchillies.com/
 * @Contact: http://www.netchillies.com/contact
 * @Privacy Policy & Terms of Use: http://www.netchillies.com/terms
 * @License Info: http://www.netchillies.com/license
 ***************************************************************************/
$(document).ready(function(){
    $('a, .ttip').tooltip({
        track: true,
        delay: 0,
        fade: 100,
        showURL: false
    });


    $('.fade').animate({
        width: '100%'
    }, 3000).slideUp(400);
    $('.close-message').click(function(){
        $(this).parent().slideUp(400);
    })
    
    
    
    // Services Page

    
    $('.additional-features span').click(function(){
        var text = $(this).text();
        if(text == 'Show'){
            $(this).parent().parent().find(".additional-features-content-html").slideToggle(500);
            $(this).text('Hide');
        }else if(text == 'Hide'){
            $(this).parent().parent().find(".additional-features-content-html").slideToggle(500);
            $(this).text('Show');
        }
    })
    
    // SEARCH BOX
    $("#search-input").bind('focus', function(){
        $(this).animate({
            width: '139'
        }, 250)
    })
    $("#search-input").bind('blur', function(){
        $(this).animate({
            width: '110'
        }, 250)
    })
    
    // CYCLE *********************************************************
    $('#t-slides').cycle({ 
        fx:     'scrollLeft',
        speed:    500, 
        timeout:  5000
    });
    
    
    // FANCY BOX *****************************************************
    
    /* This is basic - uses default settings */
	
    $("a.fancybox").fancybox({
        titlePosition: 'over',
        overlayOpacity: '0.7',
        overlayColor: '#000',
        transitionIn: 'elastic',
        transitionOut: 'elastic'
    });
	
    /* Using custom settings */
	
    $("a.fancybox-inline").fancybox({
        'hideOnContentClick': true
    });

    /* Apply fancybox to multiple items */
	
    $("a.fancybox-group").fancybox({
        'transitionIn'	:	'elastic',
        'transitionOut'	:	'elastic',
        'speedIn'		:	600, 
        'speedOut'		:	200, 
        'overlayShow'	:	false
    });
    
    
    // CONTACT FORM
    $(".contact-form").submit(function(){
        var error = 0;
        
        var emailaddress = $("#emailaddress").val();
        var question = $("#question").val();
        
        $(this).find('.required').each(function(){
            if($(this).val() == ''){
                $(this).addClass('req');
                error = 1;
            }else{
                error = 0;
            }
        })
        
        if(question != 10){
            error = 1;
            $("#question").addClass('req');
            $("#question").val('');
            
        }
        
        if(!checkEmail(emailaddress)){
            error = 1;
            $("#emailaddress").addClass('req'); 
            $("#emailaddress").val('');
        }
        
        if(error == 0){
            return true;
        }else{
            return false;
        }
        
    })
    
    $(".required").blur(function(){
        if($(this).val() != ''){
            $(this).removeClass('req');
        }
    })
    
    // Other forms
    $("form").submit(function(){
        var error = 0;
        
        var emailaddress = $(this).find('.validemail').val();
        var question = $(this).find(".validanswer").val();
        
        $(this).find('.required').each(function(){
            if($(this).val() == ''){
                $(this).addClass('req');
                error = 1;
            }else{
                error = 0;
            }
        })
         
        
        if(error == 0){
            return true;
        }else{
            return false;
        }
        
    })
    
    
    
    function checkEmail() {
        var email = document.getElementById('emailaddress');
        var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
        if (!filter.test(email.value)) {
            return false;
        }else{
            return true;
        }
    }



    
    //****************************************************************
    $("#main-nav ul li:last a, .contact-us").click(function(){
        $('#contact').scrollTo(2000);
        $("#contact .tour")
        .animate({
            'background-color':"#303843"
        }, 1000)
        .fadeIn(100)
        .animate({
            'background-color':"#303843"
        }, 2000)
        .fadeOut(300);
        return false;
    })
    
    $(".add-comment").click(function(){
        $('#respond').scrollTo(1000);
        return false;
    })


    $(".like").each(function(){
        var id = $(this).attr('id');
        
        if(getCookie(id) == 1){
            $(this).addClass('liked');
        }
        
    })
    

    $(".like").bind('click', function(){
        var link = $(this);
        var post_id = $(this).attr('rel');
        var url = $(this).attr('href');
        
        if(getCookie('like-'+post_id) != 1){
        
            $.ajax({
                type: "GET",
                url: url,
                data: "post_id="+post_id,
                success: function(result){
                    $(link).text(result);
                    setCookie('like-'+post_id, 1, 365);
                    overlay_message('Thank You!');
                    $(link).addClass('liked');
                }
            });
            
        }else{
            overlay_message('You already like this one!');
        }
        return false;
    })
    
    function overlay_message(msg){
        
        $("#overlay").fadeIn(200).animate({
            'background-color': '#000000'
        },1450).fadeOut(100);
        
        
        $("#overlay-msg").text(msg);
        $("#overlay-msg").fadeIn(250).animate({
            'top': '250'
        },400).animate({
            'color': '#FFF'
        },1000).animate({
            'top': '-100px'
        },400);
        
        
    }
    
    $(".like-button").bind('click', function(){
        var link = $(this);
        var post_id = $(this).attr('rel');
        var url = $(this).attr('href');
        
        if(getCookie('like-'+post_id) != 1){
        
            $.ajax({
                type: "GET",
                url: url,
                data: "post_id="+post_id,
                success: function(result){
                    $("#single-like").text(result);
                    setCookie('like-'+post_id, 1, 365);
                    overlay_message('Thank You!');
                }
            });
            
        }else{
            overlay_message('You already like this one!');
        }
        return false;
    })
    


    $("#overlay").fadeIn(200);
    $("#overlay-msg")
    .fadeIn(200)
    .text('Loading! Please wait..')
    .animate({
        'top': '250px'
    },300)
    
    
})

$(window).load(function(){
    $("#overlay").animate({
        'background-color': '#000'
    },800)
    .fadeOut(200);
    $("#overlay-msg")
    .text('Thank You!')
    .fadeIn(300)
    .animate({
        'top': '250px'
    },500)
    .animate({
        'top': '-100px'
    },300)
})




function setCookie(c_name,value,exdays){
    var exdate=new Date();
    exdate.setDate(exdate.getDate() + exdays);
    var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
    document.cookie=c_name + "=" + c_value;
}

function getCookie(c_name){
    var i,x,y,ARRcookies=document.cookie.split(";");
    for (i=0;i<ARRcookies.length;i++)
    {
        x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
        y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
        x=x.replace(/^\s+|\s+$/g,"");
        if (x==c_name)
        {
            return unescape(y);
        }
    }
}


jQuery.fn.extend({
    scrollTo : function(speed, easing)
    {
        return this.each(function()
        {
            var targetOffset = $(this).offset().top;
            $('html,body').animate({
                scrollTop: targetOffset
            }, speed, easing);
        });
    }
});

// CAROUSEL
$(function(){
    $("div#home-latest-works").carousel({
        dispItems: 3, 
        loop: true,
        nextBtn: '<span class="carousel-next">&nbsp;</span>',
        prevBtn: '<span class="carousel-previous">&nbsp;</span>'
    });
    
    $("div.slideshow").carousel({
        dispItems: 2, 
        loop: true,
        direction: "vertical",
        nextBtn: '<span title="Next" class="ttip sidebar-carousel-next">&raquo;</span>',
        prevBtn: '<span title="Previous" class="ttip sidebar-carousel-previous">&laquo;</span>'
    });
    
    $("div.slideshow-1").carousel({
        dispItems: 1, 
        loop: true,
        direction: "vertical",
        nextBtn: '<span title="Next" class="ttip sidebar-carousel-next">&raquo;</span>',
        prevBtn: '<span title="Previous" class="ttip sidebar-carousel-previous">&laquo;</span>'
    });
    
    $("div.slideshow-2").carousel({
        dispItems: 2, 
        loop: true,
        direction: "vertical",
        nextBtn: '<span title="Next" class="ttip sidebar-carousel-next">&raquo;</span>',
        prevBtn: '<span title="Previous" class="ttip sidebar-carousel-previous">&laquo;</span>'
    });
    
});
