/* 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
Modified Date	: 5-September-2008
Version			: 2.00.000
Modified By		: Bjorn (redone slideshow and random banners and much much more!)

*/
// JavaScript Document


var basepath = './images/';
var bannerpath = './images/';
//var allphones = Array('p1.jpg','p2.jpg','p3.jpg');

var current = 0;
var loading = false;
var slide;
var count = 0;
var slideInterval = 10000;
var numbanners = 4; // show 4 banners
var badded = false;
var currentbanner = 0; // which sliding banner is currently 
var scroll2;
var currenthelp = 'help1';
var bannerwidth = 180;
var bannerheight = 111;

// loads whatever the current phone into page through xhr
function phonie(){
	
	//if (window.banners!=undefined){
	//setBanners();
	//}
	init_slideshow();	
	}
	
function slideNext(){
	clearInterval(slide);
	init_slideshow();
	//slide = setInterval("slideshow()",5000);
	}	
	
	
	
function init_slideshow(){
	if (window.ads==undefined){
	$('phonie').innerHTML = "<div style='margin:4px;border:1px solid #ffcc00;font-weight:normal;background-color:#ffffcc;color:#555555;padding:10px;'>Error fetching advertisements..</div>";
	}
	else{
		
	for (i=0;i<ads.length;i++){
		
					var img = new Asset.image(basepath+ads[i].src,{
												  id: 'image'+i,
												  onload: function(){
												  loaded++;
												  this.inject($('phonie'));
												  var inv = new Fx.Style(this, 'opacity').set(0);
												  this.style.position = 'absolute';
												  this.style.cursor = 'pointer';
											
												  this.height = 168;
												  this.style.top = $('phonie').getTop()+'px';
												  this.style.left = $('phonie').getLeft()+'px';
												  if (loaded==ads.length){
												  slideshow();
												  $('phonie').addEvent('click', function(){
															document.location = ads[current-1].link;							 
													});
											  }
										  }
						  });
			}
	}
}


function slideshow(){
	
	
	
	 if (current>=ads.length){
		 current=0;
		 for (i=0;i<ads.length;i++){
			  var inv = new Fx.Style('image'+i, 'opacity').set(0);
			 }
		 }
 	$('phonie').setProperty('title',ads[current].name);
	
	img = 'image'+current;
	for (i=1;i<=ads.length;i++){
	$("link"+i).removeClass("highlight");
	}
	$("link"+(current+1)).addClass("highlight");
	
	var fx2 = new Fx.Styles(img,{duration:1000,transition: Fx.Transitions.linear} ); 
	fx2.start({
			 'opacity' : [0,1]
			 });
	current++;
	s = setTimeout("slideshow()",slideInterval);
}

function setBanners(){	
 var  showBanners = new Array();
	if (window.banners==undefined){
	$('banners').innerHTML = "<div style='background-color:#ffffff;color:#ff2200;padding:30px;'>Error fetching banners..</div>";
	}
	else {
		for (i=0;i<numbanners;i++){ // hmm why are we doing it like this??
		m = banners.length; 
		num = Math.ceil(Math.ceil(Math.random()*10)%m);
		showBanners.push(banners.splice(num,1)[0]);
		}
		hideBanners = banners;
		for(i=0;i<showBanners.length;i++){ 
		$('banner_scroll').innerHTML += "<a width='180'  height='111' href="+showBanners[i].link+" ><img src=\""+bannerpath+showBanners[i].src+"\" border=0 bid='ban"+i+"' /></a>";
		}
	}
}

function bannerNext(){
	if (!badded){
	addOtherBanners();
		}
	currentbanner += 4;
	scrollBanner(scroll2);	
	}

