(function($){
    $(document).ready (function(){
    	$('.field, textarea').focus(function() {
            if(this.title==this.value) {
                this.value = '';
            }
        }).blur(function(){
            if(this.value=='') {
                this.value = this.title;
            }
        });

        if (window.PIE) {
            $('#navigation ul li.contact a,.loading-overlay .overlay-container').each(function() {
                PIE.attach(this);
            });
        }

        fix_team_content_images();
        fix_work_entries_h();
        fix_sidebar_h();
        fix_footer_content_position();

        if ( $('.selectbox').length != 0 ) {
            mdl_selectbox();
            mdl_file_input();
            mdl_contact_date_picker();
        };
        // Carousel
        
        $(".work-detail .slider-carousel ul").jcarousel({
            scroll:1,
            auto:4,
            wrap:"both",
            itemFirstInCallback: mycarousel_itemFirstInCallback,
            initCallback: mycarousel_initCallback,
            start: 1
        });

        $('.product-content .right-col .image-holder a').fancybox();
        
        $('.load-more a, .ajax-work-nav li a').live('click', function(){
            var url = $(this).attr('href');
            url += '&is_ajax_request=1';
            if ( $(this).parents('.ajax-work-nav').length != 0 ) {
                $(this).parents('.ajax-work-nav').find('li a').removeClass('active');
                $(this).addClass('active');
            };
            $('.loading-overlay').show();
            $.get(url, function(d){
                $('.load-more').remove();
                if ( $('.blog-page').length != 0 ) {
                    $(d).find('.featured-post').remove().appendTo('#main .blog-content');
                    $(d).find('.load-more').remove().appendTo('#main .blog-content');
                } else if ( $('.work-page').length != 0 ) {
                    $('.work-page .work-content ul li').remove();
                    $(d).find('> ul > li').remove().appendTo('.work-page .work-content ul');
                    work_entries_h_fix();
                } else {
                    $(d).find('.events-content .event-wrapper').remove().appendTo('#main .events-content .events-container');
                    $(d).find('.load-more').remove().appendTo('#main .events-content');
                };
                $('.loading-overlay').hide();
            }, 'html')
            return false;
        })

        $('.get-a-quote').click(function(){
            $('.quote-form').remove().appendTo($('.contact-content .right'));
            $(this).parents('.get-quote').fadeOut();
            $('.quote-form').fadeIn(function(){
                mdl_selectbox();
                mdl_file_input();
                mdl_contact_date_picker();
            });
            return false;
        });

        $('.take-me-back').live('click', function(){
            $(this).parents('.quote-form').fadeOut(function(){
                $('.quote-form').remove().appendTo($('.contact-content'));
            });
            $('.get-quote').fadeIn();
            return false;
        });
        if ( window.location.hash == '#project' ) {
            $('.get-a-quote').click();
        };
    });

    $(window).load(function(){
        if ( $('.service-content').length != 0 ) {
            $('.service-content .service-right > *:last-child').addClass('last-element');
            $('.service-content .service-left').height(Math.max($('.service-content .service-left').height(),$('.service-content .service-right').height()));
        };

        fix_team_content_images();
        fix_footer_content_position();
        $(".intro .slider-carousel ul").jcarousel({
            scroll:1,
            //auto:1,
            wrap:"circular",
            start: 1, 
            initCallback: function(){
                var width = $(document).width();
                $(".intro .slider-carousel ul li").width(width);
                $('.intro .slider-carousel').append('<div class="shell"></div>');
                $('.intro .jcarousel-prev,.intro .jcarousel-next').remove().appendTo($('.intro .slider-carousel .shell'));
            }
        });

        $(window).resize(function(){
            var width = $(document).width();
            $(".intro .slider-carousel ul li").width(width);
        })

        $(".content .slider2 .with-carousel ul").jcarousel({
            scroll:1,
            //auto:1,
            wrap:"both",
            start: 1
        });

        $(window).click(function(e){
            if ( $(e.target).parents('.selectbox').length == 0 ) {
                $('.selectbox .list:visible').slideToggle('fast');
            };
        })
    })

    function mdl_selectbox () {
        $('.currentItem').css('display', 'block');
        $('.selectbox select').hide();

        $('.field-row .currentItem').click(function(){
            $('.selectbox .list:visible').slideToggle('fast');
            $(this).parent().find('.list').slideToggle('fast');
        });
        
        $('.field-row .selectbox .list a').click(function(){
            var select = $(this).parents('.selectbox').find('select');
            var value = $(this).text();
            var idx = $(this).parent().index();
            
            $('option', select).each(function(){
                $(this).attr('selected', false);
            });
            
            $('option', select).eq(idx).attr('selected', true);
            $(this).parents('.selectbox').find('.currentItem').text(value);
            $(this).parents('.list').slideToggle();
            return false;
        });
    }

    function mdl_contact_date_picker() {
        if ( $(".calendar-holder .ui-datepicker-trigger").length != 0 ) {
            $(".calendar-holder .ui-datepicker-trigger").remove();
        };
        $(".calendar-holder .calendar").removeClass('hasDatepicker').datepicker({
            showOn: "button",
            buttonImage: window.__template_directory + "/images/calendar.png",
            buttonImageOnly: true
        });
    }

    function mdl_file_input() {
        // custom upload form
        $(".custom-upload").mousemove(function(e){
            var pageX = e.pageX;
            var pageY = e.pageY;
            var thisOffsetY = $(this).offset().top;
            var thisOffsetX = $(this).offset().left;
            $(this).find('input').css({
                'left': pageX - thisOffsetX - 180,
                'top': pageY - thisOffsetY - 30
            })
            //Get File Name
            $('.custom-upload input').change(function(){ 
                var inputVal = $(this).val();
                var inputValArr = inputVal.split("\\");
                var fileName = inputValArr[inputValArr.length - 1];
                $(this).parent().find('.filename').text(fileName);
            });
        });
    }

    function mycarousel_initCallback(carousel) {
        $('.slider-nav a').bind('click', function() {
            carousel.scroll($.jcarousel.intval($(this).text()));
            return false;
        });         
    };
        
    function mycarousel_itemFirstInCallback(carousel, item, idx, state) {
        $('.slider-nav a').removeClass('active');
        $('.slider-nav a').eq(idx-1).addClass('active');
    };

    function fix_team_content_images () {
        $(window).load(function() {
            var count = $(".content-box .titles").length;
            var last_row = count - (parseInt(count/3) * 3);
            var current = 0;
            var maxH = 0;
            $(".content-box .titles").each(function() {
                current ++;
                if ( count - current < last_row ) {
                    $(this).parents('.content-box').addClass('noborder');
                };
                maxH = Math.max( maxH, $(this).height() );
            }).css({  height: maxH  });
        });
    }

    function fix_work_entries_h () {
        $(window).load(function() {
            work_entries_h_fix();
        });
    }

    function work_entries_h_fix () {
        var maxH = 0;
        var i = 0;
        $(".work-content ul li.work-entry").each(function() {
            maxH = Math.max( maxH, $(this).height() );
            i ++;
            if ( (i + 1) % 4 == 0 ) {
                $(".work-content ul li.work-entry:eq("+i+"),.work-content ul li.work-entry:lt("+i+"):gt("+(i-3)+"),.work-content ul li.work-entry:eq("+(i-3)+")").css('height', maxH);
                maxH = 0;
            };
        });
    }

    function fix_sidebar_h() {
        $(window).load(function() {
            var cont_h = $('.inner-page > div:eq(1)').height();
            var sidebar_h = $('.sidebar').height();
            if ( sidebar_h < cont_h ) {
                $('.sidebar').height(cont_h);
            };
        });
    }

    function fix_footer_content_position () {
        var height = 0;

        $('.footer .footer-box').each(function(){
            if ( $(this).find('table.menu-table').length != 0 ) {
                $(this).find('.menu-table tr:last').attr('valign', 'bottom').addClass('last');
            };
        });
        $('.footer .footer-box').each(function(){
            height = Math.max(height, $(this).height());
        });
        $('.footer .footer-box').height(height);
    }

})(jQuery)
