//jQuery Func
jQuery(function($) { 
	
	//PNG fix for IE6
	if($.browser.msie && $.browser.version.substr(0,1) == 6){
		DD_belatedPNG.fix('.socials a, .keyword-link a, .keyword-link .word, .icon');
	}
	
	$('#main .gallery-t-holder').height(0);
	$('#main .gallery-t-holder .project-descriptions-list li').each(function() {
		if ($(this).height() > $('#main .gallery-t-holder').height()) {
			$('#main .gallery-t-holder').height($(this).height() + 10);
		}
		$(this).hide();
	});
	
	$('#navigation a').each(function() {
		var href = $(this).attr('href');
		
		if (href.match(/about/)) {
			$(this).addClass('about_page');
		} else if (href.match(/services/)) {
			$(this).addClass('services_page');
		} else if (href.match(/gallery/)) {
			$(this).addClass('gallery_page');
		} else if (href.match(/blog/)) {
			$(this).addClass('blog_page');
		} else if (href.match(/contact/)) {
			$(this).addClass('contact_page');
		} else if (href.match(/clients/)) {
			$(this).addClass('clients_page');
		}
	});
	
	$('#sidebar #s').addClass('field blink').attr('value', 'SEARCH THE BLOG').attr('title', 'SEARCH THE BLOG');
	
	//Blink Fields
	 $('.blink').
	    focus(function() {
	        if(this.title==this.value) {
	            this.value = '';
	        }
	    }).
	    blur(function(){
	        if(this.value=='') {
	            this.value = this.title;
	        }
	    });
	
	var hoverintent = false;
	
	$('.postbody').jScrollPane({
		scrollbarWidth: 19,
		wheelSpeed: 50
	});
	
	//Gallery Hovers
	$('.gallery-list ul li a').hover(function() {
       show_gallery_description($(this).attr('rel'));
	}, function() {
       if (!$('.gallery-list .featured_images img.current').length) {
           hide_gallery_description();
       }
	});

    $('.gallery-list ul li a').click(function() {
    	$(this).hide().parents('.gallery-list').find('.featured_images, #' + $(this).attr('rel')).css('visibility', 'visible').addClass('current');
        show_gallery_description($(this).attr('rel'));
    	return false;
    });

    $('.gallery-list .featured_images img').click(function() {
    	$('#parent_' + $(this).attr('id')).show();
    	$(this).css('visibility', 'hidden').removeClass('current').parent().css('visibility', 'hidden');
        hide_gallery_description();
    });

    $('#close_featured').click(function() {
    	$(this).parent().find('img.current').click();
    	return false;
    });

    function show_gallery_description(id) {
        clearTimeout(hoverintent);
        $('.gallery-initial-content').hide();
        $('.project-descriptions-list li').hide();
        $('#description_' + id).show();
    }

    function hide_gallery_description() {
        hoverintent = setTimeout(function() {
            $('.gallery-initial-content').show();
            $('.project-descriptions-list li').hide();
        }, 150);
    }

    
    //Home Page Links Hover
    $('.keyword-link a').hover(function(){ 
    	$(this).parent().find('.word').show();
     },function(){ 
     	$(this).parent().find('.word').hide();
    });
	
    $('#dh_contact_form').submit(function() {
    	var valid = true;
    	var errors = '';
    	
    	$(this).find('.required').each(function() {
    		if ($(this).val() == '' || $(this).val() == $(this).attr('title')) {
    			if (valid) {
    				$(this).focus();
    			}
    			valid = false;
    			errors += 'The "' + $(this).attr('title') + '" field is required\n';
    			$(this).addClass('field-error');
    		} else {
    			$(this).removeClass('field-error');
    		}
    	});
    	
    	$(this).find('.email').each(function() {
    		var email_pattern = /^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/;
    		
    		if (!email_pattern.test($(this).val())) {
    			if (valid) {
    				$(this).focus();
    			}
    			valid = false;
    			errors += 'Please enter a valid email in the "' + $(this).attr('title') + '" field\n';
    			$(this).addClass('field-error');
    		} else {
    			$(this).removeClass('field-error');
    		}
    	});
    	
    	if (!valid) {
    		alert(errors);
    	}
    	
    	return valid;
    });
});
