/*
 *     SCRIPT:	Function Definitions
 *    VERSION:	10.24.2009
 *    UPDATED:	-


   RUNTIME GLOBALS (DO NOT MODIFY)
 * -------------------------------- */
var working = false;
var featured_loop = null;
var menu_timeoutID = null;
pngfix_elements = "div, a, li, img, input";


/* ENABLE JS FEATURES
 * Enables the user interface elements
 * that provide javascript functionality
 * -------------------------------------- */
function enable_js_features() {
	var selector = "";
	selector += "#content #featured #featured-body #featured-nav-ui" + ", ";
	selector += "#content #featured #featured-body a.nav-left," + ", ";
	selector += "#content #featured #featured-body a.nav-right," + ", ";
	//selector += "" + ", ";
	
	// Trim the last comma and space
	var strLen = selector.length;
	selector = selector.slice(0,strLen-2);

	// Show the elements
	jQuery(selector).css('display', 'block');
}

/* CLICK EVENTS
 * Here JS click() events are defined
 * ------------------------------------ */
function click_events() {
	
	$('#back-to-top').click(function(){
		if ( is_IE6() || is_IE7() ) {
			$('html').animate({scrollTop:0}, 700);
			return false;
		}
		
		if ( !$.browser.opera ) {
			$('html,body').animate({scrollTop:0}, {
				duration: 800,
				easing: 'easeInOutExpo'
			});
		} else {
			$('html').animate({scrollTop:0}, {
				duration: 800,
				easing: 'easeInOutExpo'
			});
		}
		return false;
	});

	$('#featured-body a.nav-left, #featured-body a.nav-right').click(next_featured);

	$('a.close-btn').click(function() {
		
		if ( ! is_IE6() ) {
			$(this).parent().fadeOut(forms_notification_delay);
			return false;
		} 
		
	});

}

/* STYLE ADJUSTMENTS
 * Correct minor rendering inconsistencies
 * across browsers.
 * --------------------------------------- */
function style_adjustments() {
	
	// For some reason, Gecko based browsers add a 10px right margin to .sub-navigation
	if ($.browser.mozilla) {
		$('#content #top-section ul.sub-navigation').css('right', '-10px');
		$('#content #main #left .post .post-contents ul.tags li a').css('padding-right', '0');
	}
	
}


/* HOVER EVENTS
 * Here JS hover() events are defined
 * ------------------------------------ */
function hover_events() {

	if ( stop_featured_cycle_on_hover == true ) {
		$('#featured-content p, #featured-content h1, #featured-content a.button, #featured-body .frame img').hover(function() {
			clearTimeout(featured_loop);
		}, function() {
			if (enable_featured_cycle == true) { featured_cycle(); }
		});
	}
	
	$('#searchform').hover(function() {
		
		$('#search-focus-block').css('display', 'none');
		
	});
	
}

/* FORMS INITIALIZATION
 * Initializes forms, AJAX & Validation
 * --------------------------------------- */
function init_forms() {
	
	if ( ! is_IE6() ) {
		
		$('input[type=submit]').click(function() {
			
			$('input[name=ajax]').attr('value','true');
			
			var form = $(this).parents('form');

			if ( form.children('.post-msg').css('display') == 'block' ) { 
				return false;
			}
			
			if ( validate_contact_form(form) == false ) { return false; }
				
			var action = form.attr("action");
			var data = form.dataForAjax();
			var notification = form.children('.post-msg');
			
			$.ajax({
			   type: "POST",
			   url: action,
			   data: data,
			   success: function(response){
				 
				 if ( response == 'success' ) {
				 	form.get(0).reset();
					notification.fadeIn(forms_notification_delay);
				 }
			   }
			 });
			
			return false;
		});	
	}
	
}

/* FORM VALIDATION FRAMEWORK
 * Flexible Validation Mechanism
 * --------------------------------------- */
function validate_contact_form( form ) {
	var regexp_types = [ 'name', 'email', 'message' ]
	
	var regexps = {
		'name' : /[a-zA-Z ]{2,30}/i,
		'email' : /[\w-+]+(?:\.[\w-+]+)*@(?:[\w-]+\.)+[a-zA-Z]{2,7}/i,
		'message' : /.+/i
	}

	var valid = true;
	for ( var i=0; i < regexp_types.length; i++ ) {
		var name = regexp_types[i];
		var rx = regexps[name];
	
		var input = jQuery('input[name=' + name + ']');
		var textarea = jQuery('textarea[name=' + name + ']');
		
		if ( input.length == 1 ) {
			if ( input.val().match(rx) == input.val() ) {
				input.removeClass('invalid');
			} else {
				valid = false;
				input.addClass('invalid');
			}
		}
		
		if ( textarea.length == 1 ) {
			if ( textarea.val().match(rx) == textarea.val() ) {
				textarea.removeClass('invalid');
			} else {
				valid = false;
				textarea.addClass('invalid');
			}
		}
	}
	
	return valid;
	
}

