var bestsellers = (function() {
					
var api=[]; // make object to attach methods to


var cssLeft = 558;
var counter = 1;
var urlCounter = 0
var basket_clickarea;
var intervalTimer;
var ad_slide;
var userSelectedInView = false;
var chosenBestSeller;
var antiBounce = false;
var antiBounceTimer;

var ads = [
		   {code: "BAD008", imagepath:"images/best_sellers/BAD008.jpg", name: "Carlton maxi iso 4.3", size: "-", quantity: 1, UnitPrice: "6.95", message: false, selected: false},
		   {code: "BAD010", imagepath:"images/best_sellers/BAD010.jpg", name: "Carlton Club C100 shuttles", size: "-", quantity: 1, UnitPrice: "6.95", message: false, selected: false},
		   {code: "BBL162", imagepath:"images/best_sellers/BBL162.jpg", name: "Baden All Stars Basketballs", size: "Please state size", quantity: 1, UnitPrice: "5.95", message: true, selected: false},
		   {code: "TMB130", imagepath:"images/best_sellers/TMB130.jpg", name: "Mesh Bibs", size: "Please state size/colour", quantity: 1, UnitPrice: "2.95", message: true,  selected: false},
		   {code: "FBL392", imagepath:"images/best_sellers/FBL392.jpg", name: "Mitre Midas Training Football", size: "Please state size", quantity: 1, UnitPrice: "4.75", message: true, selected: false},
		   {code: "FBL101", imagepath:"images/best_sellers/FBL101.jpg", name: "Mitre Ball Sack", size: "Please state colour", quantity: 1, UnitPrice: "8.75", message: true,  selected: false},
		   {code: "HOC001", imagepath:"images/best_sellers/HOC001.jpg", name: "Mercian Hockey Stick", size: "Please state size", quantity: 1, UnitPrice: "6.95", message: true,  selected: false},
		   {code: "HOC421", imagepath:"images/best_sellers/HOC421.jpg", name: "Slazenger Smooth Hockey Balls", size: "-", quantity: 1, UnitPrice: "18.50", message: false, selected: false},
		   {code: "HOC422", imagepath:"images/best_sellers/HOC422.jpg", name: "Slazenger Dimple Hockey Balls", size: "-", quantity: 1, UnitPrice: "18.95", message: false, selected: false},
		   {code: "NBL004", imagepath:"images/best_sellers/NBL004.jpg", name: "Mitre Intercept Netball ", size: "Please state size", quantity: 1, UnitPrice: "5.95",  message: true, selected: false},
		   {code: "RUG095", imagepath:"images/best_sellers/RUG095.jpg", name: "Mitre Grid Rugby Ball ", size: "Please state size", quantity: 1, UnitPrice: "4.50",  message: true, selected: false},
		   {code: "TTE003", imagepath:"images/best_sellers/TTE003.jpg", name: "Club Table Tennis Bat", size: "-", quantity: 1, UnitPrice: "3.95",  message: false, selected: false},
		   {code: "TTE009", imagepath:"images/best_sellers/TTE009.jpg", name: "Table Tennis Balls", size: "-", quantity: 1, UnitPrice: "14.50",  message: false, selected: false}
		  


		   
		   ];

api.kick = function(){
var userSelectedInView = false;	
bestsellers.displayItemCount();
// bindings	
var best_sellers_wrapper = document.getElementById("best_sellers_wrapper");
var best_sellers_background = document.getElementById("best_sellers_background");


$('#best_sellers_wrapper').mouseover(function() {
 api.killTimer();
});


$('#best_sellers_wrapper').mouseout(function() {
 //write_log("userSelectedInView "+userSelectedInView);
if(userSelectedInView) return;
//write_log("Resume slides");
startTimer();
});


$("best_sellers_list").click(function(event){
  event.stopPropagation();
});

$('#best_sellers_list').bind('change', function() {
if(this.value != "Choose") showChosenBestseller(this.value);
});

make_showall_popup(); // make product pick menu

$('#choose_click_area').bind('click',function(e) {
$("#best_sellers_list_wrapper").slideDown('fast');
//userSelectedInView = true;
//write_log("userSelectedInView");
e.preventDefault();
});

$('#view_click_area').bind('click', function() {
quickorder.setPriceBoxAlignment("right");
var h = $("#outer_content_wrapper").innerHeight(); // grab height of current contents
$("#greyoutOverContentDiv").css({'filter' : 'alpha(opacity=75)', 'height' : h}); 
$("#greyoutOverContentDiv").fadeIn(400);
$("#quick_order_formDiv").slideDown(300);
});

// send item to basket
$('#basket_clickarea').bind('click', function() {
if(antiBounce) return;											 
if(chosenBestSeller){
quickorder.addBestSeller(ads[chosenBestSeller]);
chosenBestSeller = null;
userSelectedInView = false;
//hide_showall_popup();
}else{
quickorder.addBestSeller(ads[urlCounter]); 
}
api.displayItemCount();
antiBounce = true;
antiBounceTimer = setInterval(function(){antiBounce = false}, 5000); 
});


placeAd(); // place first ad
startTimer();						   
};

// this is also called from within quick_order_2011.js when a line/item is added or deleted from Quick Order form
api.displayItemCount = function(){
try{
document.getElementById("num_basket_items").innerHTML = quickorder.getItemCount();	
}catch(e){};
}


/*
function make_showall_popup(){
var best_sellers_list = document.getElementById("best_sellers_list");
best_sellers_list.options.length = 1;
for (var i = 0; i < ads.length; i++) {
best_sellers_list.options[best_sellers_list.options.length] = new Option(ads[i].name, i, false);	 // carry array pointer as value
}
}
*/

function make_showall_popup(){
$('#best_sellers_list').mouseleave(function() {
$("#best_sellers_list_wrapper").slideUp('fast'); // abort
userSelectedInView = false; 
//write_log("userSelectedInView "+userSelectedInView);
});
var best_sellers_list = document.getElementById("best_sellers_list");
var ul = document.createElement("ul");	
for (var i = 0; i < ads.length; i++) {
var li = document.createElement("li");
var a = document.createElement("a");// create anchor
a.key = i;
a.onclick = function(){ showChosenBestseller(this.key); return false;};
a.href = "#";
var text = document.createTextNode(ads[i].name);
a.appendChild(text);
li.appendChild(a);
ul.appendChild(li);
}
best_sellers_list.appendChild(ul);
}



function placeAd(){
ad_slide = document.getElementById("ad_slide");
ad_slide.appendChild(getImage(0)); // place first images in slider
ad_slide.appendChild(getImage(1)); // append second image so we are ready to slide
//$("#ad_slide").css("left" , "0");

}

function getImage(num){
var img = document.createElement("img");
img.style.cssText ="float: left; width: 588px; height: 190px;";
img.src = ads[num].imagepath;
return img;	
}

function startTimer(){
intervalTimer = setInterval(rotate, 6000); 	
}

api.killTimer = function(){
//write_log("Freeze slides");
clearInterval(intervalTimer);	
}

function rotate(){
	
	counter = (counter < ads.length-1)? counter +1 : 0;
	
	urlCounter = (urlCounter < ads.length-1)? urlCounter +1 : 0;
	
	$(ad_slide).animate({"left": -588}, "slow", "swing", readyNextAnimation);
}


function readyNextAnimation(){
	if(userSelectedInView) return; // dont slide if we are showing a user selected ad
	
	if(ad_slide.firstChild) {
    ad_slide.removeChild(ad_slide.firstChild);
    }
	$(ad_slide).css("left" , "0");
	ad_slide.appendChild(getImage(counter)); // append next ad
	
}

function displayMessage(message){
document.getElementById("logDiv").innerHTML = message;	
}


// slide in chosen ad into ad_display_area
function showChosenBestseller(which){
clearInterval(antiBounceTimer);
antiBounce = false;
api.killTimer();
chosenBestSeller = which;
var how_many = ad_slide.childNodes.length;

if(how_many == 2) ad_slide.removeChild(ad_slide.lastChild); // remove the existing one

ad_slide.appendChild(getImage(chosenBestSeller)); // replace it with chosen one

$(ad_slide).animate({"left": -588}, "slow", "swing"); // slide it in



$("#best_sellers_list_wrapper").slideUp('fast');
userSelectedInView = false;
//counter = which;
	
}


 return api;
	  
}());


