/* Cufon font */

jQuery(document).ready(function() {
	$(function(){ $('form.uniForm').uniform(); });
	
	/* Dropdown menu using superfish */
	jQuery('.nav').supersubs({
		minWidth: 12,
		maxWidth: 25,
		extraWidth: 1
	}).superfish({
		delay: 250,
		animation: { opacity: 'show', height: 'show' },
		speed: 'fast',
		autoArrows: false 
	})
	.find('ul')
	.bgIframe({ 
		opacity: false 
	});
	
	/* Add html separators between widgets */
	jQuery("#secondary .widget:not(:last-child), .footer-column .widget:not(:last-child)").each( function() { 
        jQuery(this).after('<div class="hr"><hr /></div>');;
    });

	
	/* Tabs */
	jQuery(".tab-content").hide(); //Hide all content
	jQuery(".tab-container > br").remove(); //Hide all content

	jQuery("ul.tabs").each( function() {
		jQuery(this).find("li:first").addClass("active"); //Activate first tab
	});
	
	jQuery(".tab-container").each( function() {
		jQuery(this).find(".tab-content:first").show(); //Show first tab content
	});

	jQuery("ul.tabs li").click(function() {
		jQuery(this).parent().find('li.active').removeClass("active");
		Cufon.replace('ul.tabs li a', { hover: true });
		jQuery(this).addClass("active"); //Add "active" class to selected tab
		jQuery(this).parent().next().find(".tab-content").hide(); //Hide all tab content

		var activeTabIndex = (jQuery(this).find("a").attr("href")).substring((jQuery(this).find("a").attr("href")).indexOf('#tab') + 4); //Find the href attribute value to identify the active tab + content
		
		var activeTabContent = jQuery(this).parent().next().find("div.tab-content:nth-child(" + activeTabIndex + ")");
		activeTabContent.fadeIn(); //Fade in the active ID content
		
		return false;
	});
	
	/* Toggle */
	jQuery(".toggle-container .toggle-content").hide(); //Hide (Collapse) the toggle containers on load
	jQuery(".toggle-container .toggle-sign").text('+'); //Add the + sign on load

	jQuery(".toggle-container-open .toggle-content").show(); //Show the default opened toggle containers on load	
	jQuery(".toggle-container-open .toggle-sign").text('-'); //Add the - sign on load

	jQuery(".toggle-container .toggle-content").each( function() {
		jQuery(this).css('width', jQuery(this).parent().width() - 30 );
	});
		
	jQuery(".toggle-container .toggle").toggle(function() {
		jQuery(this).find('.toggle-sign').text('-');
		jQuery(this).next(".toggle-content").slideToggle();
	}, function() {
		jQuery(this).find('.toggle-sign').text('+');
		jQuery(this).next(".toggle-content").slideToggle();
	});
	
	/* Required to style post page links similar to the wp-pagenavi ones */
	jQuery('.post_linkpages a span').each( function() {                                  
        jQuery(this).parent().html(jQuery(this).html());
    });
    jQuery('.post_linkpages span').addClass('current');
	
	/* Pricing Table */
	jQuery(".price-table > br").remove();
	jQuery(".price-table .price-column:nth-child(even)").addClass('price-column-even');
	jQuery(".price-column li:nth-child(even)").addClass('even');
	
	jQuery(".price-table").each( function() { 
        jQuery(this).find('.price-column:first').addClass('price-column-first');
    });
	
	jQuery(".price-table").each( function() { 
        jQuery(this).find('.price-column:last').addClass('price-column-last');
    });
    
    /* Buttons */
    // Add target="_blank" to buttons that have the data-newwindow="true" attribute
	jQuery('.button[data-newwindow=true]').each( function() {                                  
		jQuery(this).attr('target', '_blank').removeAttr('data-newwindow');
	});
	
	// Add custom hover background color to buttons that have the data-bgcolorhover attribute
	jQuery('.button, .button-big').each( function() {
		if( jQuery(this).attr('data-bgcolor') != '' ) { jQuery(this).css('backgroundColor', jQuery(this).attr('data-bgcolor')); }
		if( jQuery(this).attr('data-txtcolor') != '' ) { jQuery(this).css('color', jQuery(this).attr('data-txtcolor')); }
		jQuery(this).hover(
			function () { // on hover
				jQuery(this).css('backgroundColor', '');
				jQuery(this).css('color', '');
				if( jQuery(this).attr('data-bgcolorhover') != '' ) { jQuery(this).css('backgroundColor', jQuery(this).attr('data-bgcolorhover')); }
				if( jQuery(this).attr('data-txtcolorhover') != '' ) { jQuery(this).css('color', jQuery(this).attr('data-txtcolorhover')); }
			}, 
			function () { // off hover
				jQuery(this).css('backgroundColor', '');
				jQuery(this).css('color', '');
				if( jQuery(this).attr('data-bgcolor') != '' ) { jQuery(this).css('backgroundColor', jQuery(this).attr('data-bgcolor')); }
				if( jQuery(this).attr('data-txtcolor') != '' ) { jQuery(this).css('color', jQuery(this).attr('data-txtcolor')); }
			}
		);
	});
	
	
	/* Lightbox shortcode button firefox fix */
	jQuery('a.lightbox button').each( function() {
		jQuery(this).parent().css('textDecoration', 'none');
	});
	
    /* Hide the footer widgets area if there's no widgets present */
	if( jQuery('.footer-column .widget').size() == 0 ) {
    	jQuery('#footer-widgets-separator-container').css('display', 'none');
    	jQuery('#footer-widgets-container').css('display', 'none');
	};
});

/* After the page has loaded... */
jQuery(window).load(function() {

	/* Insert the footer of the inner page sliders using javascript */
	jQuery('.slider').after('<div class="slider-footer"></div>');
	
	/* Apply lightbox */
	SetLightbox(false);
	
});


function SetLightbox(load) {
	
	/* PrettyPhoto */
	jQuery("a[rel^='lightbox']").prettyPhoto({
		slideshow: 5000
	});
	
	/* Reduce opacity when mouse hover lightbox images */
	jQuery("a[rel^='lightbox'] img, .post-image a img, .home-blog-list a img, .portfolio-item a img, .widget-posts a img").not("#nivo-slider img, #stage-slider img, .slider img").hover(function() {
		jQuery(this).stop().fadeTo("fast", 0.7); // This sets the opacity to 60% on hover
	},function(){
		jQuery(this).stop().fadeTo("fast", 1.0); // This sets the opacity back to 100% on mouseout
	});
}

/* Event triggered before every stage slider transition */
function onCycleBefore() {
	// By default resume the slider, if a video exists it will pause it when autoplay is on
	jQuery('#stage-slider').cycle('resume'); // go to next slider item
	
	// Search for an embeded video and remove it
	var element = jQuery(this).parent().find('object');
	element.wrap(function() {
		return '<div id="' + element.attr('id') + '" />';
	}).remove();
	
	// Now create a video if it's present on this slider item
	createVideo(jQuery(this).find('.stage-slider-video'));
}