/* LIGHTBOX INITIALIZATION
 * Prettyphoto Lightbox configuration. 
 * --------------------------------------- */
function init_lightbox() {
	
	if ( enable_lightbox == true ) {

		$("a[rel^='lightbox']").prettyPhoto({
			animationSpeed: lightbox_animation_speed, /* fast/slow/normal */
			padding: lightbox_padding, /* padding for each side of the picture */
			opacity: lightbox_opacity, /* Value betwee 0 and 1 */
			showTitle: lightbox_showtitle, /* true/false */
			allowresize: lightbox_allowresize, /* true/false */
			counter_separator_label: '/', /* The separator for the gallery counter 1 "of" 2 */
			theme: lightbox_theme, /* light_rounded / dark_rounded / light_square / dark_square */
			hideflash: lightbox_hideflash, /* Hides all the flash object on a page, set to TRUE if flash appears over prettyPhoto */
			modal: lightbox_modal_state, /* 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 */
		});

	}
	
}

/* MENU INITIALIZATION
 * 
 * Initializes the menu and makes sure
 * it is cross platform and cross browser.
 * 
 * On IE6, the dropdown menu will only
 * work with JS enabled, since IE lacks
 * support for CSS's > selector, which is
 * used to unhide the unordered list.
 * --------------------------------------- */
function init_menu() {
	
	if ( is_IE6() ) {
		
		// Apply Blue Theme Fixes
		var bg = $('#content').css('background-image');
		if ( bg.indexOf('/blue/') != -1 ) {
				$('#header .container ul#navigation li ul').css({
					'background-image' : 'none',
					'background-color' : '#E1E1E1'
				});	
				
				$('#header .container ul#navigation li ul li a').css({'border-color' : 'E1E1E1'	}).hover(function() {
					$(this).css('border-color', '#cacaca');
				}, function() {
					$(this).css('border-color', '#E1E1E1');
				});;
		}
		
		
		$('#header .container ul#navigation li').hover(function() {

			if ( $(this).children().length > 1 ) { 

				var _this = $(this);
				
				clearTimeout(menu_timeoutID);
				menu_timeoutID = setTimeout(function() {

					_this.find('ul:first').css({ visibility: "visible", display: "none" }).slideDown(menu_dropdown_duration, menu_ease_equation);

				},menu_show_delay);

			}			
			
		}, function() {

			clearTimeout(menu_timeoutID);
			$(this).find('ul:first').css({visibility: "hidden"});
			
		});
		
	} else {
		
		if ( is_IE7() ) {
			$('#header .container ul#navigation li ul').css('margin-left', '0');
		}
		
		$('#header .container ul#navigation li').hover(function() {
			
			$(this).find('ul:first').css({'visibility': 'hidden', 'display': 'none'});
			
			var _this = $(this);
		
			clearTimeout(menu_timeoutID);
			menu_timeoutID = setTimeout(function() {
				$('#search-focus-block').css('display', 'block');
				_this.find('ul:first').css({ visibility: "visible", display: "none" }).slideDown(menu_dropdown_duration, menu_ease_equation);
			},menu_show_delay);
				
		}, function() {
			
			clearTimeout(menu_timeoutID);
			$(this).find('ul:first').css({visibility: "hidden"});
				
		});
	}
}

/* FEATURED HOME INITIALIZATION
 * Configures, Retrieves and Creates elements
 * needed for this section to work. The cache
 * class is used here. 
 * --------------------------------------- */
function init_featured_home() {

	var featured_cache = $('#featured-items');
	var featured_nav = $('#featured-nav-ui');
	var items = featured_cache.children().length;

	for ( var i=1; i < items; i++ ) {
		featured_nav.append('<li><a rel="' + (i+1) + '" href="#"></a></li>');
	}

	featured_nav_click();
	
	if (enable_featured_cycle == true) { featured_cycle(); }
	
}

/* FEATURED CYCLE
 * Cycles the featured items an interval
 * defined by featured_cycle_interval. 
 * --------------------------------------- */
