ord=Math.random()*10000000000000000;



/******************************************************************************************************************/

// INPUT AUTO FILL HANDLE

/******************************************************************************************************************/



function handleAutoFillInput(){

	mytext = Array();

	

	$('.autofill').each(function(i){

		mytext[i] = $(this).attr('value');

	});

	

	$('.autofill').click(function(){ 

		

		myindex = $('.autofill').index(this);

		$(this).attr('value','');

		

		$(this).blur(function(){ 

			if (this.value == "") this.value = mytext[myindex];

		});

	});

}



/******************************************************************************************************************/

// CAROUSEL HANDLE

/******************************************************************************************************************/

var visible = Array();		// number of visible elements
var mycar = Array();
var current = Array();
var num = Array();
var w = Array();

function moveTo(i, index){
	mleft = '-'+ (w[i] * index ) +'px';
	mycar[i].find('UL').animate({ marginLeft: mleft }, 500);
	mycar[i].find('.next').css({ cursor: 'pointer', opacity: 1 });
	mycar[i].find('.prev').css({ cursor: 'pointer', opacity: 1 });
	if (index == 0) {
		mycar[i].find('.prev').css({ cursor: 'auto', opacity: 0.2 });
	}
	if (index == (num[i] - visible[i])) {
		mycar[i].find('.next').css({ cursor: 'auto', opacity: 0.2 });
	}
}


function handleCarousel(){

	// for each carousel instance

	$('.last-gallery').each(function(i){

		mycar[i] = $(this);
		num[i] = mycar[i].find('LI').size();
		w[i] = mycar[i].find('LI').width();
		//mycar[i].find('UL').width(w[i] * num[i]);
		visible[i] = Math.round(mycar[i].find('.viewport').width() / w[i]);
		if (num[i] > visible[i]) {
			current[i] = 0;
			mycar[i].find('.prev').css({ cursor: 'auto', opacity: 0.2 });

			// click events 

			mycar[i].find('.next').click(function(){
				if (current[i] < (num[i] - visible[i])) {
					current[i] ++;
					moveTo(i, current[i]);
				}
			});

			mycar[i].find('.prev').click(function(){
				if (current[i] > 0) {
					current[i] --;
					moveTo(i, current[i]);
				}
			});
		}
		else {
			mycar[i].find('.nav').hide();			
		}
	});
}



function handleSlideshow(){

		

	// for each carousel instance

	$('.carousel').each(function(i){

		mycar[i] = $(this);
		num[i] = mycar[i].find('LI').size();
		w[i] = mycar[i].find('LI').width();
		mycar[i].find('UL').width(w[i] * num[i]);
		visible[i] = Math.round(mycar[i].find('.viewport').width() / w[i]);
		if (num[i] > visible[i]) {
			current[i] = 0;
			mycar[i].find('.prev').css({ cursor: 'auto', opacity: 0.2 });

			// click events 

			mycar[i].find('.next').click(function(){
				if (current[i] < (num[i] - visible[i])) {
					current[i] ++;
					moveTo(i, current[i]);
				}
			});

			mycar[i].find('.prev').click(function(){
				if (current[i] > 0) {
					current[i] --;
					moveTo(i, current[i]);
				}
			});
		}
		else {
			mycar[i].find('.nav').hide();			
		}
	});
}



/******************************************************************************************************************/

// SLIDER HANDLE

/******************************************************************************************************************/



function slideTo(index){
	mleft = '-'+ (ws * index ) +'px';
	$('.slider .viewport UL').animate({ marginLeft: mleft }, 500);

	sel = index;

	$('.slider .nav LI').removeClass('sel');
	$('.slider .nav LI:eq('+ sel +')').addClass('sel');
}



function slideLoop(){
	slideTo(curr_slide);
	curr_slide ++;
	if (curr_slide == nums) { curr_slide = 0; }
	SliderTimerId = window.setTimeout('slideLoop('+ curr_slide +')', 10000);
}

function handleSlider(){
	nums = $('.slider .nav LI').size();
	ws = $('.slider .viewport LI').width();
	curr_slide = 0;

	$('.slider .nav').css({
		cursor: 'auto',
		opacity: 0.5
	});

	$('.slider .nav LI:eq(0)').addClass('sel');

	// loop events
	slideLoop(curr_slide);

	// click events
	$('.slider .nav LI').click(function(){
		if (!$(this).hasClass('sel')) {
			curr_slide = $('.slider .nav LI').index(this);
			slideTo(curr_slide);
		}
	});


	$('.slider .autoplay').toggle(function(){

		// pause event

		$(this).removeClass('pause');
		$(this).addClass('play');
		clearTimeout(SliderTimerId);		
	}, function(){

		// play event

		$(this).removeClass('play');
		$(this).addClass('pause');
		SliderTimerId = window.setTimeout('slideLoop('+ curr_slide +')', 10000);

	});
}



/******************************************************************************************************************/

// TABS HANDLE

/******************************************************************************************************************/



function handleTabs(){

	$('.tabs .tab').hide();
	$('.tabs .sel').each(function(){

		initid = $(this).find('A').attr('href');
		$(initid).show();
		initclass = initid.slice(1);
		$(this).parents('UL').addClass(initclass);
	});

	$('.tabs .label A').click(function(){

		$(this).parents('.tabs').find('.label LI').removeClass('sel');
		$(this).parent().addClass('sel');
		
		myid = $(this).attr('href');
		$(this).parents('.tabs').find('.tab').hide();
		$(myid).show();

		myclass = myid.slice(1);
		$(this).parents('UL').removeClass();
		$(this).parents('UL').addClass(myclass);

		return false;

	});
}



/******************************************************************************************************************/

// On document load...

/******************************************************************************************************************/



$(function(){

	// handle autofill

	handleAutoFillInput();

	

	// handle carousel

	handleCarousel();

	

	// handle slider

	handleSlider();

	

	// handle tabs

	handleTabs();

	

	// handle accordion

	$('.accordion').accordion({

		autoheight: true,

		event: 'mouseover',

		header: '.head'

	});

	

	

	// single gallery opacity

	$('.singlegall .caption').css({ opacity: 0.8 });

});


