//////////////////////////////////////////////////////
// Fade In/Out SlideShow with Auto Play function    //
// and a news scroller								//
// Developed by Eng. Mohammed Yehia Abdul Mottalib  //
// http://www.dahabtech.com							//
// Mobile: (+20) 10 3930 361 OR (+20) 14 5325 822	//
// Landline: (+20) 69 3642 312						//
// Address: Masbat - Salah El Din Centre			//
// *************************************************//
// Developed for free use			                //
// Feel free to use this form anywhere in your		//
// in your website									//
// Date: 4th June 2010                              //
// Copyrights 2010 | Dahab TEchnology				//
//////////////////////////////////////////////////////


// Creating a new Object and sets its initials and properties

var fSS = {

	// Initial Property : contains all the settings that affects the animation
	init: function() {
		
		// =========== Duration and Frame Rate settings ============= //

		fSS.frameRate 		= 75;  // <- Frame Rate per seconds
		fSS.duration 		= 0.5; // <- Duration in seconds i prefer 1 seond to make it smooth 
		fSS.news		    = Core.getElementsByClass("pscrollernews");				// <- Get all the news headers with the class pscrollernews
		fSS.nTimeRate		= 6000;		// <- Time to swap slides default is 5000 = 5 seconds. You may change it to what suits you.
		
		// =========== News Scroller settings ============ //
		
		for(var i = 0, ii = fSS.news.length; i<ii; i++) {
			fSS.news[i].nDefault		= 28;
			fSS.news[i].nCurrent		= 3;
			fSS.news[i].nEnd			= -28;
		}
		    
		fSS.nincrementTop 	= (fSS.news[0].nDefault - fSS.news[0].nCurrent) / (fSS.frameRate * fSS.duration); 
		fSS.nFirstHeader 	= 0;
		fSS._lastViewed		= fSS.nFirstHeader;
		
		setInterval("fSS.newsPlay()",fSS.nTimeRate);
		fSS.newsScrollerHandler(fSS.nFirstHeader);
	},
	
	newsScrollerHandler: function(div) {
		
		for(var i = 0, ii = fSS.news.length; i<ii; i++) {
			if(div == i) {
				fSS.newsScrollUp(div);
			} else if(fSS._lastViewed == i) {
				fSS.newsScrollDown(fSS._lastViewed);
			} else {
				fSS.newsHide(i);
			}
		}
	},
	
	newsScrollUp: function(i) {
		
		fSS.news[i].nDefault -= fSS.nincrementTop;
		
		if(fSS.news[i].nDefault <= fSS.news[i].nCurrent) {
			
			fSS.news[i].nDefault = fSS.news[i].nCurrent;
		
		} else {
			
			setTimeout(function() { fSS.newsScrollUp(i); },500 / fSS.frameRate);
		}
		
		fSS.news[i].style.top = fSS.news[i].nDefault + "px";
		
		if(fSS.news[i].nDefault <= fSS.news[i].nCurrent) {
			
			fSS.news[i].nDefault = 28;
		
		}
		
	},
	
	newsScrollDown: function(i) {
		
		fSS.news[i].nCurrent -= fSS.nincrementTop;
		
		if(fSS.news[i].nCurrent <= fSS.news[i].nEnd) {
			
			fSS.news[i].nCurrent = fSS.news[i].nEnd;
		
		} else {
			
			setTimeout(function() { fSS.newsScrollDown(i); },500 / fSS.frameRate);
		}
		
		fSS.news[i].style.top = fSS.news[i].nCurrent + "px";
		
		if(fSS.news[i].nCurrent <= fSS.news[i].nEnd) {
			
			fSS.news[i].nCurrent = 3;
		
		}
		
	},
	
	newsHide: function(i) {
		
		fSS.news[i].style.top = fSS.news[i].nDefault + "px";
		
	},

	newsPlay : function() {
		
		fSS.nFirstHeader++;
		if(fSS.nFirstHeader >= fSS.news.length) {
			fSS.nFirstHeader = 0;
		}
		fSS.newsScrollerHandler(fSS.nFirstHeader);
		fSS._lastViewed = fSS.nFirstHeader;
	}
};

// Starts the fSS.init 
Core.start(fSS);