/* offersAds function 
   to make ads crossfade over existing Flash based ad

*/

var offersAds = (function() {
var api=[]; // make object to attach methods to
var offersAdsDiv1, offersAdsDiv2;
var counter = 0;
var intervalTimer;
var offers_wrapper, extra_info_area;
var extrasWindowIsOpen = false;

var ads = [ 
		   
		    {url: "images/pick_your_kit.png", hotspots: [165, 281, 399, 517], php:["football_strip.php","rugby_strip.php","netball_strip.php", "hockey_strip.php"], html: [] },
			{url: "images/slazenger_hockey_ad.png", hotspots: false, php: false, html: [] }, 
			{url: "images/transparent_ad.gif", hotspots: false, php:false, html: []}
		   
		  ];

var divs = [];

api.kick = function(){ 
setTimeout(api.startTimer, 5000);
offers_wrapper = document.getElementById("offers_wrapper");
extra_info_area = document.getElementById("extra_info_area");
offers_wrapper.onmouseover = function(){api.killTimer();};
offers_wrapper.onmouseout = function(){if(!extrasWindowIsOpen) api.startTimer();};
divs = []; // reset
// append ads to their respective divs
for (var i = 0; i < ads.length; i++) {
var div = document.createElement("div");
div.style.cssText = "position: absolute; top: 0px; left: 0px;"
divs.push(div);
div.style.zIndex = "2";
var img = document.createElement("img");
img.src = ads[i].url;
div.appendChild(img);

// make image maps for 'pick your kit' ad only - array item 1
if(ads[i].hotspots != false){
for (var k = 0; k < ads[i].hotspots.length; k++) {
var hotspot = add_hotspot(ads[i].hotspots[k],i, k);
div.appendChild(hotspot);
}
}
$(div).hide(); // hide all divs initially. They will be revealed later one at a time by fadePrevious()
offers_wrapper.appendChild(div);
}
}



api.startTimer = function (){
clearInterval(intervalTimer);
intervalTimer = setInterval(rotate, 10000); 		
}

api.killTimer = function(){
clearInterval(intervalTimer);	
}

api.resume = function(){
extrasWindowIsOpen = false;
}
						 
function rotate(){
try{
$(divs[counter]).css("zIndex","2");	 //bring to front
$(divs[counter]).fadeIn(1000, fadePrevious);



}catch(e){
write_log("Oops - error "+e);	
}
	
}

function fadePrevious(){
var previous = (counter == 0)? ads.length-1 : counter-1;
$(divs[previous]).fadeOut();
counter = (counter < ads.length-1)? counter +1 : 0;
}


function add_hotspot(coords, adNumber, key){
var link = document.createElement("a");
link.href = "#";
link.onclick = function(){offersAds.show_more_info("+key+"); return false;};
var img = document.createElement("img");
img.onmouseover = function(){if(!extrasWindowIsOpen) load_info(key, adNumber);}; // preload info on hotspot mouseover
img.src = "images/info_but_small.png";
img.style.cssText = "z-index: 3; position: absolute; left: 145px; top: "+coords+"px";
link.appendChild(img);
return link;
}


function load_info(which, adNumber){
$("#extra_info_area").html(""); // delete any old stuff

if(ads[adNumber].html[which]){

$("#extra_info_area").html(ads[adNumber].html[which]); // load from property

}else{
	var url = "php/offers_extra_info/"+ads[adNumber].php[which];
	
	$.ajax({
	url: url,
	error: showErrorMessage,
	cache: false,
	success: function(data) {
	
	$("#extra_info_area").html(data);
	ads[adNumber].html[which] = data; // keep data for future display
	}
	});
	}
}

function showErrorMessage(){
$("#extra_info_area").html("Sorry, an error occured fetching this data");	
}


api.show_more_info = function(){
 $("#extra_info_area_wrapper").fadeIn('fast');
extrasWindowIsOpen = true;
}

return api;
	  
}());





	/*
	function write_log(text){
	var logDiv = top.document.getElementById('logDiv');
	var mytextnode = top.document.createTextNode(text);
	logDiv.appendChild(mytextnode);
	var mybr = top.document.createElement("br");
	logDiv.appendChild(mybr);
	};

	*/
	function write_log(text){
		return;
	var logDiv = top.document.getElementById('logDiv');
	logDiv.innerHTML = text;
	};	
