 /*	Revoltz JS, Core
	This code is copyright (c) Ernesto Mendez 2010.
	http://der-design.com */

cufon_init();

$(document).ready(function () {

	common_lib_functions();

	enhancements();

	effects();

	init_prettyphoto();

	topbar_features();

	init_flickr_widgets();

});

/* Function definitions */

function cufon_init() {

	init_color_schemes();

	Cufon('.title, #nav .special span', {
		hover: {color: theme_color('menu_active_color') },
		hoverables: {span:true, a:true}
	});

	Cufon('.post .wrap .date small, .post .wrap .date big', {
		hover: {color: theme_color('menu_active_color') },
		hoverables: {a:true}
	});

	add_action('cufon_init', cufon_enhancements );

}

function init_color_schemes() {

	theme_defaults = {}

	theme_defaults['default'] = {}
	theme_defaults['red'] = {}
	theme_defaults['modern'] = {}

	/* Default Color Scheme */

	theme_defaults['default']['menu_active_color'] = '#5f857b';

	theme_defaults['default']['topbar_search_bg_hover'] = '#8c8884';

	theme_defaults['default']['link_underline'] = '#5f857b';

	/* Red Color Scheme */

	theme_defaults['red']['menu_active_color'] = '#a62823';

	theme_defaults['red']['topbar_search_bg_hover'] = '#62615c';

	theme_defaults['red']['link_underline'] = '#a62823';

	/* Modern Color Scheme */

	theme_defaults['modern']['menu_active_color'] = '#a7afba';

	theme_defaults['modern']['topbar_search_bg_hover'] = '#212429';

	theme_defaults['modern']['link_underline'] = '#425b85';
	
	
	$(document).ready(function() {

		theme_defaults['frame_opacity'] = 0.5;

		theme_defaults['menu_desc_color'] = $('#nav li:not(.current_page_item) .special small').css('color');

		theme_defaults['topbar_search_bg'] = $('#topbar form input[name=s]').css('background-color');

	});

}

function theme_color(id) {

	return ( theme_defaults[id] ) ? theme_defaults[id] : theme_defaults[color_scheme][id];

}


function enhancements() {

	// Disable autocomplete for search form

	$('#topbar form input').attr('autocomplete', 'off');

	// Remove title from navigation elements
	
	$('#nav li a').removeAttr('title');

	// Alternating Colors for menu items
	
	$("#nav li").find("ul:first").children("li").children("a:even").addClass('alt');

	// Add parent class

	$('#nav li').each(function() {

		if ( $(this).find('ul').length >= 1 && $(this).parent().attr('id') != 'nav' ) {

			$(this).addClass('page-parent');

		}

	});

	// Optimize Menu description wrapping

	var counter = 1;

	intID1 = setInterval(function() {

		if ( $('.cufon-ready').length == 1 ) {

			$('#nav .special').each(function() {

				var w = $(this).find('canvas').width();

				w = ( w <= 80 ) ? 80 : w;

				w += 40;  // Offset

				$(this).parent().width(w);

			});

			do_action('cufon_init');

			clearInterval(intID1);

		}

		// Prevent infinite loop

		if ( counter == 1000 ) {clearInterval(intID1);}

		counter+= 1;

	},10);

	// Enhance content links

	$('.excerpt a:not(.more-link), .post-content a:not(.more-link), .widget_text a, #portfolio .portfolio-item a.more-link, .comment .comment-body p a').each(function() {

		if ( $(this).find('img').length == 0 ) {

			$(this).css({
				'borderBottom': 'solid 1px ' + theme_color('link_underline')
			});

		}

	});

	// Remove underline from Post Edit Link

	$('.post-edit-link').css('border', 'none').attr('target', '_blank');

	// Prevent post counts from breaking widget_categories

	$('.widget_categories').each(function() {

		$(this).find('li').each(function() {

			var html = String( $(this).html() );

			var link = String( $(this).find('a').html() );

			var match = html.match(/ \(\d\)\s+/);

			if ( match ) {

				var post_count = $.trim(match[0]);

				$(this).html( html.replace(post_count, '') );

				$(this).find('a').html(link + ' &nbsp;<small>' + post_count + '</small>');

			} else {

				return false;

			}
			
		});

	});

}

