// JavaScript Document
var company = "Migliore Technologies Inc.";
var version = 1.0;

// jQuery overloader
function M( selector ){
	if(jQuery){
		if ( typeof( jQuery( selector) ) == "object")
			return jQuery( selector);
		else 
			return jQuery( selector )[0];
	}
}

function onSelectItem(row){jQuery("input#s").focus();}

function formatItem(row) {var attr;if(row.length == 3){attr = "attr=\"" + row[2] + "\"";} else {attr = "";} return row[0] + "<span "+attr+"></span>"}



// Onload functionality
window.onload = function(){
	M('#language').onmouseover = swapFlag;
	M('#language').onmouseout = swapFlag;
}

// OnReady functionality
jQuery(document).ready(function() {
		
	var options = {	delay:10, minChars:2, matchSubset:1, matchContains:1, cacheLength:10, formatItem:formatItem, onItemSelect:onSelectItem, autoFill:false };
	jQuery("#s").autocomplete(url + "/wp-content/plugins/autocompleter/values.php",options);
	jQuery("#s").bind('click focus', clearSearch);
	
	jQuery(".content").hide();
	//toggle the componenet with class msg_body
	jQuery("#logo").click(function()
	{
	  jQuery(".content").slideToggle(500);
	});
	
	// Now jCarousel
	// Right now need to hack it to only show up on the homepage
	// Future to detect if jcarousel function is defined and then load it
	if( $('#migcarousel') )
	   $('#migcarousel').jcarousel();
			  
	  // End of pageload code
});


// Onunload functionality
window.onunload = function(){
		
}


// Clear Search Box when active
function clearSearch()
{
	if( this.value == "Enter Search Terms"){
		this.value =''; 
		this.style.color='#000'; 
		this.style.fontStyle='normal'; 
	}
}


// Swap the current flag with its on state
function swapFlag(){
	var flag_image = M("#language_flag");
	var flag_image_src = flag_image.src;
	var flag_image_pieces = flag_image_src.split("/");
	var image_name = flag_image_pieces[ (flag_image_pieces.length - 1) ];
	
	// Check for "off" if there, put the original
	// If not, put _off in filename before .png
	if(image_name.indexOf("_off") == -1){
		var image_name_pieces = image_name.split(".");
		var new_image_name = image_name_pieces[0] + "_off.png";
	} else {
		var image_name_pieces = image_name.split("_");
		var new_image_name = image_name_pieces[0] + ".png";		
	}
	var new_image_src = url + "/wp-content/themes/migliore/images/flags/" + new_image_name;
	flag_image.src = new_image_src;	
}


// Prepare the search box for site search
function showLanguages(){
	alert("Coming Soon");
	return false;
	lang_box = true;
	$('#languages').toggleClass('visible', 2000);
	var lang_box = $('#language_selector');
	
	elPosition = lang_box.position();
	
	$('#languages').css({
		position : 'absolute',
		width: lang_box.width,
		top : elPosition.top + 25,
		left : elPosition.left
	});
	
}

function doSearch(){
	var q =  $('#s')[0].value;
	if(q != "" && q != "Enter Search Terms")
		window.location = url + "search/"+q;
}	
