﻿$(document).ready(function(){
	var soapEnv =
	"<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/'> \
	<soapenv:Body> \
	     <GetListItems xmlns='http://schemas.microsoft.com/sharepoint/soap/'> \
	        <listName>Brand_Windows</listName> \
	        <viewFields> \
	            <ViewFields> \
	               <FieldRef Name='Title' /> \
	               <FieldRef Name='Image' /> \
	               <FieldRef Name='Link' /> \
	               <FieldRef Name='Window' /> \
	           </ViewFields> \
	        </viewFields> \
	    </GetListItems> \
	</soapenv:Body> \
	</soapenv:Envelope>";
	jQuery.ajax({
	url: "../_vti_bin/lists.asmx",
	type: "POST",
	dataType: "xml",
	data: soapEnv,
	complete: processResult,
	contentType: "text/xml; charset=\"utf-8\""
	});
});

function processResult(xData, status) {
	var browser=navigator.appName;
	if(browser== "Microsoft Internet Explorer"){
		$(xData.responseXML).find("z\\:row").each(function() {
		    image_url = $(this).attr('ows_Image');
		    title = $(this).attr('ows_Title');
		    link = $(this).attr('ows_Link');
		    wind = $(this).attr('ows_Window');
			var Position = image_url.indexOf(','); 
			var Position2 = link.indexOf(',');
			
			function Strip(str, n){
				if (n <= 0)
					return "";
				else if (n > String(str).length)
					return str;
				else
				    return String(str).substring(0,n);
			}	
			function NewWindy(w){
			  if(w==1) {
			    return '_blank' }
			  else {
			       return '_self' }
			}	
			Windy = NewWindy(wind);
			BrandWind = Strip(image_url, Position);
			Linky = Strip(link, Position2);
			//var BWStuff = "<img src='" + BrandWind + "' width='960' height='272' />";
			var BWStuff = "<a href='"+ Linky +"' target='"+ Windy +"'><img src='" + BrandWind + "' width='960' height='272' alt='" + title + "'/></a>";
			var BWNav = "<li><a href='#'><img src='" + BrandWind + "' width='50' height='18' alt='" + title + "'/></a></li>";
			$('#BW').append(BWStuff);		
			$('#BWnav').append(BWNav);
		});
	}	
	else{
		$(xData.responseText).find("z\\:row").each(function() {
		    image_url = $(this).attr('ows_Image');
		    title = $(this).attr('ows_Title');
		    link = $(this).attr('ows_Link');
		    wind = $(this).attr('ows_Window');
		
			var Position = image_url.indexOf(','); 
			var Position2 = link.indexOf(',');
			
			function Strip(str, n){
				if (n <= 0)
					return "";
				else if (n > String(str).length)
					return str;
				else
				    return String(str).substring(0,n);
			}	
			
			function NewWindy(w){
			   if(w==1) {
			       return '_blank' }
			   else {
			       return '_self' }
			}			
			Windy = NewWindy(wind);
			Image = Strip(image_url, Position);
			Linky = Strip(link, Position2);
			var BWStuff = "<a href='"+ Linky +"' target='"+ Windy +"'><img src='" + Image + "' width='960' height='272' alt='" + title + "'/></a>";
			var BWNav = "<li><a href='#'><img src='" + Image + "' width='50' height='18' alt='" + title + "'/></a></li>";
			$('#BW').append(BWStuff);		
			$('#BWnav').append(BWNav);
		});
		
		
	}
	
	$('#BW').cycle({
		fx:     'cover', 
		speed:   1200, 
		timeout: 6000,
		random: 1,
		pause: 1,
		cleartype:  1 ,
	    pager:  '#BWnav', 
    	pagerAnchorBuilder: function(idx, slide) {
    	return '#BWnav li:eq(' + idx + ') a';
    	}  
	});
}            