(function(){
	$.fn.toggleValue = function(attr){
		return this.each(function(){
			var obj = $(this),
				v = obj.attr(attr);
			obj.focus(function(){
				$(this).val(($(this).val() == v) ? '' : $(this).val());
			});
			obj.blur(function(){
				$(this).val(($(this).val() == '') ? v : $(this).val());
			});
		});
	}
})(jQuery);

$(function(){
	
	// Stage Carousel
	
	if($('.stage')[0]){
		$('.stage div+div > ul').simpleCarousel({
			btnPrev: '.navigation .prev',
			btnNext: '.navigation .next',
			itemWidth: 990,
			itemsHTML: '<a href="#"><span></span></a>',
			visible: 1,
			circle: true,
			auto: true,
			autoTime: 4000,
			navigation: '.navigation p',
			speed: 1000,
            itemsCount: 3,
			afterFinish: function(active,obj){
				//console.log(active);
				$('.stage .controls h2').text($('img', obj).attr('alt'));
			}
		});
	}

// END Stage Carousel

// Phone Carousel


    


// END Phones Carousel
	
	// Curresncies tabs
	
	if ($('.currencies').length > 0) {
		$('.currencies dd > ul > li a').click(function(){
			$('.currencies dd > ul > li.active').removeClass('active');
			$(this).parent().addClass('active');
			if ($.browser.webkit) {
				$(this).trigger('focus');
			}
			return false;
		});
		
		
		$('.currencies dd > ul > li a').focus(function(){
			$('body').keydown(function(event){
				if (event.which == '39') {
					if ($('.currencies dd > ul > li.active').next()[0]) {
						$('.currencies dd > ul > li.active').removeClass('active').next().addClass('active');
					}
				}
				else 
					if (event.which == '37') {
						if ($('.currencies dd > ul > li.active').prev()[0]) {
							$('.currencies dd > ul > li.active').removeClass('active').prev().addClass('active');
						}
					}
			});
		});
		
		$('.currencies dd > ul > li a').blur(function(){
			$('body').unbind('keydown');
		});
		
	}
	
	// END Curresncies tabs
	
	// Menu Animation
	
	$('#header > ul li a').mouseenter(function(){
		var self = this;
		$('span', this).stop(true).animate({'width': '0px', 'height':'3px'}, 100, function(){
			$(this).animate({'width':$(self).width()+'px', 'height':'3px'}, 400);
		});
	});
	
	$('#header > ul li a').mouseleave(function(){
		$('span', this).stop(true).animate({'width':'18px', 'height':'3px'}, 400);
	});
	
	// END Menu Animation
	
	//Show Search
	
	$('#header .search input:text').toggleValue('title');
	
	$('.search a').click(function(){
		$(this).next('form').toggle();
	});
	
	$('.search a').toggle(
		function(){
			$(this).click(function(){
				$(this).next('form')
			});
		},
		function(){
			$(this).click(function(){
				$(this).next('form').removeClass('show');
			});
		});
		
	//END Show Search
	
	//FAQ
	if ($('.faq').length > 0) {
		$('.faq h4').click(function(){
			$(this).next('.answer').toggle();
		});
	}
	//END FAQ
	
	//Tag cloud
	var length = $('#tags li').length -1;
	var items = [];
	
	function randOrd(){
	return (Math.round(Math.random())-0.5); } 
	
	$('#tags li').each(function(i,ob){
		items.push($(ob).html());
	});
	
	items.sort(randOrd);
	
	$('#tags ul').html('');
	
	for (var i = 0; i < items.length ; i++){
		$('#tags ul').append('<li>'+items[i]+'</li>');
	}
	
	var max = 21;
	
	$('#tags li').each(function(i,ob){
		var font = (Math.floor(Math.random()*max)+20)/2;
		
		if (font > 17) {
			max = 15;
		}
		
		$(ob).css({'font-size':font+'px','opacity': (font-8)/10});
		
		if (i == 0) {
			$(ob).css({'text-align':'center'});
		} else {
			
			if (i == length) {
				$(ob).css({'text-align': 'center'});
			} else {
				
				if (i % 2 == 0) {
					$(ob).css({'text-align': 'right'});
					if (i < length/2){
						$(ob).css({'padding-right': ((length*5)/i)+'px'});
					}else {
						$(ob).css({'padding-right': ((length*5)/(length-i))+'px'});
					}
				}else {
					$(ob).css({'text-align': 'left'});
					if (i < length/2){
						$(ob).css({'padding-left': ((length*5)/i)+'px'});
					}else {
						$(ob).css({'padding-left': ((length*5)/(length-i))+'px'});
					}
				}
				
			}
		}
		
	});
	/*
	$('#tags').tagcloud({
			centrex: 80,
			centrey: 70,
			min_font_size: 10,
			max_font_size: 20,
			zoom: 150
		});*/
	//END Tag cloud
});