function addOtherBanners(){
	loadimg = 0;
	$('banner_scroll').style.width = $('banner_scroll').getSize().size.x;
	$('banner_scroll').style.width = $('banner_scroll').getSize().size.x+((bannerwidth+20)*Math.ceil(hideBanners.length/4)*4)+'px';
	
	for (i=0;i<hideBanners.length;i++){
			var img = new Asset.image(bannerpath+hideBanners[i].src,{
						id : 'ban'+parseInt(numbanners+i),
						onload: function(){
						//$('banner_scroll').style.width = $('banner_scroll').getSize().size.x+this.width+20+'px';
						
						this.height = bannerheight;
						this.width = bannerwidth;
						this.style.cursor = 'pointer';
					 	
						this.inject($('banner_scroll'));
						this.addEvent('click',function(e){
													  
							document.location = hideBanners[this.id.substr(3)-numbanners].link;
							   });
				
						loadimg++;
				
						if (loadimg==hideBanners.length){
							
							scroll2 = new Fx.Scroll('banners', {
								wait: false,
								duration: 1000,
								offset: { x: -1},
								transition: Fx.Transitions.Quad.easeOut						
								});
							scrollBanner(scroll2);	
							badded = true;
				}
			}
		});	
	}
}
	
function scrollBanner(s){ 
	if ($('ban'+currentbanner)!=null){
	//	s.toElement('ban'+currentbanner);
	s.scrollTo(770*(currentbanner/4),0);
	}
	}
	

function showLogin(obj){ // login at the page top
	if ($('loginblue').style.display =="none"){
	$('loginblue').style.display ="block";
	p = findPos(obj);
	$('loginblue').style.left = (p[0]-85)+"px";
	$('loginblue').style.top = (p[1]+15)+"px";
	}
	else{
		$('loginblue').style.display ="none";
		}
	}
	
	
function showLogin2(){ // login at my account girl
	var scroll = new Fx.Scroll('logingirl', {
	wait: false,
	duration: 500,
	offset: { y: 100}
	});
	scroll.toElement('loginform');
}


function viewVideo(obj){ // loads flash flv player in slideshow tab

	var d = new Date();
	url = "getflash.html?id="+d.getTime();	
	jx = new Ajax(url, {				
				method: 'get',				
				onRequest: function(){						
				$('phonie').innerHTML = "<div style='margin:4px;border:1px solid #ffcc00;font-weight:normal;background-color:#ffffcc;color:#555555;padding:10px;'>Loading commercials..</div>";
				},
				onComplete: function(){	
				$('phonie').innerHTML = jx.response.text;
				obj.addClass("highlight");
				}		
				});
	jx.request();
}

function initFSelect(){ // help dropdowns
	$$("#fselect2").addEvent('click', function(){
				$(currenthelp).setStyles({
						display : 'block',
						left : findPos(this)[0]+'px',
						top : (findPos(this)[1]+this.getSize().size.y)+'px',
						width : (this.getSize().size.x-2)+'px' // minus 2 for border
						});
				});
	
	
	$$("#fselect1").addEvent('click', function(){
				$('helpmain').setStyles({
						display : 'block',
						left : findPos(this)[0]+'px',
						top : (findPos(this)[1]+this.getSize().size.y)+'px',
						width : (this.getSize().size.x-2)+'px' // minus 2 for border
						});
				});
	
	$$("#helpmain a").addEvent('click',function(){
					$('htext1').setText(this.getText());
					
					this.parentNode.style.display = 'none';
					currenthelp = 'help'+this.id.split("_")[1];
					//alert($(currenthelp).getFirst().getText());
					$('htext2').setText($(currenthelp).getFirst().getText());
				});
	
	$$(".helpmenu").addEvent('mouseleave',function(){
				//alert('ss');
				this.style.display = 'none';				   
				});

	}
	
function gotoSlide(num){ // move the slideshow to clicked number 
	clearTimeout(s);
	for (i=0;i<ads.length;i++){
			  var inv = new Fx.Style('image'+i, 'opacity').set(0);
			 }
	current = num;
	slideshow();
	}
