
 	var $ = glow.dom.get;
	
	
	
/* loads the json and then starts to create the promo (create_promo) */

function load_json(carousel_object)
	{    
		var request = glow.net.get
		(
	
		
		carousel_object.json_path
		, 
			{
		  		onLoad: function(response) 
				{
					var promo_list;
					promo_list = glow.data.decodeJson(response.text()) ; 
						 carousel_object.promo = promo_list;
					
		 			create_promo(carousel_object);
		  		},
		  		onError: function(response) 
				{}
			}
		);
	}
	
	
/********************* CAROUSEL CREATION **********************************************************************/	
/* creates the promo carousel from the json feed if less than 4 items do not create the carousel
uses the helper function create_list_item and add_listeners
 */
function create_promo(carousel_object)
{
	
	carousel_ol = "#" + carousel_object.name + "_carousel";
	$(carousel_ol).empty();	
	var list_items ='';
	for ( var i in carousel_object['promo'][carousel_object.name] )
	{	
		list_items += (create_list_item(carousel_object.build_small_image_url(i),carousel_object.get_small_image_alt(i),carousel_object.build_title_text(i)));
										
	}	

	$(carousel_ol).html(list_items);
	add_listeners(carousel_object);
	highlight_thumb(carousel_object,'0');	
 

  if (carousel_object['promo'][carousel_object.name].length > 0)
	{create_carousel(carousel_object);}	
	else
	{ $(carousel_ol + "_list").addClass('no_carousel')}
	
}

/* helper function for create promo, uses imagec to get the promo thumbnails, */
function create_list_item(image_url,image_alt,text)
{
	if ( image_alt == undefined ) image_alt = '';
	var list_item = '<li tabindex="0"><img src="' + image_url 	+'" alt="' + image_alt + '"/>' + text +  '</li>';
	return list_item;
}

	
	/* creates the carousel, harcodes the size of the carousel due to cross browser issue */
function  create_carousel(carousel_object) 
{
			glow.dom.get("#"+ carousel_object.name + "_carousel_wrapper").css('display','block');
			carousel_ol = "#" + carousel_object.name + "_carousel";
			carousel_object.carousel = new glow.widgets.Carousel
			(
				carousel_ol,
				{
		        loop: false,
		        size: 3,
		        step: 3 
						}
				
			);

			glow.dom.get(carousel_ol).height(carousel_object.the_height);
			glow.dom.get("#" + carousel_object.name + "_carousel_list .carousel-nav").height(carousel_object.the_height);
			glow.dom.get("#"+ carousel_object.name + "_carousel_list .carousel-window").height(carousel_object.the_height);
			if (carousel_object.paginate)
			{
			change_paginator(carousel_object);
			glow.events.addListener(carousel_object.carousel, "afterScroll", function (){change_paginator(carousel_object)});
			}

}	


/********************* END OF CAROUSEL CREATION **********************************************************************/


/************************** LISTENERS *******************************************************************************/
/* Add three listeners, one for when it is clicked, one for hover in and one for hover out, ***also adds a class with the item index which is passed through*/

function add_listeners(carousel_object)
{

	glow.dom.get("ol#" + carousel_object.name + "_carousel").each(function() {
	  glow.dom.get(this).get("Li").each(function(i) {
	    glow.dom.get(this).addClass((i));
		  if (carousel_object['promo'][carousel_object.name].length >4)
			{glow.events.addListener( (this),'click',function () { clicked_promo(carousel_object,i,'1') });}
		  else
			{glow.events.addListener( (this),'click',function () { clicked_promo(carousel_object,i,'0') });}
	if (carousel_object.hover)
	{
		glow.events.addListener(this, 'mouseover', function () { hoverin(carousel_object,i)});
		glow.events.addListener(this, 'mouseout', function () { hoverout(carousel_object,i)});

	} 
	 });
	});
	
	
}


/* listener for carousel item when clicked, swaps out all the images and text and also highlight the right thumb
with helper highlight_thumb*/

function clicked_promo(carousel_object,i,paginate)
{
carousel_object.html_params(i);
if (paginate == '1'){change_paginator(carousel_object);}
carousel_object.swap_images(i);
highlight_thumb(carousel_object,i);	
}


function html_inject(carousel_object,target,source)
{
$("#carousel_wrapper_"+carousel_object.name+" " + target).html(source);	
	
}    

function html_inject_header(carousel_object,target,source)
{  
$("#carousel_wrapper_"+carousel_object.name+" " + target).html('<a href="" id="promo_header">' + source + '</a>');	
	
}



function attr_inject(carousel_object,target,source)
{
$("#carousel_wrapper_"+carousel_object.name+" " + target).attr("href",source) ;
}



function upper_limit(visible,carousel_length)
{ if 
 	( (visible[(visible.length-1)] +1) > carousel_length ) 
 { return carousel_length; }   else {return (visible[(visible.length-1)] +1);}
}


/* changes the pagination */

function change_paginator(carousel_object)
{
	var carousel_name= carousel_object.name; 
	var carousel_length = carousel_object['promo'][carousel_object.name].length;
	var visible = carousel_object.carousel.visibleIndexes();
  
	
	var type= carousel_object.content_type
	if (carousel_object['promo'][carousel_object.name].length >3)
	{	
	$("#"+ carousel_name + "_carousel_browser").html(type + "<i>" +(visible[0]+1) + "-"  + upper_limit(visible,carousel_length) + " of " + carousel_length  + "</i>")
	}	
} 


// Change the class on the thumbmails so the correct one is highlighted
function swap_image(carousel_object,myId,target,image_url,alt,width,height,length)

{

	var $get	= glow.dom.get;
	var $anim	= glow.anim.css;
	if (length){ } else {var length = 0.5;}        
	var myAnim = $anim( target, length, {"opacity" : {to:0}}).start();	
	glow.events.addListener(myAnim, "complete", function() 
	{
		var newImage = new Image(width, height);
		newImage.src = (image_url);
		newImage.alt = alt
		var getRef = glow.net.get
		(newImage.src, 
			{
				onLoad: function(response)
				 {        
					$get( target).attr("src", newImage.src);
					$get(target).attr("width", newImage.width);
					$get(target).attr("height", newImage.height);
					$get(target).attr("alt", newImage.alt);
				  $anim( target, length, {"opacity" : {from:0,to:1}}).start();						
				},
				onError: function(response)
				 {
					 console.log("error" + response);
				}
			}
		);

	});



}


/* searches for each li in ol#carousel then removes the class seleced, then looks for the li with the class passed in(id) and adds selected to it */
function highlight_thumb(carousel_object,id)
{
	glow.dom.get("ol#" + carousel_object.name + "_carousel").each(function() {
			glow.dom.get(this).get("Li").each(function(i) { 
				glow.dom.get(this).removeClass("selected");
	  			});})
				
	if (glow.dom.get("ol#" + carousel_object.name + "_carousel li").hasClass(id))
			{
	  		glow.dom.get("ol#" + carousel_object.name + "_carousel li." + id).addClass('selected')
			}
}


/* following two function either fill or clear carousel_hover_info depending on the mouse state */
function hoverin(carousel_object,i) {$("ol#" +carousel_object.name  +  "_carousel li." + i ).addClass('hover');}

function hoverout(carousel_object,i) {$("ol#" + carousel_object.name + "_carousel li." + i ).removeClass('hover');}     

function string_shorten(string,length,postShortenChracacter)
{
	string = string.substr(0,length);
  string = string.replace(/[ ,]+\S*$/,'') + postShortenChracacter; 
 	return string;

}