/* Copyright (c) 2007 Affno (Pvt) Ltd, . All rights reserved.

 This software is the confidential and proprietary information of        
 Affno  ("Confidential Information").  You shall not disclose such 
 Confidential Information and shall use it only in accordance with
 the terms of the license agreement you entered into with Affno.

Module Name		: MOBWEB1
File Name		: phonie.js
Description		: Mobitel phone slideshow
Created By		: Bjorn de Lima
Created Date	: 22-August-2007
Modified By		: Bjorn (added banners)
Modified Date	: 17-April-2008
Version			: 1.00.000
*/
// JavaScript Document


var basepath = '../images/phonie/';
var bannerpath = './images/home_banners/';
//var allphones = Array('p1.jpg','p2.jpg','p3.jpg');

var current = 0;
var loading = false;
var slide;
var count = 0;
var d = new Date();
ran = d.getTime();

// loads whatever the current phone into page through xhr
function phonie(){
	if (window.banners!=undefined){
	setBanners();
	}
	
	slideshow();
	slide = setInterval("slideshow()",5000);
	}
	
function slideNext(){
	clearInterval(slide);
	slideshow();
	slide = setInterval("slideshow()",5000);
	}	
	
function setBanners(){
	if (window.banners==undefined){
		$('banners').innerHTML = "<div style='background-color:#ffffff;color:#ff2200;padding:30px;'>Error fetching banners..</div>";
	}
	else{
		len = banners.length;
		l = len-2; //change this to 4 when there are 4 banners
		for (i=0;i<l;i++){ // hmm why are we doing it like this??
		m = banners.length; 
		num = Math.ceil(Math.ceil(Math.random()*10)%m);
		banners.splice(num,1);
		}
		for(i=0;i<2;i++){ //change this to 4 when there are 4 banners
		obj = $('b'+parseInt(i+1));
		obj.title = banners[i].name;
		//obj.link = banners[i].link;
		obj.innerHTML = "<a href="+banners[i].link+" ><img src=\""+bannerpath+banners[i].src+"\" border=0 /></a>";
		}
		}
	}	

function slideshow(){
	if (window.ads==undefined){
	$('phonie').innerHTML = "<div style='background-color:#ffffff;color:#ff2200;padding:30px;'>Error fetching advertisements..</div>";
		}
	else{
	alladds = ads;			
	}
	
	if (window.newbasepath!=undefined){
		basepath = newbasepath;
		}
	if (!loading ){
	
		// can we load the next image or what
	$('phonie').innerHTML = "<div style='background-color:#ffffff;color:#ff2200;padding:3px;'>Loading..</div>";
	loading = true;
	
	var img = new Asset.image(basepath+alladds[current].src+"#"+ran, {
											   id:'phone',
											   onload: function(){
												   count++;
												   $('phonie').title = alladds[current].name;
													$('phonie').link = alladds[current].link;
													$('phonie').style.cursor = "pointer";
													$('phonie').addEvent('click',function(){
													document.location = $('phonie').link;
													});
													$('phonie').innerHTML = "";
												   this.inject('phonie');
												   this.setStyle('opacity','0');
												   var fx = new Fx.Style(this,'opacity',{ duration:300 });
												   fx.start(0,1);
												   current++;
												   if (current==alladds.length){
													   current = 0;
													 }
													loading = false;   
												}
												
											   });
	}
	
if (count>100){ // we have 
	clearInterval(slide);
	}	

}
