
	


	
	var carousels = {
		// distance between offers in pixels
		dx : [6,40,30],
	
		// these settings are used in calculations and are not settings do not mess with them
		 offer_images_loaded: [0,0,0],
		 extra_objects:[0,0,0],	 
		 max_objects:[0,0,0],
		 current_offer:[0,0,0],
		 original_offers:[0,0,0]
	}


	
   $(document).ready(function(){
    
	
	
	//-- For eacho carousell on the page, you need to count the number of offers originally and make buttons for each one.
		$( ".lego20_flexi_offer_carousel_main" ).each(
		
			function( intIndex ){
				obj = $(this);
			
				my_parent =obj.attr("id");
				my_parent_id = (parseInt(my_parent.substr(9)));		
				obj.children(".lego20_flexi_offer_carouse_inner").children(".lego20_flexi_offer_carouse_middle_holder").children().length;
				carousels['original_offers'][my_parent_id] = obj.children(".lego20_flexi_offer_carouse_inner").children(".lego20_flexi_offer_carouse_middle_holder").children().length;				
			
			
				for (i=2;i<=carousels['original_offers'][my_parent_id];i++)
					{
					 node2= obj.find(".lego20_flexi_offer_carousel_offer_tabs_holder .lego20_flexi_offer_carousel_offer_tabs .lego20_flexi_offer_carousel_offer_tab:last").clone(false)
					 node2.html(i)
					 obj.find(".lego20_flexi_offer_carousel_offer_tabs_holder .lego20_flexi_offer_carousel_offer_tabs .lego20_flexi_offer_carousel_offer_tab:last").after(node2)
					}
			
			
			}
	 
		);
	
	
	//	-- 703 929 3473
	
		
		init_page();
	
	
	
	
	
	/*------This function resizes all the carouselss---------------*/
	var resizeTimer = null;
	$(window).bind('resize', function() {
		if (resizeTimer) clearTimeout(resizeTimer);
		resizeTimer = setTimeout(init_page, 500);
	});
	/*------This function resizes all the carouselss END ---------------*/

	function init_stuff(obj) {
	
		m = obj.width();																						//-----	width of the screen obtained from div with id of main
		
		my_parent =obj.attr("id");
		my_parent_id = (parseInt(my_parent.substr(9)));
		
		dx = carousels['dx'][my_parent_id];																		//-----	distance between offers in pixels
		
		//-----	get the width of an offer div
		c = parseInt(obj.children(".lego20_flexi_offer_carouse_inner").children(".lego20_flexi_offer_carouse_middle_holder").children(".lego20_flexi_offer_carouse_offer_block").width());	
		
		//-----	an offer block takes up this much space.
		obw = c + dx;	
							
		//-----	get the height of an offer div
		obh = obj.children(".lego20_flexi_offer_carouse_inner").children(".lego20_flexi_offer_carouse_middle_holder").children(".lego20_flexi_offer_carouse_offer_block").height();				
		
		//-----	set the height of the ''innder' div to the height of the offer divs (assumes all the same height)
		obj.children(".lego20_flexi_offer_carouse_inner").height(obh);																		
		
		if (typeof(old_max_objects) == "undefined") {
			old_max_objects = carousels['max_objects'][my_parent_id];
		}

		if (old_max_objects <carousels['max_objects'][my_parent_id]) {
		old_max_objects = carousels['max_objects'][my_parent_id];
		}
		
		carousels['max_objects'][my_parent_id] = Math.floor(m/obw);											//-----	calculate the maximum number of objects that will fit in the width of the browser
																												// -- here we calculate if we have an odd or even number of 
																												//objects that will fit. if its even we subtract 1.
			is_even = carousels['max_objects'][my_parent_id]%2;
			
			if (is_even != 1) {
				carousels['max_objects'][my_parent_id]--;		
			}
			
		// ---------------------------------------------------------------------------------------------------------------------------
	
	
	  carousels['extra_objects'][my_parent_id] =  Math.floor((carousels['max_objects'][my_parent_id])*2);
		
		
	 if (old_max_objects < carousels['max_objects'][my_parent_id]) {
		
	
	duplicates = (Math.floor(carousels['max_objects'][my_parent_id] / carousels['original_offers'][my_parent_id]) + 2)/2
	
	
	x= 0;
	
	
	
	
	while (x<=duplicates) 
	 {
		 var node2 = obj.children(".lego20_flexi_offer_carouse_inner").children(".lego20_flexi_offer_carouse_middle_holder").children().clone(false);
		  obj.children(".lego20_flexi_offer_carouse_inner").children(".lego20_flexi_offer_carouse_middle_holder").append(node2)
		
		
		x++;  
		
		 
		}
		
		 obj.children(".lego20_flexi_offer_carouse_inner").children(".lego20_flexi_offer_carouse_middle_holder").children().css("margin-left",.5*carousels['dx'][my_parent_id]);
		 obj.children(".lego20_flexi_offer_carouse_inner").children(".lego20_flexi_offer_carouse_middle_holder").children().css("margin-right",.5*carousels['dx'][my_parent_id]);
	
		
	}	
		
		//obj.children(".lego20_flexi_offer_carouse_inner").width(carousels['max_objects'][my_parent_id]*obw);
				obj.children(".lego20_flexi_offer_carouse_inner").width(976);

	
	}
	

function slide_offers(obj, id_to_move) {
	
	
		my_parent =obj.attr("id");
		my_parent_id = (parseInt(my_parent.substr(9)));
	
	
		my_int   = parseInt(id_to_move) -1 ;
		your_int = parseInt(carousels['original_offers'][my_parent_id]);
		

		
		my_clicks= my_int + your_int -1
		c = parseInt(obj.children(".lego20_flexi_offer_carouse_inner").children(".lego20_flexi_offer_carouse_middle_holder").children(".lego20_flexi_offer_carouse_offer_block").width());
		m = obj.width();	
		
		a = (m/(c+carousels['dx'][my_parent_id]));
		
		mid = m / 2; 	
		
		dx = carousels['dx'][my_parent_id];																		//-----	distance between offers in pixels
		
		c = parseInt(obj.children(".lego20_flexi_offer_carouse_inner").children(".lego20_flexi_offer_carouse_middle_holder").children(".lego20_flexi_offer_carouse_offer_block").width());	//-----	get the width of an offer div
		obw = c + dx;	
		
		//clicks = ((carousels['max_objects'][my_parent_id]-1)/2) - my_clicks
		clicks = (((carousels['max_objects'][my_parent_id] - 1)/2) ) - my_clicks - carousels['max_objects'][my_parent_id] +1;
		
		
		np = (clicks  * obw)-dx
		
		obj.children(".lego20_flexi_offer_carouse_inner").children(".lego20_flexi_offer_carouse_middle_holder").animate({left: np }, "slow", "linear")

		carousels['current_offer'][my_parent_id] = my_int +1
		
		
		obj.children(".lego20_flexi_offer_carousel_offer_tabs_holder").children(".lego20_flexi_offer_carousel_offer_tabs").children().each(
		
		function( intIndex ){
		
		
				if ((intIndex) == my_int+1) {
								//alert(intIndex+ "==" + my_int + $(this).attr("id"))

		 			$(this).addClass("lego20_flexi_offer_carousel_offer_tab_highlight")
				} else {
				$(this).removeClass("lego20_flexi_offer_carousel_offer_tab_highlight")
				}
		}
		
		
		)
		//obj.children(".lego20_flexi_offer_carousel_offer_tabs_holder").children(".lego20_flexi_offer_carousel_offer_tabs").children().addClass("offer_tab_highlight")

	}


	function advance_offers(obj,dir) {
	 carousels['current_offer'][my_parent_id] = carousels['current_offer'][my_parent_id] + dir
	 
	if (carousels['current_offer'][my_parent_id] > carousels['original_offers'][my_parent_id]) {
	 
	 carousels['current_offer'][my_parent_id] = 1
	 }
	 
	  if (carousels['current_offer'][my_parent_id] < 1) {
	 
	 carousels['current_offer'][my_parent_id] =  carousels['original_offers'][my_parent_id]
	 }
	 
	slide_offers(obj,carousels['current_offer'][my_parent_id])
	}

    function init_page() {
 
	 
	 	$( ".lego20_flexi_offer_carousel_main" ).each(
		
			function( intIndex ){
				obj = $(this);
			
				init_stuff(obj);
			
				slide_offers(obj,1);
				
				
			}
	 
		);
	 
	
	}
	

	/* -------- This function defines what happens when you click on a  offer number -----*/
    $('.lego20_flexi_offer_carousel_offer_tab').click(
		function () {
			var name = $(this).html();
			obj = $(this).parents(".lego20_flexi_offer_carousel_main");
			slide_offers(obj,name);
	    }
	);
	
	
	 $('.lego20_flexi_offer_carousel_arrows_left').click(
		function () {
			obj = $(this).parents(".lego20_flexi_offer_carousel_main");
			advance_offers(obj,-1);
	    }
	);
	
	
	
	 $('.lego20_flexi_offer_carousel_arrows_right').click(
		function () {
			obj = $(this).parents(".lego20_flexi_offer_carousel_main");
			advance_offers(obj,1);
	    }
	);
	
	

	$(".lego20_s190_token_offerblock_3_plandetails_tab").click(function()
  		{
    	$(this).next(".lego20_s190_token_offerblock_3_plan_details_info").toggle(600);
 	 });
	
	

  });
  

