$(document).ready(function(){
	$('.overFade').hover(ovrFade,outFade);
	overZoomInit();
	$('.overZoom').hover(ovrZoom,outZoom);
	
	$('#menu li.main-item').hover(dd_hover, dd_out);
	$('#menu .dropdown li').hover(dd_hover2, dd_out2);
	
});



// hover: FADE
function ovrFade(){$(this).animate({opacity:0.5}, 200);}
function outFade(){$(this).animate({opacity:1}, 200);}
// hover:ZOOM
function overZoomInit(){
	$('.overZoomInner').each(function(){
		$(this).width($(this).next().width());
		$(this).height($(this).next().height());
	});
	//$('.overZoomInner').width($('img',this).width());
	//$('.overZoomInner').height($('img',this).height());
	
}
function ovrZoom(){
	$('.overZoomInner', this).fadeTo(100, 1);
}
function outZoom(){
	$('.overZoomInner', this).fadeTo(100,0);
}


// MAIN NAVIGATION DROPDOWN

function menu_dd_init(HOME){
	$('#menu .tabs').prepend($('#menu_port'));
	$('#menu .tabs').prepend($('#menu_blog'));
	$('#menu .tabs').prepend($('#menu_home'));
	
	$('#menu .tabs ul').removeClass().addClass('dropdown');
	$('ul.dropdown ul').removeClass().addClass('dropdown2');

	$('#menu li').removeClass().addClass('main-item');
	$('.main-item li').removeClass();
	
	$('.dropdown').parent().addClass('hasmore');
	$('.dropdown2').parent().addClass('hasmore2');
	
	$('#menu .tabs').show();
}
function dd_hover(){ $('ul:eq(0)', this).slideDown(100); }
function dd_out(){ $('ul:eq(0)', this).slideUp(100); }
function dd_hover2(){ $('ul:eq(0)', this).slideDown(200); }
function dd_out2(){ $('ul:eq(0)', this).slideUp(100); }





