// JavaScript Document

jQuery.noConflict();

jQuery(document).ready(function(){
	
	//Add attributes
	
	jQuery('#navigation li a').addClass('main_buttons');
	jQuery('#navigation li a a').removeClass('main_buttons');
	
	jQuery('.commentlist li:first-child, .children li:first-child').addClass('first');
	jQuery('.commentlist li:last-child, .children li:last-child').addClass('last');
	
	if ( jQuery('ol.commentlist li').find('ul.children') ) {
		jQuery(this).addClass('asdf');
	}
	
	jQuery('ol.commentlist li:not(:has("ul.children"))').addClass('last')
	
	jQuery('#navigation').superfish({
		delay:       800,
		animation:   { opacity: 'show', height:'show' },
		speed:       300,
		autoArrows:  true,
		anchorClass : 'sf-with-ul',
		arrowClass  : 'sf-sub-indicator',
		dropShadows: false
	}); 
	
	jQuery('#featured_desc').jcarousel({
		scroll: 1,
		auto: 5,
		wrap: 'both',
		animation: 1500,
		initCallback: feature_carousel_initCallback,
		buttonNextHTML: null,
		buttonPrevHTML: null,
		easing: 'easeInOutExpo'
	});
	
	jQuery('#featured_images').jcarousel({
		scroll: 1,
		auto: 5,
		wrap: 'both',
		animation: 1500,
		initCallback: feature_carousel_initCallback,
		itemVisibleInCallback: {onBeforeAnimation: images_itemVisibleInCallback},
		buttonNextHTML: null,
		buttonPrevHTML: null,
		easing: 'easeInOutExpo'
	});
	
	jQuery('.roundify').corner('8px');
	jQuery('.wp-pagenavi span, .wp-pagenavi a, .wp-commentnavi span, .wp-commentnavi a').corner('3px');
	jQuery('#navigation li a.main_buttons').corner('5px');
	
	//Add Design Related
	
	jQuery('.widget_calendar #calendar_wrap').unwrap();
	jQuery('.widget_calendar').wrapInner('<div class="calendar_wrapper"></div>');
	
	jQuery('.widget_text .textwidget').unwrap();
	jQuery('.widget_text').wrapInner('<div class="widget_wrapper_text"></div>');
	
	jQuery('.info_section ul li:first-child').addClass('first');
	jQuery('.info_section ul li:last-child').addClass('last');
	
	jQuery('#single_page .basic_style_1 a, #post_list .basic_style_1 a').find('img').addClass('image_with_desc_wrap');
	
	jQuery('#footer_menu li:not(li:last-child)').append('&bull;')
	
	//Pretty Photo (lightbox effect)
	jQuery('#single_page .basic_style_1 a[href$=jpg], #single_page .basic_style_1 a[href$=gif], #single_page .basic_style_1 a[href$=png], #single_page .basic_style_1 a[href$=bmp]').addClass('pretty_photo');
	jQuery('.pretty_photo').prettyPhoto();
	
	//Form
	
	var loader = jQuery('<div id="loader"><img src="images/ajax-loader.gif" alt="loading..." /> <span>Sending Message...</span></div>')
		.appendTo('#form_result')
		.hide();
	jQuery().ajaxStart(function() {
		jQuery('#contact_form').hide(500);
		jQuery('#quick_contact').hide(500);
		loader.show(500);
	}).ajaxStop(function() {
		loader.hide(500);
	}).ajaxError(function(a, b, e) {
		throw e;
	});
	
	jQuery("#contact_form").validate({
			
		rules: {
			input_name: 'required',
			input_email: {
				required: true,
				email: true
			},
			input_subject: 'required',
			textarea_message: 'required'
		},
		messages: {
			input_name: 'Name is required',
			input_email: {
				required: 'Email is required',
				email: 'Invalid email format'
			},
			input_subject: 'Please specify a subject',
			textarea_message: 'Message is empty'
		},
		errorElement: 'span',
		errorClass: 'error_message',
		errorPlacement: function(error, element) {
			error.appendTo(element.parent('span').prev('label'));
		},
		submitHandler: function(form) {
			jQuery(form).ajaxSubmit({
				beforeSubmit : function () {
					jQuery('#contact_form').hide(500);
					loader.show(500);
				},
				success : function () {
					loader.hide(500);
				},
				target : '#form_result1'
			});
		}
			
	});
	
	jQuery("#quick_contact").validate({
			
		rules: {
			input_name: 'required',
			input_email: {
				required: true,
				email: true
			},
			input_subject: 'required',
			textarea_message: 'required'
		},
		messages: {
			input_name: 'Name is required',
			input_email: {
				required: 'Email is required',
				email: 'Invalid email format'
			},
			textarea_message: 'Message is empty'
		},
		errorElement: 'span',
		errorClass: 'error_message',
		errorPlacement: function(error, element) {
			error.appendTo(element.parent('p').prev('p'));
		},
		submitHandler: function(form) {
			jQuery(form).ajaxSubmit({
				beforeSubmit : function () {
					jQuery('#quick_contact').hide(500);
					loader.show(500);
				},
				success : function () {
					loader.hide(500);
				},
				target : '#form_result2'
			});
		}
			
	});
	
	/*** Cufon ***/
	/** ----------------------------------------------------- **/
	
	Cufon.replace(
		
		'#featured_desc h2,' +
		'#featured_desc h3,' +
		'#contact_page #contact_form label,' +
		'.comment-author,' +
		'.font_sansation,' +
		'.wp-pagenavi'
					  
	,{ fontFamily: 'Sansation', hover: true });
	
	Cufon.replace(
		
		'.basic_style_1 h1, .basic_style_1 h2, .basic_style_1 h3, .basic_style_1 h4, .basic_style_1 h5, .basic_style_1 h6'
					  
	,{ fontFamily: 'Sansation', hover: true });
	
	//Initialize Zebra Table
	
	zebra_table();
	
});

//Functions

function feature_carousel_initCallback($carousel) {
	
	jQuery('#featured_buttons li').bind('click', function() {
		$carousel.scroll(jQuery.jcarousel.intval(jQuery(this).text()));
		$carousel.startAuto(0);
		jQuery('#featured_buttons li.clicked').removeClass('clicked');
		jQuery(this).addClass('clicked');
		return false;
	});

};

function images_itemVisibleInCallback(carousel, item, idx, state) {
	jQuery('#featured_buttons li.clicked').removeClass('clicked');
	jQuery('#featured_buttons li:contains("' + idx + '")').addClass('clicked');
}; 

jQuery(function(){ //smoothscroll
	jQuery('.smooth_scroll').click(function() {
		if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') 
			&& location.hostname == this.hostname) {
				var $target = jQuery(this.hash);
				$target = $target.length && $target || jQuery('[name=' + this.hash.slice(1) +']');
			if ($target.length) {
				var targetOffset = $target.offset().top;
				jQuery('html,body').animate({scrollTop: targetOffset}, 1000);
				return false;
			}
		}
	});
});

function zebra_table(){ //zebra table

	jQuery('tbody tr:nth-child(odd)').addClass('odd');
	
}
