// Gets QueryString Parameter
function getParameterByName( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return null;
  else
    return decodeURIComponent(results[1].replace(/\+/g, " "));
}
// Main Arda Js
$(document).ready(function(){
	// Begin PageRedirect Meta Data
	var link = $("meta[name=PageRedirect]").attr("content");
	if (link != null) {
		if (link.indexOf("http") != 0) {
			link = "http://" + link;
		}
		window.location = link;
	}
	// End PageRedirect Meta Data
	
	// Begin Search Setup
	var searchUri = "/arda/search.aspx?type=web&q=";

	// Clear search input on focus
	$("li.search > .searchField").focus(function(){
		$(this).val("");
	});
	
	// Simulates Enter Key Press Submission
	$("li.search > .searchField").keyup(function(event){
		if (event.keyCode == '13')
		{
			event.preventDefault();
			window.location =  searchUri + $(this).val();
			return false;
		}
	});
	
	$("li.search > .btnSiteSearch").click(function(e){		
		e.preventDefault();
		window.location = searchUri + $("li.search > .searchField").val();
		return false;
	});
	// End Search Setup
	
		// Begin Search Issues Callout
	//Clear Default Input Text
	$("#txt_SearchWord").focus(function(){
		$("#txt_SearchWord").val("");
	});
	
	// Simulates Enter Key Press Submission
	$("#txt_SearchWord").keyup(function(e){
		if (e.keyCode == '13')
		{
			e.preventDefault();
			var searchIssueUri = searchUri.replace("type=web&q=","type=legislativeissue");
			var searchIssueParameters = "";

			searchIssueParameters += "&issue_date=" + $("#ddl_SearchDate").val();
			searchIssueParameters += "&issue_type=" + $("#ddl_SearchLocation").val();
			searchIssueParameters += "&q="
			if ( $("#txt_SearchWord").val() != "" && $("#txt_SearchWord").val() != "Search by keyword")
			{
				searchIssueParameters += $("#txt_SearchWord").val();
			}

			window.location =  searchIssueUri + searchIssueParameters;
			return false;
		}
	});

	
	$(".btn_SearchIssues").click(function(e){
		e.preventDefault();
		var searchIssueUri = searchUri.replace("type=web&q=","type=legislativeissue");
		var searchIssueParameters = "";

		searchIssueParameters += "&issue_date=" + $("#ddl_SearchDate").val();
		searchIssueParameters += "&issue_type=" + $("#ddl_SearchLocation").val();
		searchIssueParameters += "&q="
		if ( $("#txt_SearchWord").val() != "" && $("#txt_SearchWord").val() != "Search by keyword")
		{
			searchIssueParameters += $("#txt_SearchWord").val();
		}

		window.location =  searchIssueUri + searchIssueParameters;
		return false;
	});
	// End Search Issues Callout
	
	// From ARDA-in2col
	//$("#NarrowTable1").tablesorter();
	$(".floatRight li:last-child").addClass("last");
	
    // Homepage News Rotator
	$('.homerotation').vTicker({ 
		speed: 500,
		pause: 4000,
		animation: 'fade',
		mousePause: true,
		showItems: 1
	});
	
//	try      
//    {
       //alert("test");
//      $(".narrow").tablesorter(); 
//    } 
//    catch(err){
//      alert(err);
//    }
   if ($("#AIFpage").length) {  
	    $("#logo ").addClass("aifLogo");
}
}); 

// Begin AutoFlowplayer
$(document).ready(function () {
	AutoFlowplayer();
});

// Set to Server Flowplayer SWF path
var flowplayerSwfPath = "/js/flowplayer/flowplayer-3.2.5.swf";

// Edit With Caution!
// Loops through all Anchor tags with "flowplayer" CssClass
function AutoFlowplayer() {
	var regExpressions = ["width=([0-9]{1,4}(px)?)", "height=([0-9]{1,4}(px)?)"];            
	var errorMessage = "AutoFlowplayer Error: {0}";
	var errorHtmlFormat = "<span style='color: red;'>{0}</span>";

	$("a.flowplayer").each(function (i) {
		try {
			var file = String($(this).attr("href"));
			var style = "";

			if (file.indexOf("width=") > -1 && file.indexOf("height=") > -1) {
				var width = String(file.match(regExpressions[0])[1]);
				width = (width.indexOf("px") == -1) ? width + "px" : width; // Adds px if not present

				var height = file.match(regExpressions[1])[1];
				height = (height.indexOf("px") == -1) ? height + "px" : height; // Adds px if not present

				style = "display:block;width:" + width + ";height:" + height + ";";
			}

			// Correct URL Parameters for Flash
			file = file.replace("&amp;", "&").replace("&", "%26");
			$(this).attr("href", file);

			// update ID
			var id = ($(this).attr("id") == "" || $(this).attr("id") == null) ? "AutoFlowplayer_" + i : $(this).attr("id");
			$(this).attr("id", id);

			// set style
			style = ($(this).attr("style") == undefined) ? style : $(this).attr("style");
			$(this).attr("style", style);

			// flowplayer the ID
			if (typeof flowplayer == "function" && style != "") {
				// Strip Subitems if no splash image is found
				if (!$(this+":contains('<img')")){
					$(this).html("");
				}
				flowplayer(id, flowplayerSwfPath);
			}
			else if (typeof flowplayer != "function") {
				throw (errorMessage.replace("{0}", "Flowplayer could not be found."));
			}
			else if (style == "") {
				throw (errorMessage.replace("{0}", "Width and Height could not be determined from HREF URL parameters for the file " + file));
			}
		} catch (err) {
			alert(err);
			$("#" + id).html(errorHtmlFormat.replace("{0}", err));
		}
	});
}