function featured_cycle() {

	if ( featured_cycle_interval < 60 ) { featured_cycle_interval *= 1000 }

	featured_loop = setInterval(function() {
		
		next_featured();
		
	},featured_cycle_interval);

}

/* NEXT FEATURED
 * Smart & Multipurpose function that 
 * changes the featured items. Also serves
 * as the click() event handler for the
 * featured content switchers. 
 * --------------------------------------- */
function next_featured() {
	var increment = 1;
	if ( $(this).hasClass('nav-left') ) { increment = -1; clearInterval(featured_loop); featured_loop = null; }
	if ( $(this).hasClass('nav-right') ) { increment = 1; clearInterval(featured_loop); featured_loop = null; }
	
	var items = $('#featured-items').children().length;
	var current = $('#featured-nav-ui li.active a').attr('rel');
	var next = cycle(parseInt(items), parseInt(current), increment );

	var selector = '#featured-nav-ui li a[rel=' + next + ']';
	$(selector).parent().click();

	return false;

}

/* FEATURED NAV ELEMENTS CLICK
 * Handles the switching when the featured
 * items navigation (bottom right) elements
 * are clicked.
 * --------------------------------------- */
function featured_nav_click() {
	
	$('#featured-nav-ui li').click(function() {
		if ( $(this).hasClass('active') || working == true ) { return false; }
		
		working = true;
		
		var featured_cache = $('#featured-items');
		var num = $(this).children().eq(0).attr('rel') - 1;
		var block = featured_cache.children().eq(num);
		var h1 = block.children().eq(0).text();
		var img = block.children().eq(1).attr('src');
		var p = block.children().eq(2).text();
		var href = block.children().eq(3).attr('href');
		
		var duration = 900;
		
		var text_fade_duration = duration*0.4


		$('#featured-nav-ui li').removeClass('active');
		$(this).addClass('active');
		
		$('#featured-body img.back').attr('src', img);
		
		$('#featured-body img.front').stop().animate({
			'opacity' : '0'
		},duration);
		
		setTimeout(function() {
			$('#featured-body img.back').attr('id', 'featured-back-image');
			$('#featured-body img.front').attr('id', 'featured-front-image');
			$('#featured-body img').removeClass();
			$('#featured-back-image').addClass('front').attr('id','');
			$('#featured-front-image').addClass('back').css('opacity', '1').attr('id', '');
			working = false;
			if ( featured_loop == null ) { 
				if (enable_featured_cycle == true) { featured_cycle(); }
			}
		},duration+30);

		if ( !$.browser.msie || disable_text_fade_on_ie == false ) {
			$('#featured-content h1, #featured-content p').stop().animate({
				'opacity' : '0'
			},text_fade_duration);
		}

		setTimeout(function() {
			$('#featured-content h1').text(h1);
			$('#featured-content p').text(p);
			$('#featured-content a.button').attr('href', href);
			
			if ( !$.browser.msie || disable_text_fade_on_ie == false ) {
				$('#featured-content h1, #featured-content p').animate({
					'opacity' : '1'
				},text_fade_duration);
			}
	
		},text_fade_duration+30);

		return false;
	});
}

/* CYCLE
 * Cycling Algorithm
 * --------------------------------------- */
function cycle(range, current, increment) {
	var item = current + increment;
	if ( item == 0 ) { item = range; }
	if ( item > range ) { item = 1;	}
	return item;
}

/* TARGET _BLANK SUPPORT IN JS
 * The XHTML 1.0 Strict doctype, doesn't
 * support the target="_blank" attribute,
 * so it's added on JS in order to validate.
 * --------------------------------------- */
function setup_external_links() {
	$('a[rel=_blank]').attr('target', '_blank');
}

/* IE6 DETECTION
 * Detects Internet Explorer 6
 * --------------------------------------- */
function is_IE6() {
	return (navigator.appVersion.indexOf("MSIE 6.")==-1) ? false : true;
}

/* IE7 DETECTION
 * Detects Internet Explorer 7
 * --------------------------------------- */
function is_IE7() {
	return (navigator.appVersion.indexOf("MSIE 7.")==-1) ? false : true;
}

/* IE6/7 DETECTION
 * Detects Internet Explorer 6 and/or 7
 * --------------------------------------- */
function is_IE67() {
	if ( is_IE6() || is_IE7() ) {
		return true;
	} else {
		return false;
	}
}

/* EOF */
