// JavaScript Document


//common class declaration
function slideshow(){
	
	this.reset_buttons = function(){
		for(i=1; i<=sw.val_by_name("slideshow", "num_of_frames"); i++){
		$("#goto"+i).removeClass("active");
		}
	}
	
	this.onclick = function(frame){
	$("#slideshow1").cycle((frame-1));
	this.reset_buttons();
	$("#goto"+frame).addClass("active");
	}
	
	this.onAfter = function(curr,next,opts){
	var caption = 'Image ' + (opts.currSlide + 1) + ' of ' + opts.slideCount;
	$('#slideshow1_caption').html(caption);
	ss1.reset_buttons();
	$("#goto"+(opts.currSlide + 1)).addClass("active");
	
	
	var att=$('#slide'+(opts.currSlide + 1)).attr('onclick');
		if(att!=null || att!=undefined){
		//alert('contacins click');
			$("#clicker").bind("click", function(){
			$('#slide'+(opts.currSlide + 1)).click();
			});
		$("#clicker").addClass('pointer');
		}
		else{
		$("#clicker").unbind('click');
		$("#clicker").removeClass('pointer');
		}
		
	}
	
	//begin randomzing methods
	this.reorder = function() {
		var obj = $('#slideshow1 img');
		var count = obj.length;
		for(var i=1; i<=count; i++) {
			itA = $('#slideshow1 img:nth-child('+i+')');
			rnd = this.rmax(count);
			if(rnd==i) continue;
			itB = $('#slideshow1 img:nth-child('+rnd+')');
			var itAC = itA.clone(true);
			var itBC = itB.clone(true);
			
			itBC.insertAfter(itA);		
			itA.remove();
	
			itAC.insertAfter(itB);
			itB.remove();
		}
	}
	
	 this.rmax = function(max){
	  var n = Math.round(Math.random()*max);
	  return n == 0 ? 1 : n;
	}
	//end randomzing

}

//declaring the first instance
var ss1 = new slideshow();





//copying onload
var onload_functions_at_slidshow1 = window.onload;
window.onload = function () {
onload_functions_at_slidshow1();
//extending onload

	if(sw.val_by_name("slideshow", "displayOrder")=="random"){
	ss1.reorder();
	}

	$("#slideshow1").cycle({
		fx: "fade",
		duration: 5000,
		after:ss1.onAfter
		
	});
	$("#slidewrap1").removeClass("unv")

}