function effects() {

	// Topbar links

	if ( ! $.browser.msie ) {
		
		$('#topbar a').hover(function() {

			topbar_img = $(this).find('img');

			if ( topbar_img.length == 1 ) {

				topbar_img.stop().animate({opacity:1}, 200);

			}

		}, function() {

				topbar_img.stop().animate({opacity:0.6},200);

		});
		
	}

	// Dropdown menu animation

	$('#nav li').hover(function() {

		$(this).find('ul:first').css({visibility: 'visible',display: 'none'}).slideDown(400,'easeOutSine');

	}, function() {

		$(this).find('ul:first').css({visibility: 'hidden'});

	});

	// Image frame animations for homepage posts

	if ( ! $.browser.msie ) {

		$('#content .img-wrap .frame, .post .post-img .frame').hover(function() {

			$(this).stop().animate({opacity:1}, 200);

		}, function() {

			$(this).stop().animate({opacity: theme_color('frame_opacity')},300);

		});

	}
	
	// Portfolio overlay animation

	$('#portfolio-wrap .thumb').hover(function() {

		$(this).find('.overlay').css({visibility:'visible'}).stop().animate({'opacity':'0.85'},500);

	}, function() {

		$(this).find('.overlay').stop().animate({'opacity':'0'}, 300);

	});

	// Topbar form floater animation

	$('#topbar form').hover(function() {

		var input = $(this).find('input[name=s]');

		input.stop().animate({'backgroundColor': theme_color('topbar_search_bg_hover')}, 300);

		if ( input.val() != '' ) { return false; }

		$(this).find('.floater').stop().animate({left: '10px'}, 300);

	}, function() {

		$(this).find('input[name=s]').stop().animate({'backgroundColor': theme_color('topbar_search_bg')},300);

		$(this).find('.floater').stop().animate({left: '-100px'}, 300);

	});

	$('#topbar form, #topbar #form .floater').click(function() {

		var floater = ( $(this).hasClass('floater') ) ? $(this) : $(this).find('.floater');

		floater.stop().animate({left:'-100px'},300);

		floater.parents('form').find('input[name=s]').focus();

	});

	if ( ! back_to_top_show ) {

		// Footer hover effect (show back to top button)

		$('#footer').hover(function() {

			try { clearTimeout(back_to_top_timeoutID); } catch(e) { /* pass */ }

			$('#back-to-top a').stop().animate({top: '0'}, 200);

		}, function() {

			back_to_top_timeoutID = setTimeout(function() {

				$('#back-to-top a').stop().animate({top:'20px'}, 200);

			}, 800);

		});

		// Back to top hover / click effect(s)

		$('#back-to-top a').hover(function() {

			try { clearTimeout(back_to_top_timeoutID); } catch(e) { /* pass */ }

		}, function() {

			back_to_top_timeoutID = setTimeout(function() {

				$('#back-to-top a').stop().animate({top:'20px'}, 200);

			}, 800);

		});

	} else {

		$('#back-to-top a').css('top', '0px');

	}

	$('#back-to-top a').click(function() {
		
		var duration = 600;
		var easing = 'easeInOutSine';
		if ( $.browser.msie ) {$('html').animate({scrollTop:0}, duration);return false;}
		if ( !$.browser.opera ) {$('html,body').animate({scrollTop:0}, {duration: duration, easing: easing});}
		else {$('html').animate({scrollTop:0}, {duration: duration,	easing: easing});}
		return false;

	});

}

function init_flickr_widgets() {

	var c = 1;

	$('.revoltz-flickr').each(function() {$(this).attr('id','flickr-widget-' + c);c+=1;});

	$('.revoltz-flickr').each(function() {

		var query = $(this).find('.query').attr('href');

		query = $.base64Encode(query);

		query = encodeURIComponent(query);

		var id = $(this).attr('id');

		var url = append_slash(templatepath) + 'includes/ajax/flickr_ajax.php?id=' + id + '&q=' + query;

		$.ajax({
		  url: url,
		  cache: false,
		  success: function(code){
			eval(code);
		  }

		});
		
	});

}