// MS [mainSlider]

        var MS_width = 0; 			// var: Main slider width
		var MS_itemWidth = 0;		// var: Slide width
		var MS_itemHeight = 0;		// var: Slide height
		var MS_prevSlide = 0; 		// var: Previous slide id
		var MS_totalItems = 0; 		// var: Slide count
		var autoplay = true; 		// var: Autoplay state
		var MS_direction = '';		// var: Slider direction
		var MS_ready = true;

		var MS_delay = 4000;
		var MS_speed = 'slow';
		
		function MS_autoplay(delay){
			if(autoplay==true){
				MS_right(1);
				setTimeout("MS_autoplay("+delay+")", delay);
			}
		}
		
		// prepares main slider
		function MS_Init(delay, speed, direction){
			// gets the slider direction
			MS_direction = direction; 
			if(delay!=null){MS_delay = delay};
			if(speed!=null){MS_speed = speed};
			
			// sets dimensions
			MS_width = $('#MS').width();
			MS_itemWidth = MS_width;
			MS_itemHeight = $('.MS_listItem').height();
			
			// clones the firts slide and appends it after the last slide
			$('#MS .MS_listItem:first').clone().appendTo('#MS .MS_list');
			
			// counts the total number of slides
			MS_totalItems = ($('#MS .MS_listItem').length)-1;
			
			//creates control buttons for slides
			$('#MS_controls').html('');
			for(i=0; i<(MS_totalItems); i++){
				$('#MS_controls').append('<div class="MS_slideButton" id="MS_btn'+i+'" onclick="MS_any('+i+')"></div>');
			}
			$('#MS_controls').append('<div class="clear"></div>');
			
			
			
			
			if(MS_direction=='dirHor'){
				$('#MS .MS_list').width(MS_width*((MS_totalItems+1)*1.2));
				$('#MS .MS_list').height(MS_itemHeight);
			} else if(MS_direction=='dirVer'){
				$('#MS .MS_list').width(MS_width);
				$('#MS .MS_list').height(MS_itemHeight*(MS_totalItems*1.2));
			}
			
			//sets slider's autoplay to true
			autoplay=true;
			
			MS_prevSlide=0;
			MS_setActiveBtn(0);
			
			setTimeout("MS_autoplay("+delay+")", delay);
		}
		
		// moves to any slide
		function MS_any(x){
			if(MS_ready==true){
				autoplay=false;
				MS_ready=false;
				MS_setActiveBtn(x);
				if(MS_direction=='dirHor'){
					$('#MS .MS_list').animate({marginLeft:-(MS_itemWidth*x)}, MS_speed, "easeOutExpo", function(){
						MS_prevSlide = x;
						MS_ready=true;
					});
				} else if(MS_direction=='dirVer'){
					$('#MS .MS_list').animate({marginTop:-(MS_itemHeight*x)}, MS_speed, "easeOutExpo", function(){
						MS_prevSlide = x;
						MS_ready=true;
					});
				}
			}
		}
        
		// moves one slide right
        function MS_right(auto){
			if(MS_ready==true){
				MS_ready=false;
				if(auto==0){autoplay=false;}
				currSlide = MS_prevSlide+1;
				if(currSlide>(MS_totalItems-1)){
					autoplay=false;
					MS_setActiveBtn(0);			
				} else {
					MS_setActiveBtn(currSlide);			
				}
				
				if(MS_direction=='dirHor'){
					$('#MS .MS_list').animate({marginLeft:-(MS_itemWidth*currSlide)}, MS_speed, "easeOutExpo", function(){
						if(currSlide>(MS_totalItems-1)){
							currSlide=0;
							$('#MS .MS_list').css('margin-left','0');
						}
						MS_ready=true;
						MS_prevSlide=currSlide;
					});
				} else if(MS_direction=='dirVer'){
					$('#MS .MS_list').animate({marginTop:-(MS_itemHeight*currSlide)}, MS_speed, "easeOutExpo", function(){
						if(currSlide>(MS_totalItems-1)){
							currSlide=0;
							$('#MS .MS_list').css('margin-top','0');
						} 
						MS_ready=true;
						MS_prevSlide=currSlide;
					});
				}
			}
        }
        
		// moves one slide left
        function MS_left(){
			if(MS_ready==true){
				MS_ready=false;
				autoplay=false;
				currSlide = MS_prevSlide-1;
				
				if(MS_direction=='dirHor'){
					if(currSlide<0){
						currSlide = (MS_totalItems-1);
						$('#MS .MS_list').css('margin-left','-'+(MS_itemWidth*MS_totalItems)+'px');
					}
					MS_setActiveBtn(currSlide)
					$('#MS .MS_list').animate({marginLeft:-(MS_itemWidth*currSlide)}, MS_speed, "easeOutExpo", function(){
						MS_prevSlide=currSlide;
						MS_ready=true;
					});
				} else if(MS_direction=='dirVer'){
					if(currSlide<0){
						currSlide = (MS_totalItems-1);
						$('#MS .MS_list').css('margin-top','-'+(MS_itemHeight*MS_totalItems)+'px');
					}
					MS_setActiveBtn(currSlide)
					$('#MS .MS_list').animate({marginTop:-(MS_itemHeight*currSlide)}, MS_speed, "easeOutExpo", function(){
						MS_prevSlide=currSlide;
						MS_ready=true;
					});
				}
			}
        }
		
		// highlights current slide button
		function MS_setActiveBtn(x){
			$('#MS_controls .MS_slideButton.current').toggleClass('current');
			$('#MS_controls #MS_btn'+x).addClass('current');
		}






// WORKS [trainSlider]

$(document).ready(function(){
	trainInit();
});
var trItemWidth = 0;

function trainInit(){
	trItemWidth = $('.trainSlider:first .trainItem').width();
}

function train_left(SLIDER){
	$('#'+SLIDER+' .trainList').animate({marginLeft:-trItemWidth}, 200, function(){
		$('#'+SLIDER+' .trainList').append($('#'+SLIDER+' .trainList .trainItem:first'));
		$('#'+SLIDER+' .trainList').css('margin-left','0');
	});
}

function train_right(SLIDER){
	$('#'+SLIDER+' .trainList').prepend($('#'+SLIDER+' .trainItem:last'));
	$('#'+SLIDER+' .trainList').css('margin-left','-'+trItemWidth+'px');
	$('#'+SLIDER+' .trainList').animate({marginLeft:0}, 200 );
}


function train_showControls(ID){
	$('#'+ID).show();
}



// Description/tagline fix
function descFix(){
	descHeight = $('.page_desc').height();
	if(descHeight < 50){descHeight=50};
	$('.headline_title').height(descHeight);
}
	

// POST GALLERY

var vitWidth = 0;
var vitListWidth = 0;
var vitSingleWidth = 0;
var vitReady = false;

function vitrinaInit(){
	if ( $('#postGallery')[0] && $('#postGallery')!='' ) {
		vitWidth = $('#vitrina').width();
		$('#postGallery img').each(function(i){
			$(this).appendTo('#vitrinaList');
			vitSingleWidth = $(this).width();
			vitListWidth += vitSingleWidth;
		});
		if(vitListWidth>vitWidth){$('#vitrinaControls').show();}
		$('#vitrinaList').width(vitListWidth*(1.2));
		$('#vitrina img').show();
		$('#postGallery').remove();
		vitReady=true;
	} else {$('#vitrina').hide();}
} 


