delay = delay * 1000;
var timerOn = true;
var slides,
slideShow,
blockyButtonsWrap,
blockyButtons,
blockyMain;
var slideNum = 0;
var currentDiv = 0;	
var bgColorOn = "#254897";
var bgColorOff = "#55a2ec";
var playButtonPath = "/ue/home/blockyPlay_btn.gif";
var pauseButtonPath = "/ue/home/blockyPause_btn.gif";

if(!isSears){
	bgColorOn = "#7d0707";
	bgColorOff = "#d2021c";
	playButtonPath = "/ue/home/km_blockyPlay_btn.gif";
	pauseButtonPath = "/ue/home/km_blockyPause_btn.gif";
	borderColor = "#EBEBEB";
}

$(function(){
	
	//get the number of slides that are going to play
	slideShow =  $("#blockySlideShow");
	blockyMain = slideShow.parent();
	slides = slideShow.find(".blockySlide");
	slideNum = slides.length;
	//default timer
	defaultTimer = setTimeout('changeSlider(currentDiv,slideNum)', delay);
	
	
			
	//create buttons
	var buttonContent = "<div id='blockySlideBtns'>\n\r";
	for(var j=0; j < slideNum; j++){
		buttonContent += "<div class='blockNumberButtons' id='" +j+ "'>" +(j+1)+ "</div>\n";
	}
	buttonContent += "<div class='blockNumberButtons' id='play'><img src='" + pauseButtonPath + "' /></div>\n\r</div>\n\r";	
	blockyMain.append(buttonContent);
	
	//Set up initial look
	slides.eq(0).css("display","block");
	blockyButtonsWrap = slideShow.next();
	blockyButtons = blockyButtonsWrap.find('div');
	blockyButtons.css("background-color", bgColorOff);
	blockyButtonsWrap.find('#play').css("background-color","white");
	blockyButtons.eq(currentDiv).css("background", bgColorOn);
	if(isBorderOn){
		blockyMain.css("border","1px solid "+borderColor);	
		}		
	blockyMain.css("height", sliderHeight).css("background","none");
	//button functionality
	blockyButtons.click(blockyButtonClick).hover(blockyButtonHoverIn, blockyButtonHoverOut);
	
});

//timed change
function changeSlider(changeThisDiv,slideNum){ 
	var thisBlockySlides = $("#blockySlideShow .blockySlide");
	var thisBlockyButtons = $("#blockySlideBtns .blockNumberButtons");
	
	thisBlockySlides.eq(changeThisDiv).fadeOut(1000);
	thisBlockyButtons.eq(changeThisDiv).css("background", bgColorOff);
	if(changeThisDiv >= slideNum-1){
		currentDiv = 0;
	} else {
		currentDiv++;
	}
	thisBlockySlides.eq(currentDiv).fadeIn(1000);
	thisBlockyButtons.eq(currentDiv).css("background", bgColorOn);
	defaultTimer = setTimeout('changeSlider(currentDiv,slideNum)', delay);	
}

//click change
function clickChangeSlider(toThisDiv){ 
	var thisBlockySlides = $("#blockySlideShow .blockySlide");
	var thisBlockyButtons = $("#blockySlideBtns .blockNumberButtons");
	thisBlockySlides.eq(currentDiv).fadeOut(1000);//.css("display", "none");
	thisBlockyButtons.eq(currentDiv).css("background", bgColorOff);
	thisBlockySlides.eq(toThisDiv).fadeIn(1000);//.css("display", "block");
	thisBlockyButtons.eq(toThisDiv).css("background", bgColorOn);
	clearTimeout(defaultTimer);
	defaultTimer = 0;
	timerOn = false;
	currentDiv = toThisDiv;	
	$("#play img").attr("src",playButtonPath);
}

//playbutton
function timerRevolt(){
	if(timerOn){
		clearTimeout(defaultTimer);
		defaultTimer = 0;
		timerOn = false;
		$("#play img").attr("src",playButtonPath);
	} else {
		timerOn = true;
		changeSlider(currentDiv,slideNum);
		$("#play img").attr("src", pauseButtonPath);
	}
}

function blockyButtonClick(){
	var button = $(this);
	var thisID = button.attr("id");
	if(thisID=="play"){
		timerRevolt();
	} else if(thisID == currentDiv){

	} else {
		thisID;
		clickChangeSlider(thisID);
		button.css("background",bgColorOn);
	}
}

function blockyButtonHoverIn(){
	var button = $(this);
	var thisID = button.attr("id"); 
	if(thisID != "play"){
		button.css("background",bgColorOn);
	}
}
function blockyButtonHoverOut(){
	var button = $(this);
	var thisID = button.attr("id"); 
	if(thisID != "play" && thisID != currentDiv){
		button.css("background",bgColorOff);
	}
}
