jQuery(document).ready(function() {


	//more link event/animation
	jQuery('a#more-link').unbind('click').bind('click', function(event) {
		//event.preventDefault();
		
		if (jQuery('#expander').is(':hidden') == true){
			jQuery('a#more-link span').html('Less...');
		} else {
			jQuery('a#more-link span').html('More...');
		}
		jQuery('#expander').slideToggle('fast');
		return false;
		
	});


	if (jQuery.browser.msie && jQuery.browser.version.substr(0,1) < 7) {
		//return;
	} else {
 
		//navigation animation effects
		jQuery('div.menu li:not(.current_page_item, .current_page_parent) a')
			.css( {backgroundPosition: "-10px -250px"} )
			.mouseout(function(){
				jQuery(this).stop().animate(
					{backgroundPosition:"(-10px -250px)"}, 
					{queue:false, duration:250}
				)
			})	
			.blur(function(){
				jQuery(this).stop().animate(
					{backgroundPosition:"(-10px -250px)"}, 
					{queue:false, duration:250}
				)
			})
			.mouseover(function(){
				jQuery(this).stop().animate(
					{backgroundPosition:"(-10px -43px)"}, 
					{queue: false,duration:500}
				)
			})
			.focus(function(){
				jQuery(this).stop().animate(
					{backgroundPosition:"(-10px -43px)"}, 
					{queue: false,duration:500}
				)
			});
	}
		
	//color switching
	jQuery("#color").css('visibility','visible');
	jQuery("#red").click(function(e){
		color_select('red');
		return false;
	})
	jQuery("#white").click(function(e){
		color_select('white');
		return false;
	})
	
	function color_select(color){
		
		if (color=='red'){
			jQuery("#red").addClass('active');
			jQuery("#white").removeClass('active');
			jQuery.cookie('color', 'red', {path: '/', expires: 365});
			
			jQuery('link[@rel*=style][title]').each(function(i){
											    
				if (this.getAttribute('title') == "color") {
					this.href = 'http://vinagency.com/wp-content/themes/vin/red.css';
				}	
				
			});

			
		} else {
			jQuery("#white").addClass('active');
			jQuery("#red").removeClass('active');
			jQuery.cookie('color', 'white', {path: '/', expires: 365});
			
			jQuery('link[@rel*=style][title]').each(function(i){
											    
				if (this.getAttribute('title') == "color") {
					this.href = 'http://vinagency.com/wp-content/themes/vin/white.css';
				}	
				
			});
		}
		try{
			
			setTimeout( function(){ Cufon.replace('h1');	Cufon.replace('h2'); }, 50 );
			
		} catch(e){}
	}
	


	//homepage banner slideshow
	
		
	//try{console.log('!');} catch(e){}
	
	var current_index = 0;
	var outer_wrapper_id = 'banner';
	var outer_wrapper_class = 'banner-container';
	var controls_id = 'slideshow-controls';
	var slideshow_delay = 5;
	var slideshow_interval;
	
	var slideshow_data = [
		'http://vinagency.com/wp-content/themes/vin/images/header_1.jpg',
		'http://vinagency.com/wp-content/themes/vin/images/header_2.jpg',
		'http://vinagency.com/wp-content/themes/vin/images/header_5.jpg',
		'http://vinagency.com/wp-content/themes/vin/images/header_6.jpg',
		'http://vinagency.com/wp-content/themes/vin/images/header_4.jpg',
		'http://vinagency.com/wp-content/themes/vin/images/header_3.jpg'
	];
		
	if (jQuery.browser.msie && jQuery.browser.version.substr(0,1) < 7) {
		//return;
	} else {
		
		if (jQuery('body').attr('id') == 'home'){
			startSlideshow();
		}
	}
//console.log('!');
	function startSlideshow(){
		
//console.log('startSlideshow');
		
		jQuery('div.'+outer_wrapper_class).attr('id','bottom').css('z-index',1);
		
		setupControls();
		
		adjustControls();
		
		slideshow_interval = setInterval(nextSlide, slideshow_delay * 1000);
	}
	
	function adjustControls(){
		jQuery('#'+controls_id+' a').each(function(i){
			if (i == current_index){
				jQuery(this).addClass('active');
			} else {
				jQuery(this).removeClass('active');
			}		 
		});
	}
	
	function setupControls(){
		//jQuery('#'+controls_id).show();
		jQuery('#'+controls_id+' a').each(function(i){
			jQuery(this).attr('rel',i).bind('click', function(e){
				current_index = jQuery(e.currentTarget).attr('rel');
				current_index--;
				nextSlide();
				return false;
			});
		});
		
		jQuery('#slideshow-controls').slideUp();
		
		jQuery('#banner').hover(
			function(){
				jQuery('#'+controls_id).slideDown();
			},
			function(){
				jQuery('#'+controls_id).slideUp();
			});
		
	}
	
	function nextSlide(){
		
		clearInterval(slideshow_interval);
		
		current_index++;
				
		if (current_index >= slideshow_data.length){ current_index = 0;}
	
//console.log('nextSlide '+current_index);
	
		var img_src = slideshow_data[current_index];
		img_src += '?random=' + (new Date()).getTime() +'';//ie caching problem fix
	
		jQuery('<img />')		
			.attr('src', img_src)
			.one('load', function(){
				
//console.log('loaded '+slideshow_data[current_index]);
				
				jQuery('div#'+outer_wrapper_id)
					.append( 
						jQuery('<div />')
							.addClass(outer_wrapper_class)
							.hide()
							.css({
								'background-image':'url('+slideshow_data[current_index]+')',
								'z-index':10
							})
							.fadeIn('slow',function(){
								jQuery('div#'+outer_wrapper_id).find('div.'+outer_wrapper_class+'[id=bottom]').remove();
								jQuery(this).css('z-index',1).attr('id','bottom');				    
							})
					);
				adjustControls();
				slideshow_interval = setInterval(nextSlide, slideshow_delay * 1000);
			})
			.each(function(){
			//Cache fix for browsers that don't trigger .load()
				if (this.complete){
					$(this).trigger('load');
				}
			});
	
	}


});