function vitrinaLeft(){
	if(vitReady==true){
		vitReady=false;
		var vitImgWidth = $('#vitrinaList img:last').width()+3;
		$('#vitrinaList').prepend($('#vitrinaList img:last'));
		$('#vitrinaList').css('margin-left','-'+vitImgWidth+'px');
		
		$('#vitrinaList').animate({marginLeft:0}, 200, function(){
			vitReady=true;
		});
	}
}

function vitrinaRight(){
	if(vitReady==true){
		vitReady=false;
		var vitImgWidth = $('#vitrinaList img:first').width()+3;
		
		$('#vitrinaList').animate({marginLeft:-vitImgWidth}, 200, function(){
			$('#vitrinaList').append($('#vitrinaList img:first'));
			$('#vitrinaList').css('margin-left',0);
			vitReady=true;
		});
	}
}





// PORTFOLIO LIST STYLE SWITCH

$(document).ready(function () {
	storeSettings();
	listTypeInit($.cookie('VilistType'));
});

function storeSettings(){
	if(!$.cookie('VilistType')){
		$.cookie('VilistType', 'grid');
	}
}


var prevListType;

function listTypeSwitch(type){
	Cufon.refresh();
	if(type!=prevListType){
		$('#portfolio-listing').hide(); $('#portfolio-listing').removeClass().addClass(type).fadeIn(200);
		if(type=='grid'){
			$('#ltgrid').removeClass().addClass('listing-btn grid active');
			$('#ltlist').removeClass().addClass('listing-btn list');
			$.cookie('VilistType', 'grid');
			Cufon.refresh();
		} else {
			$('#ltlist').removeClass().addClass('listing-btn list active');
			$('#ltgrid').removeClass().addClass('listing-btn grid');
			$.cookie('VilistType', 'list');
			Cufon.refresh();
	}
		prevListType = type;
	}

}

function listTypeInit(type){
	if(type=='grid'){
		$('#ltgrid').removeClass().addClass('listing-btn grid active');
		$('#ltlist').removeClass().addClass('listing-btn list');
		$.cookie('VilistType', 'grid');
	} else {
		$('#ltlist').removeClass().addClass('listing-btn list active');
		$('#ltgrid').removeClass().addClass('listing-btn grid');
		$.cookie('VilistType', 'list');
	}
	prevListType = type;
	
}




// VIDEO CORNER

var VC_width = 0;
var VC_height = 0;
var VC_totItems = 0;
var VC_prevItem = 0;

function vidCornerInit(){
	VC_width = $('#vidCornerListWrap').width();
	VC_height = $('#vidCornerListWrap').height();
	VC_totItems = $('.vidCornerListItem').length;
	$('.vidCornerList').width(VC_width*VC_totItems).height(VC_height);
	$('.vidCornerListItem').width(VC_width).height(VC_height);
	VC_prevItem = 0;
	
	// VIDEO CORNER CONTROLS
	$('#vidCornerControls').html('');
	for(i=0; i<VC_totItems; i++){
		$('#vidCornerControls').append('<div class="VC_btn" onclick="vidCornerSlide('+i+')"></div>');
	}
	$('#vidCornerControls').append('<div class="clear"></div>');
	
	vidCornerSlide(0);
}

function vidCornerSlide(x){
	$('.vidCornerList').animate({marginLeft:-(x*VC_width)}, 200);
	$('.VC_btn.active').toggleClass('active');
	$('.VC_btn:eq('+x+')').addClass('active');
}




// TOOLTIP

$(document).ready(function() {
 
    $('a[rel=tooltip]').mouseover(function(e) {
        var tip = $(this).attr('title');   
        $(this).attr('title','');
        $(this).append('<div id="tooltip"><div class="tipHeader"></div><div class="tipBody">' + tip + '</div><div class="tipFooter"></div></div>');    
        $('#tooltip').css('top', e.pageY + 10 );
        $('#tooltip').css('left', e.pageX + 20 );
        $('#tooltip').fadeIn('500');
        $('#tooltip').fadeTo('10',0.8);
    }).mousemove(function(e) {
        $('#tooltip').css('top', e.pageY + 10 );
        $('#tooltip').css('left', e.pageX + 20 );
    }).mouseout(function() {
        $(this).attr('title',$('.tipBody').html());
        $(this).children('div#tooltip').remove();
    });
});
