// GENERAL SITE JS ADDITIONS
// JQUERY ACCORDION MENU for Left Nav
/*
$(function(){
  $("#sCats ul li ul").hide(); 
  $("#sCats ul.toplevel > li > a.exp").click(function() {
  	$(this).addClass("current");
  	var $subnav = $(this).next().next();
		if($subnav.is(":visible")) {
		$subnav.animate({height: "toggle"}, 200).prev().removeClass("current");
		}
		if(!$subnav.is(":visible")) {
		$("#sCats ul li ul:visible").animate({height: "toggle"}, 200).prev().prev().removeClass("current");
		$subnav.animate({height: "toggle"}, 200);
		}
		return false;
	});
});
*/
// EOF Left Nav Menu

// JQUERY DROPDOWN MENU
$(document).ready(function(){
   	//BEGIN CPACK MAIN TABS
   	$(" #nav > ul ").css({
        display: "none"
    }); // Opera Fix
    $("#nav li").hover(function(){
        $(this).addClass("hovered");
        $(this).find('ul:first').css({
            visibility: "visible",
            display: "none"
        }).fadeIn("50").addClass("nothovered");
        
    }, function(){
        $(this).find('ul:first').css({
            visibility: "hidden"
        });
        $(this).removeClass("hovered");
        $(this).removeClass("nothovered");
    });
});

// SEARCH

$(document).ready(function() {
	$("input.search_input").keypress(function(e) {
		if(e.which == 13) {
			sf_search( $(this).val() );
			return false;
		}
	});
	$("input.search_input_ajax").keypress(function(e) {
		if(e.which == 13) {
			set_search( $(this).val()+'*', 'Name','C',true );
			return false;
		}
	});
});

function sf_search(val) {
	window.location.href='/search.php?q=' + val;
}

function sf_ajax_search(id) {
	set_search($(id).val()+'*','Name','C',true);
}

function sf_input_search(id) {
	sf_search($(id).val());	
}

// popup OVERLAY

var overlay_color = '#ccc'; //use #333 for most cases

function open_overlay(overlay_el) {
	$('.overlay_wrap').html('&nbsp;');
	var api = $('.overlay').overlay({
		expose: 		overlay_color,
		effect: 		'apple',
		speed:			'fast',
		api:			true
	});
	if(objPropExists(api.getConf(),'onBeforeLoad')) {
		api.unbind('onBeforeLoad');
	}
	api.onBeforeLoad(function() {
	  	var c = $(overlay_el);
		// grab wrapper element inside content 
		var wrap = this.getContent().find(".overlay_wrap"); 
		if(c.length ==0) {
			wrap.load(overlay_el); 
		} else {
			wrap.html(c.html());	
		}
	});
	api.load();
}


// UTILITIES

//add >= and <= to selectors
$.extend($.expr[':'],{
	egt: function (a, i, m) {
		return i >=m[3]-0;
	},
	elt: function (a, i, m) {
		return i <=m[3]-0;
	}
});

jQuery.fn.tag = function() {
  return this[0] ? this[0].tagName.toLowerCase() : null;
};


/**
*
*
*
UTILITY FUNCTIONS
*
*
*/


function URLEncode (clearString) {
  var output = '';
  var x = 0;
  clearString = clearString.toString();
  var regex = /(^[a-zA-Z0-9_.]*)/;
  while (x < clearString.length) {
    var match = regex.exec(clearString.substr(x));
    if (match != null && match.length > 1 && match[1] != '') {
    	output += match[1];
      x += match[1].length;
    } else {
      if (clearString[x] == ' ')
        output += '+';
      else {
        var charCode = clearString.charCodeAt(x);
        var hexVal = charCode.toString(16);
        output += '%' + ( hexVal.length < 2 ? '0' : '' ) + hexVal.toUpperCase();
      }
      x++;
    }
  }
  return output;
}


function clearFields(className) {
	$("."+ className).each(function() {
		var id = $(this).attr("id");
			$(this).val('');
		
		} );
}

function constructQueryString() {
		if(arguments.length == 1 && typeof(arguments[0]) == "object") {
			arguments = arguments[0];
		}
		var qs = 'function='+arguments[0];
		for(i = 0; i < arguments.length-1; i++) {
			qs += '\&arg_' + i + '=' + URLEncode(arguments[i+1]);
		}
		return qs;
}

function array_append(arr,val){
	arr[arr.length]=val;
}

jQuery.fn.option = function (value) {
return this.each(function() {
        var sel = $(this)[0];
        for ( var i=0; i<sel.length; i++ )
                if (sel[i].value == value)
                        sel.selectedIndex = i;
        });
};

function print_result(result) {
	if (result['result']) {
		show_message(result['message']);
	} else {
		show_error(result['message']);
	}
}

function hide_error() {
	$("#cpack_error_box").hide();
}

function show_error(error_text) { 
	if(error_text.length > 0) {
		$("#cpack_error_box div.warning").html(error_text);	
		$("#cpack_error_box").show();
	}
}

function hide_message() {
	$("#cpack_message_box").hide();
}

function show_message(message_text) {
	if(message_text.length > 0) {
		$("#cpack_message_box div.message").html(message_text);	
		$("#cpack_message_box").show();
		setTimeout(function() { 
							$("#cpack_message_box").fadeOut("slow");
					}, 3500 );
	}
}

function objPropExists(object,property) {
	if(typeof(object) == "undefined") {
		return false;	
	}
	if(typeof(object[property]) != "undefined") {
		return true;	
	}
	return false;
}

function objPropLength(object) {
	var j = 0;
	for(var i in object) {
		j++;
	}
	return j;
}

function copy_form(from, to) {
    $("." + from).each(function() {
        var classes = $(this).attr("class").split(" ");
        var field = classes[1]; //second attribute of class is fieldname
        $("." + field + "." + to).val($(this).val());
    });
}	

function isEmpty(str) {
	var myRe = /^\s*$/;
	return myRe.test(str);	
}

