// Speed of the automatic slideshow
var slideshowSpeed = 9000;
var item_width = 0;
var left_value = 0;
var left_indent = 0;
var total = 3;//total number of news items
var count = 1;

$(document).ready(function() {

	var fl1 = document.getElementById('firstline1').innerHTML;
	var sl1 = document.getElementById('secondline1').innerHTML;
	var fl2 = document.getElementById('firstline2').innerHTML;
	var sl2 = document.getElementById('secondline2').innerHTML;
	var fl3 = document.getElementById('firstline3').innerHTML;
	var sl3 = document.getElementById('secondline3').innerHTML;
	var fl4 = document.getElementById('firstline4').innerHTML;
	var sl4 = document.getElementById('secondline4').innerHTML;
	var fl5 = document.getElementById('firstline5').innerHTML;
	var sl5 = document.getElementById('secondline5').innerHTML;
	var fl6 = document.getElementById('firstline6').innerHTML;
	var sl6 = document.getElementById('secondline6').innerHTML;
	var fl7 = document.getElementById('firstline7').innerHTML;
	var sl7 = document.getElementById('secondline7').innerHTML;
	
	var photos = [ 
		{
			"title" : "Sterling Funding",
			"image" : "banner_funding.jpg",
			"url" : "http://www.sterlingfunding.com",
			"target" : "_self",
			"firstline" : fl5,
			"secondline" : sl5,
			"firstlineStyle" : {"text-align" : "left","font-weight" : "bold", "font-size" : "34px","float" : "left", "color" : "#007667", "margin-left" : "115px", "margin-top" : "135px", "line-height" : "34px"},
			"secondlineStyle" : {"text-align" : "left","font-size" : "20px","float" : "left", "color" : "#09347a", "margin-left" : "115px", "margin-top" : "2px", "line-height" : "20px", "width" : "350px"} ,
			"learnmoreStyle" : {"display":"block","float" : "left", "margin-left" : "180px", "margin-top" : "2px", "width" : "139px", "height" : "47px",  "background-image" : "url(/assets/images/btn_learn_more.png)"}
		},{
			"title" : "Machine",
			"image" : "banner_cardswipe.jpg",
			"url" : "/merchants/",
			"target" : "_self",
			"firstline" : fl1,
			"secondline" : sl1,
			"firstlineStyle" : {"text-align" : "right","font-size" : "48px","float" : "right", "color" : "#fff", "margin-right" : "32px", "margin-top" : "30px", "line-height" : "48px"},
			"secondlineStyle" : {"text-align" : "right","font-size" : "26px","float" : "right", "color" : "#fff", "margin-right" : "32px", "margin-top" : "16px", "line-height" : "26px", "margin-left" : "0px", "width" : "350px"},
			"learnmoreStyle" : {"display":"block","float" : "right", "margin-right" : "42px", "margin-top" : "8px", "width" : "139px", "height" : "47px",  "background-image" : "url(/assets/images/btn_learn_more.png)", "margin-left" : "0px"}
		/*},{
			"title" : "Durbin",
			"image" : "banner_congress.jpg",
			"url" : "/news/sterling-news/durbin-debit-savings-with-sterling-payment-technologies/",
			"target" : "_self",
			"firstline" : fl6,
			"secondline" : sl6,
			"firstlineStyle" : {"text-align" : "left","font-size" : "52px","float" : "left", "color" : "#4d4d4f", "margin-left" : "32px", "margin-top" : "40px", "line-height" : "50px"},
			"secondlineStyle" : {"text-align" : "left","font-size" : "24px","float" : "left", "color" : "#4d4d4f", "margin-left" : "32px", "margin-top" : "5px", "line-height" : "24px", "width" : "500px"},
			"learnmoreStyle" : {"display":"block", "float" : "left", "margin-left" : "42px", "margin-top" : "0px", "width" : "139px", "height" : "47px",  "background-image" : "url(/assets/images/btn_learn_more.png)"}
		*/
		},{
			"title" : "Best Channel Vendor",
			"image" : "banner_bcv.jpg",
			"url" : "/pos_resellers/",
			"target" : "_self",
			"firstline" : fl6,
			"secondline" : sl6,
			"firstlineStyle" : {"text-align" : "left","font-size" : "34px","float" : "left", "color" : "#bfe0ff", "margin-left" : "32px", "margin-top" : "40px", "line-height" : "34px"},
			"secondlineStyle" : {"text-align" : "left","font-size" : "24px","float" : "left", "color" : "#ffffff", "margin-left" : "32px", "margin-top" : "5px", "line-height" : "24px", "width" : "500px"},
			"learnmoreStyle" : {"display":"block", "float" : "left", "margin-left" : "42px", "margin-top" : "0px", "width" : "139px", "height" : "47px",  "background-image" : "url(/assets/images/btn_learn_more.png)"}
		},{
			"title" : "All Cards",
			"image" : "mainImage_2.jpg",
			"url" : "/merchants/",
			"target" : "_self",
			"firstline" : fl3,
			"secondline" : sl3,
			"firstlineStyle" : {"text-align" : "left","font-size" : "48px","float" : "left", "color" : "#fff", "margin-left" : "50px", "margin-top" : "30px", "line-height" : "48px"},
			"secondlineStyle" : {"text-align" : "left","font-size" : "26px","float" : "left", "color" : "#fff", "margin-left" : "50px", "margin-top" : "16px", "line-height" : "26px"},
			"learnmoreStyle" : {"display":"block","float" : "left", "margin-left" : "56px", "margin-top" : "8px", "width" : "139px", "height" : "47px",  "background-image" : "url(/assets/images/btn_learn_more.png)"}
		},{
			"title" : "bbb",
			"image" : "banner_handshake.jpg",
			"url" : "http://www.bbb.org/west-florida/business-reviews/credit-card-processing-companies/sterling-payment-technologies-in-tampa-fl-11003003#sealclick",
			"target" : "_new",
			"firstline" : fl2,
			"secondline" : sl2,
			"firstlineStyle" : {"text-align" : "left","font-size" : "48px","float" : "left", "color" : "#4d4d4f", "margin-left" : "50px", "margin-top" : "30px", "line-height" : "48px"},
			"secondlineStyle" : {"text-align" : "left","font-size" : "26px","float" : "left", "color" : "#4d4d4f", "margin-left" : "50px", "margin-top" : "10px", "line-height" : "24px"},
			"learnmoreStyle" : {"display":"block","float" : "left", "margin-left" : "56px", "margin-top" : "8px", "height" : "88px", "width" : "140px",  "background-image" : "url(/assets/images/bbb_seal.png)"}
		},{
			"title" : "Spree",
			"image" : "mainImage_5.jpg",
			"url" : "/products/spree/",
			"target" : "_self",
			"firstline" : fl4,
			"secondline" : sl4,
			"firstlineStyle" : {"text-align" : "left","font-size" : "48px","float" : "left", "color" : "#4d4d4f", "margin-left" : "50px", "margin-top" : "40px", "line-height" : "48px"},
			"secondlineStyle" : {"text-align" : "left","font-size" : "26px","float" : "left", "color" : "#4d4d4f", "margin-left" : "50px", "margin-top" : "0px", "line-height" : "26px", "width" : "500px"},
			"learnmoreStyle" : {"display":"block","float" : "left", "margin-left" : "56px", "margin-top" : "8px", "width" : "139px", "height" : "47px",  "background-image" : "url(/assets/images/btn_learn_more.png)"}
		},{
			"title" : "Sterling Virtual Terminal",
			"image" : "banner_vt.jpg",
			"url" : "/products/sterling-virtual-terminal/",
			"target" : "_self",
			"firstline" : fl7,
			"secondline" : sl7,
			"firstlineStyle" : {"text-align" : "left","font-size" : "36px","float" : "left", "color" : "#1090a2", "margin-left" : "50px", "margin-top" : "25px", "line-height" : "36px"},
			"secondlineStyle" : {"text-align" : "left","font-size" : "20px","float" : "left", "color" : "#4d4d4f", "margin-left" : "50px", "margin-top" : "0px", "line-height" : "20px", "width" : "500px"},
			"learnmoreStyle" : {"display":"block","float" : "left", "margin-left" : "56px", "margin-top" : "5px", "width" : "200px", "height" : "47px",  "background-image" : "url(/assets/images/btn_learn_more.png)"}
		}
	];



	jQuery('#mycarousel').jcarousel();
	    
	    
	// Backwards navigation
	$("#back").click(function() {
		stopAnimation();
		navigate("back");
	});
	
	// Forward navigation
	$("#next").click(function() {
		stopAnimation();
		navigate("next");
	});
	
	var interval;
	$("#control").toggle(function(){
		stopAnimation();
	}, function() {
		// Change the background image to "pause"
		$(this).css({ "background-image" : "url(/assets/images/btn_pause.png)" });
		
		// Show the next image
		navigate("next");
		
		// Start playing the animation
		interval = setInterval(function() {
			navigate("next");
		}, slideshowSpeed);
	});
	
	
	var activeContainer = 1;	
	var currentImg = 0;
	var animating = false;
	var navigate = function(direction) {
		// Check if no animation is running. If it is, prevent the action
		if(animating) {
			return;
		}
		
		// Check which current image we need to show
		if(direction == "next") {
			currentImg++;
			if(currentImg == photos.length + 1) {
				currentImg = 1;
			}
		} else {
			currentImg--;
			if(currentImg == 0) {
				currentImg = photos.length;
			}
		}
		
		// Check which container we need to use
		var currentContainer = activeContainer;
		if(activeContainer == 1) {
			activeContainer = 2;
		} else {
			activeContainer = 1;
		}
		
		showImage(photos[currentImg - 1], currentContainer, activeContainer);
		
	};
	
	var currentZindex = -1;
	var showImage = function(photoObject, currentContainer, activeContainer, styleLine1obj, styleLine2obj) {
		animating = true;
		
		// Make sure the new container is always on the background
		currentZindex--;
		
		// Set the background image of the new active container
		$("#headerimg" + activeContainer).css({
			"background-image" : "url(/assets/images/homepageBanners/" + photoObject.image + ")",
			"display" : "block",
			"z-index" : currentZindex
		});
		
		// Hide the header text
		$("#headertxt").css({"display" : "none"});
		
		// Set the new header text
		$("#firstline")
			.html(photoObject.firstline)
			.css(photoObject.firstlineStyle);
		$("#secondline")
			.attr("href", photoObject.url)
			.html(photoObject.secondline)
			.css(photoObject.secondlineStyle);
		$("#learnmore")
			.attr("href", photoObject.url)
			.attr("target", photoObject.target)
			.css(photoObject.learnmoreStyle);	

		
		// Fade out the current container
		// and display the header text when animation is complete
		$("#headerimg" + currentContainer).fadeOut(function() {
			setTimeout(function() {
				$("#headertxt").css({"display" : "block"});
				animating = false;
			}, 100);
		});
	};
	
	var stopAnimation = function() {
		// Change the background image to "play"
		$("#control").css({ "background-image" : "url(/assets/images/btn_play.png)" });
		
		// Clear the interval
		clearInterval(interval);
	};
	
	// We should statically set the first image
	navigate("next");
	
	// Start playing the animation
	interval = setInterval(function() {
		navigate("next");
	}, slideshowSpeed);
	

// *******************************************************************	
	
	runNews = setInterval('rotateNews()', 5000);

	//grab the width and calculate left value
	item_width = $('#slides li').outerWidth();
	left_value = item_width * (-1);

	//news ticker rotater
	$("#newsTicker li").hide();
	$("#newsTicker li:first-child").show();

	total = $("#newsTicker li").size();


});

	//news Ticker
	function rotateNews() {
        count++;
        if (count > total) {
        	count = 1;
    		$("#newsTicker li:nth-child("+total+")").hide();
	        $("#newsTicker li:nth-child("+count+")").show();
        } else {
        	$("#newsTicker li:nth-child("+(count-1)+")").hide();
       		$("#newsTicker li:nth-child("+(count)+")").show();
        }
	}





