// Slideshow

duration = 15; // 2 Seconds
currentIndex = 1;
// slides = Array (0, -890,-1780,-2670, -3560);
slides = Array (0, -890,-1780,-2670);
timeout = 0;
var ss; 
//var size = 5;
var size = 4;

	

function sld (index) {
	
	if (index) {
		//requesting from the web page
		if (index < (size+1)) {
			currentIndex = index;	
			timeout = 2;
		}
	}
	else {
		// looping			
		if (timeout != 0){
				timeout--;
			return;
		}		
	}
	
	// set tabs
	
	J(".tabs a").removeClass('ts');	
	J(".tabs a").addClass('ta');
	J("#tab"+currentIndex).addClass('ts');
	J("#tab"+currentIndex).removeClass('ta');	
	
	// slide
	J("#slidePanel").animate({left:slides[currentIndex-1]},'slow');
	
	// prepare to next step
	currentIndex++;
	if (currentIndex > size){
		currentIndex = 1;	
	}	
	
}

function loopBack () {
	// loop sliding
	sld();
	setTimeout( loopBack,( (duration*1000)));	
}


// start show at plage loading
J(document).ready(function() {
						   
	loopBack ();  
	// bind menu
	J(".menu-active").bind("mouseover", function(){
 	 J(this).addClass('menu-activehover');
	 J(this).removeClass('menu-active');
		});
	J(".menu-active").bind("mouseout", function(){
 	 J(this).addClass('menu-active');
	  J(this).removeClass('menu-activehover');
		});
	
	//load news items if applicable
	if (document.getElementById('rss-news')){
		
			J.ajax({
			  type: "GET",
			  url: "../news/index.php?feed=rss2",	
			  dataType : "xml",
			  success: function(msg){
				// parse xml
				J('#rss-news').html(parseFeed(msg));
			  }
			});
	}
	
	// salesBully
	if (document.getElementById('sbdemo1')){
		sbdAttach();
		
		
	}
	
	
	try {
		setSize ();
	}catch	(e) {}
	
	} );

// parse xmlfeed
var feed;
function parseFeed (feed){

	txt = '';
	items = feed.getElementsByTagName('item');
	for (j = 0; j < items.length; j++){
			txt += 	"<a href=\""+ getText(items[j].getElementsByTagName('link')[0])+"\" >"+getText(items[j].getElementsByTagName('title')[0])+"</a> - ";
			dt = Date.parse(getText(items[j].getElementsByTagName('pubDate')[0]));
			txt +=  dt.toString("dddd, MMMM d, yyyy")+" <br/><br/> ";
			
			// display only 7 post
			if (j > 5)
				break;
	}
	return txt;
}

function getText (node) {
	try{
		if (node.firstChild.nodeValue) 			
			return node.firstChild.nodeValue;
		else
			return  node.firstChild.textContent;
	} catch (e) {};
}

//load contents using ajax
function ld (page, id) {
	// load page in to contentViewer
	J(".atabs a").removeClass('ts');	
	J(".atabs a").addClass('ta');
	J("#"+id).addClass('ts');
	J("#"+id).removeClass('ta');
	
	J.ajax({
	  type: "GET",
	  url: page,
	  success: function(msg){
		J("#contentViewer").html(msg);	  
	 }
	})
	
	// change menu
	mnu = 3;
	if (id == 'tx4')
		mnu = 4;
	
	J.ajax({
	  type: "GET",
	  url: 'mm.php?tab='+mnu,
	  success: function(msg){
		J(".menu").html(msg);	  
	 }
	})
	//change tabs
	
//	J(this).removeClass('ta');
	//
}


function sbdAttach(){
	// attach mouse listerns
	
	J('#sbdemo1').mouseover( function() { J('#sbdemo1').attr('src','../content-images/sb-demo1-over.jpg'); } );
	J('#sbdemo1').mouseout( function() { J('#sbdemo1').attr('src','../content-images/sb-demo1.jpg'); } );
	
	J('#sbdemo2').mouseover( function() { J('#sbdemo2').attr('src','../content-images/sb-demo2-over.jpg'); } );
	J('#sbdemo2').mouseout( function() { J('#sbdemo2').attr('src','../content-images/sb-demo2.jpg'); } );
	
	J('#sbdemo3').mouseover( function() { J('#sbdemo3').attr('src','../content-images/sb-demo3-over.jpg'); } );
	J('#sbdemo3').mouseout( function() { J('#sbdemo3').attr('src','../content-images/sb-demo3.jpg'); } );
	
}

function startTransision () {
	// start slide show
	if (!ss){
		J('#sb_slideshow').cycle({ 
   				 fx:    'fade', 
    			speed:  2500 ,
				timeout:  5000 
				 
			 });
		ss = true;
	}
	return true;
}

// image slide show
var imgArr = Array("../content-images/1.jpg","../content-images/2.jpg","../content-images/3.jpg","../content-images/4.jpg","../content-images/5.jpg","../content-images/6.jpg");
var imgObj = Array();
function initss () {
	J("#shadow").css({width:J(document).width(), height:J(document).height()});	
	
	for (i = 0; i < 6; i++){
		imgObj[i] = new Image;
		imgObj[i].src = imgArr[i];
	}	
}



var nvgt = "<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tr><td width=\"5\">&nbsp;</td><td width=\"100\"><a href=\"javascript:goPrev()\"><img id=\"btn1\" src=\"../images/arrow_left.gif\"  height=\"40\" border=\"0\" /></a></td><td>&nbsp;</td><td width=\"100\" align=\"left\"><a href=\"javascript:goNext()\"><img src=\"../images/arrow_right.gif\" id=\"btn2\"  height=\"40\" border=\"0\" /></a></td><td width=\"5\">&nbsp;</td></tr></table>";
var imgIndex = 0;
function startshow(){
	// start slide show 
	imgIndex = 0;
	J("#shadow").css({display:"block", opacity: 0}).animate({opacity: 1}, 800, function(){
			//2nd step 2. display box
			wd = J(window).width();
			hd = J(window).height();
			J("#ImageBoxOuterContainer").css({left:(wd-740)/2, top:10,display:"block"});
			J("#ImageBoxContainer2").css({left:(wd-740)/2, top:10,display:"block"}).html(nvgt);
						slideAnimate ();															
			});
	
	
}

function closeShow() {
	J("#ImageBoxOuterContainer,#ImageBoxContainer2").css({display:"none"});	
	J("#shadow").animate({opacity: 'toggle'}, 800);
	
}

function slideAnimate () {
	// animate 
	J("#ImageBoxContainer2").css({opacity: 0}).html("<img src=\""+imgObj[imgIndex].src+"\">"+nvgt)
	.animate({opacity: 1},800, '', function() {
	 //swap contents
	J("#ImageBoxContainer1").html("<img src=\""+imgObj[imgIndex].src+"\">"+nvgt);																							 	J("#ImageBoxContainer2").css({opacity: 0});
																								 			
	});		
}

function goPrev() {
	// go to next image
	imgIndex--;
	if (imgIndex < 0)
		imgIndex = 5;
	slideAnimate ();
}

function goNext() {
	// go to next image
	imgIndex++;
	if (imgIndex > 5)
		imgIndex = 0;
	slideAnimate ();
}
