var newsTickerCollection;

// Echo Data Fields
var dNewsTitle;
var dNewsLink;
var dNewsAlert;
var dNewsActive;
var dNewsOrderWeight;
 


function InitNewsTickerEcho(){
	//siteUrl is a relative link to the server
	var siteUrl = '/Data';
  var ctx = new SP.ClientContext(siteUrl);
  //Use .get_current() just for the local context of the page
	  //ctx = new SP.ClientContext.get_current(); 
  
  //Now that we are at the siteUrl, 
  //the .get_lists() will parse through the /Lists path automatically
  var listName = ctx.get_web().get_lists().getByTitle("NewsTicker");

  //It is easier to write the Caml as a string and have a function return it 
  var camlView = GetNewsTickerCaml();
  //var camlViewDefault = "<View><Query/></View>";
  var camlQuery = new SP.CamlQuery();
  camlQuery.set_viewXml(camlView);

  newsTickerCollection = listName.getItems(camlQuery);
	ctx.load(newsTickerCollection);

  ctx.executeQueryAsync(Function.createDelegate(this, this.EchoNewsTickerCollection), Function.createDelegate(this, this.onNewsTickerEchoFailure))
	}


function onNewsTickerEchoFailure(sender, args) {
	//alert("Request Failed: " + args.get_message() + " :: " + args.get_stackTrace());
	}


function EchoNewsTickerCollection(sender, args){
	//alert("EchoNewsTickerCollection");
	
	//If the data is going into the Dom, I've found it easier to "clean" the container
	$('#NewsReel')
		.text("")
		;

	//This is standard code that allows the collection to be enumerated
	//or better said, loaded and inserted into the DOM
	var itemPointer = newsTickerCollection.getEnumerator();
	while(itemPointer.moveNext()){
		var zObject = itemPointer.get_current();

		//local variable = zObject.get_item("NameOfListField");
		//And don't forget  _x0020_  for "Field_x0020_Names_x0020_WithSpaces"
		dNewsTitle = zObject.get_item("Title");
		dNewsLink = zObject.get_item("Link");
			//Validated with Jquery/FireBug
			//Assign Value Object with Jquery.data and then review in FireBug 
			//$2_1 Actual Link Path
			//$6_1 Link Description
			
			//If this was a picture, it would be coded as:
			//	var pictureUrl = zObject.get_item('Picture').get_url();
			dNewsLinkUrl = dNewsLink["$2_1"];
			dNewsLinkDescr = dNewsLink["$6_1"];
		dNewsAlert = zObject.get_item("Alert");
		//alert(dNewsTitle);
		
		InsertNewsEchoItem(dNewsTitle, dNewsLinkUrl, dNewsLinkDescr, dNewsAlert);
		}

	// Initialize post functions if desired after data is Echoed
	InitNewsTicker();
	}



function InsertNewsEchoItem(fnTitle, fnLinkUrl, fnLinkDescr, fnAlert){
	zEchoItem = "";
	if(fnAlert == false){
		zEchoItem += "<div class='newsItem'>";
		}
	else{
		zEchoItem += "<div class='newsItem alertTrue'>";
		}	
	zEchoItem += "<a href='" + fnLinkUrl + "' alt='" + fnLinkDescr + "'>" + fnTitle + "</a>";
	zEchoItem += "</div>";

	$('#NewsReel')
		.append(zEchoItem)
		;

	$('#NewsReel')
		.find('div.newsItem:last')
		.addClass("echoData")
		.data("dNewsTitle",dNewsTitle)
		.data("dNewsLinkUrl",dNewsLinkUrl)
		.data("dNewsLinkDescr",dNewsLinkDescr)
		.data("dNewsAlert",dNewsAlert)
		;
	}// ^^InsertEchoItem
	


function GetNewsTickerCaml(){
	//alert("GetNewsTickerCaml");
	
	//Pay Attention to the <ViewFields> that limits the fields returned
	//Means faster processing
	
  var zNewsTickerCamlView = "" +
  	"<View>" +
  		"<Query>" +

  			"<Where>" +
  				"<Eq>" +
  					"<FieldRef Name='Active'/>" +
  					"<Value Type='Integer'>1</Value>" +
  				"</Eq>" +
  			"</Where>" +

  			"<GroupBy>" +
  				"<FieldRef Name='Alert' Type='Integer' Ascending='FALSE' />" +
  			"</GroupBy>" +

  			"<OrderBy>" +
  				"<FieldRef Name='OrderWeight' Type='Number' Ascending='TRUE' />" +
  			"</OrderBy>" +

  		"</Query>" +
  		"<RowLimit>9</RowLimit>" +
  		"<ViewFields>" +
	  		"<FieldRef Name='Title' />" +
  			"<FieldRef Name='Link' />" +
  			"<FieldRef Name='Alert' />" +
  		"</ViewFields>" +
  	"</View>"
  	;
	return zNewsTickerCamlView;
	}// ^^GetNewsTickerCaml