function cufon_enhancements() {

	if ( $.browser.msie ) { cufon_ie_enhancements(); return false; }

	// Determine which .special item has the biggest height

	var max_height = 0;

	$('#nav .special').each(function() {

		var h = $(this).height();

		max_height = ( h > max_height ) ? h : max_height;

	});

	// Make the canvas elements grab hover

	$('#nav .special').each(function() {

		$(this).css('position', 'relative');

		var canvas = $(this).find('canvas');
			
		var padding_bottom = max_height - canvas.height() + 35;

		var padding_right = $(this).width() - canvas.width();

		canvas.css({
			'padding': '0 ' + padding_right + 'px ' + padding_bottom + 'px 0',
			'z-index': '2'
		});

		// Activate menu item canvas

		if ( $(this).parents('li').hasClass('current_page_item') ) {

			$(this).parents('li.current_page_item .special').find('a').css('background-image', 'none');

		}

		// Make links the same height

		$(this).css({
			height: max_height + 'px'
		});


	});

	// Hover event on menu items

	$('#nav li:not(.current_page_item) .special').hover(function() {

		$(this).find('small').css('color', theme_color('menu_active_color'));

	}, function() {

		$(this).find('small').css('color', theme_color('menu_desc_color'));

	});

	// Blog posts date alignment

	$('.post .date .month canvas, .post .date .year canvas').css('marginLeft','2px');

}

function init_prettyphoto() {

	var lightbox = $("a[rel^='lightbox']");

	if ( lightbox.length ) {

		$("a[rel^='lightbox']").prettyPhoto({
			animationSpeed: 'normal', /* fast/slow/normal */
			opacity: 0.5, /* Value between 0 and 1 */
			showTitle: true, /* true/false */
			allowresize: true, /* true/false */
			default_width: 500,
			default_height: 344,
			counter_separator_label: '/', /* The separator for the gallery counter 1 "of" 2 */
			theme: 'light_square', /* light_rounded / dark_rounded / light_square / dark_square / facebook */
			hideflash: false, /* Hides all the flash object on a page, set to TRUE if flash appears over prettyPhoto */
			wmode: 'opaque', /* Set the flash wmode attribute */
			autoplay: true, /* Automatically start videos: True/False */
			modal: false, /* If set to true, only the close button will close the window */
			changepicturecallback: function(){}, /* Called everytime an item is shown/changed */
			callback: function(){} /* Called when prettyPhoto is closed */
		});

	}

}

function cufon_ie_enhancements() {

	if ( is_ie7() || is_ie8() ) {

		var heights = [];

		// Fix Descriptions, store heights

		$('#nav .special').each(function() {

			var desc = $(this).find('small').html();

			$(this).find('small').remove();

			$(this).append('<small style="display:block; line-height: 1.5em; margin-top: 3px;">' + desc + '</small>');

			heights.push( $(this).height() );

		});

		// Set optimum height

		var h = maxval(heights);

		$('#nav .special').height(h - 10);

		// Set description hover color

		$('#nav li:not(.current_page_item) .special').hover(function() {

			$(this).find('small').css('color', theme_color('menu_active_color') );

		}, function() {

			$(this).find('small').css('color', theme_color('menu_desc_color') );

		});

	}

}

function topbar_features() {

	if ( topbar_autohide == true ) {

		// Setup globals

		topbar_in_focus = false;

		topbar_TID = 0;

		var topbar = $('#topbar');

		var easing = 'easeOutExpo';

		// Add an extra padding to the body

		$('body').css('paddingTop', '35px');

		// Setup the topbar's positioning

		topbar.css({
			'position'	:	'absolute',
			'zIndex'	:	'11',
			'top'		:	'-35px'
		});

		// Hover event that will trigger the topbar slide

		var anim = 300;

		$('#header').hover(function() {

			topbar.stop().animate({

				'top'	:	'0px'

			}, anim, easing );

		}, function() {

			clearTimeout(topbar_TID);

			topbar_TID = setTimeout(function() {

				if ( topbar_in_focus == true ) {

					// do nothing

					return false;

				} else {

					topbar.stop().animate({

						'top'	:	'-35px'

					}, anim*0.75, easing );

				}

			}, 10);

		});


		// Topbar hover event (booleans)

		topbar.hover(function() {

			topbar_in_focus = true;

		}, function() {

			topbar_in_focus = false;

		});


	}

}