function createSlider(){
	var slider_elements_qtd = $('.img-slider').children().length-3;
	$('.img-slider').css('width',(parseInt($('.container').css('width')))*slider_elements_qtd);
	if(slider_elements_qtd > 1){
		showBtnNext(false);
	}
}

function showBtnNext(effects){
	if(typeof(effects)=="undefined") $('.next-actions').stop().animate({left: '965px'},"medium");
	else $('.next-actions').stop().css('left','965px');
}

function hideBtnNext(effects){
	if(typeof(effects)=='undefined') $('.next-actions').stop().animate({left: '998px'},"medium");
	else $('.next-actions').stop().css('left','998px');
}

function showBtnPrevious(effects){
	if(typeof(effects)=='undefined') $('.prev-actions').stop().animate({left: '0px'},"medium");
	else $('.prev-actions').stop().css('left','0px');
}

function hideBtnPrevious(effects){
	if(typeof(effects)=='undefined') $('.prev-actions').stop().animate({left: '-34px'},"medium");
	else $('.prev-actions').stop().css('left','-34px');
}

function refreshArrows(){
	var slider_elements_qtd = $('.img-slider').children().length-3;
	
	if((parseInt($('.img-slider img').css('left')) + parseInt($('.img-slider').css('width')) > 998)) showBtnNext();
	else hideBtnNext();

	if(parseInt($('.img-slider img').css('left')) != 0) showBtnPrevious();
	else hideBtnPrevious();
}

function hideControllers(){
	hideBtnNext();
	hideBtnPrevious();
	hideDragBox();
}

function isLastMedia(){
	if ((parseInt($('.img-slider img').css('left')) + parseInt($('.img-slider').css('width')) - 998) == 998) return true;
	return false;
}

function showEffects(){
	if (isLastMedia()){
		$('.content-menu-wrapper').delay(2000).animate({height: '143px'},1000);
	}
	
}

function goToNextWork(){
	if($('.the-hidden-background').css('display') == 'none'){
		if(theSignal == false){
			theSignal = true;
			var currentDistance = parseInt($('.img-slider img').css('left'))-998;
			var slider_elements_qtd = getSliderElementsQtd();
			
			if((parseInt($('.img-slider img').css('left')) + parseInt($('.img-slider').css('width')) > 998)){
				if(effects == "slider")
					$('.img-slider img').animate({left: currentDistance},850,function(){refreshArrows();theSignal = false;} );
				else {
					$('.img-slider img').fadeTo('medium', 0.2, function() {
						$('.img-slider img').css('left',currentDistance);
					});
					$('.img-slider img').fadeTo('medium', 1,function(){theSignal = false;});
				}
				showEffects();
				currentMedia++;
				$('.counter .current').html(parseInt($('.counter .current').html()) + 1);
				updateContentData();
			} else
				theSignal = false;
		}
	}
}

function goToPreviousWork(){
	if($('.the-hidden-background').css('display') == 'none'){
		if(theSignal == false){
			theSignal = true;
			var currentDistance = parseInt($('.img-slider img').css('left'))+998;
			var slider_elements_qtd = $('.img-slider').children().length-3;
			
			if(parseInt($('.img-slider img').css('left')) != 0){
				if(effects == "slider"){
					$('.img-slider img').animate({left: currentDistance},850,function(){refreshArrows();theSignal = false;});
				} else {
					$('.img-slider img').fadeTo('medium', 0.2, function() {
						$('.img-slider img').css('left',currentDistance);
					});
					$('.img-slider img').fadeTo('medium', 1, function(){theSignal = false;});
				}
				currentMedia--;
				$('.counter .current').html(parseInt($('.counter .current').html()) - 1);
				updateContentData();
			} else
				theSignal = false;
		}
	}
}

function hideDragBox(){
	$('.information').stop().animate({opacity:0},"slow",function(){$(this).hide()});
}

function showDragBox(){
	$('.information').show();
	$('.information').stop().animate({opacity:1},"slow");
}

function minimizeDragBox(){
	if(parseInt($('.information').css('height')) > parseInt(27))
		lastInformationHeight = document.getElementById('information').offsetHeight;
	if($('.information .content').css('display') == "none"){
		$('.information').css("opacity",1);
		$('.information').animate({height: lastInformationHeight},'medium')
	} else{
		$('.information').css("opacity",0.65);
		$('.information').animate({height: '27px'},'medium')
	}
	$('.information .content').slideToggle('medium');
	$('.ui-resizable-se').slideToggle('medium');
	if(parseInt(lastInformationHeight) > parseInt(27))
		lastInformationHeight = $('.information').css('height');
}

function changeTheme(theme){
	theme = parseInt(theme);
	switch(theme){
		case 1:
			$('body').animate({backgroundColor:'#000000'},'slow');
			$('.header').animate({backgroundColor: '#333333'},'slow');
			$('.footer').animate({backgroundColor:'#333333'},'slow');
			$('.theme-manager').animate({backgroundColor:'#333333'},'slow');
			break;
		case 2:
			$('body').animate({backgroundColor:'#333333'},'slow');
			$('.header').animate({backgroundColor: '#000000'},'slow');
			$('.footer').animate({backgroundColor:'#000000'},'slow');
			$('.theme-manager').animate({backgroundColor:'#000000'},'slow');
			break;
		case 3:
			$('body').animate({backgroundColor:'#000000'},'slow');
			$('.header').animate({backgroundColor:'#000000'},'slow');
			$('.footer').animate({backgroundColor:'#000000'},'slow');
			$('.theme-manager').animate({backgroundColor:'#000000'},'slow');
			break;
		case 4:
			$('body').animate({backgroundColor:'#3333333'},'slow');
			$('.header').animate({backgroundColor: '#333333'},'slow');
			$('.footer').animate({backgroundColor:'#333333'},'slow');
			$('.theme-manager').animate({backgroundColor:'#333333'},'slow');
			break;
	}
	var params = "theme="+theme;
	$.ajax({
	    type: 'POST',
	    url: template_url+"/do.change.theme.php",
	    data: params
	});
}

function showContactForm(){
	$('.the-hidden-background').css('opacity','0.9');
	$('.the-hidden-background').fadeIn('slow');
	$('.contact-form').animate({height:'445px'}, "slow");
	
	var params = "";
		$.ajax({
	     type: 'POST',
	     url: template_url+"/do.form.contact.php",
	     data: params,
	     success: function(data){
	     	$('.contact-form-wrapper').html(data);
	     	$('.contact-form-wrapper').css('height','445px').fadeIn('medium');
	     }
	});
}

function updateContentData(){
	currentMediaTitle = $('.img-slider img:nth-child('+currentMedia+')').attr('title');
	currentMediaTitle = currentMediaTitle.split(' - ');

	client_area = currentMediaTitle[0];
	client_name = currentMediaTitle[1];
	
	post_id = parseInt($('.img-slider').attr('id'));

	old_client_area = $('.other-works-this-category h3 a').html();
	old_client_name = $('.other-works-this-client h3 a').html();
	
	if ((client_name != old_client_name) || (client_area != old_client_area)) {
		var params = "client_name="+client_name+"&client_area="+client_area+"&old_client_area="+old_client_area+"&old_client_name="+old_client_name+"&post_id="+post_id;
		$.ajax({
			type: 'POST',
			dataType: 'json',
			url: template_url+"/do.update.media.data.php",
			data: params,
			success: function(data){
				if (data.result == 'success') {
					if (typeof(data.client_area_name) != "undefined") $('.other-works-this-category h3 a, #information .content-wrapper .info-cat').animate({opacity: 0},"slow", function(){$(this).html(data.client_area_name);$(this).animate({opacity: 1},"slow");$(this).attr('href',site_base_url+'/'+toPermalink(data.client_area_name));});
					if (typeof(data.client_name) != "undefined") $('.other-works-this-client h3 a, #information .title-wrapper span').animate({opacity: 0},"slow", function(){$(this).html(data.client_name);$(this).animate({opacity: 1},"slow");$(this).attr('href',site_base_url+'/'+toPermalink(data.client_name));});
					if (typeof(data.category_works_thumbs) != "undefined") $('.other-works-category-thumbs-wrapper').animate({opacity: 0},"slow", function(){$(this).html(data.category_works_thumbs);$(this).animate({opacity: 1},"slow");setTimeout("refreshReflection('area');", 1000);});
					if (typeof(data.client_works_thumbs) != "undefined") $('.other-works-client-thumbs-wrapper').animate({opacity: 0},"slow", function(){$(this).html(data.client_works_thumbs);$(this).animate({opacity: 1},"slow");setTimeout("refreshReflection('client');", 1000);});
				}
		    }
		});
	}
}

function showIndicateForm(){
	$('.the-hidden-background').css('opacity','0.9');
	$('.the-hidden-background').fadeIn('slow');
	
	var params = "";
	$.ajax({
    	type: 'POST',
    	url: template_url+"/do.form.indicate.php",
    	data: params,
    	success: function(data){
	     	$('.contact-form-wrapper').html(data);
	     	$('.contact-form-wrapper').css('height', '445px').fadeIn('medium');
	    }
	});
}

function hideContactForm(){
	$('.contact-form').animate({height:'445px'}, "slow");
	$('.the-hidden-background').css('opacity','0');
	$('.the-hidden-background').fadeOut('slow');
	$('.contact-form-wrapper').fadeOut('medium');
}

function refreshReflection(target){
	if (target == 'client')
		$(".other-works-this-client img").reflect({opacity: 0.3,height: 0.9});
	else if (target == 'area')
		$(".other-works-this-category img").reflect({opacity: 0.3,height: 0.9});
}

function toPermalink(s){
	sA = s.split('');
	sAO = new Array();
	sLength = s.length;
	var accents = 'ÀÁÂÃÄÅàáâãäåÒÓÔÕÕÖØòóôõöøÈÉÊËèéêëðÇçÐÌÍÎÏìíîïÙÚÛÜùúûüÑñŠšŸÿýŽž _@!?=()*&%$#\/';
	var aPattern = ['A','A','A','A','A','A','a','a','a','a','a','a','O','O','O','O','O','O','O','o','o','o','o','o','o','E','E','E','E','e','e','e','e','e','C','c','D','I','I','I','I','i','i','i','i','U','U','U','U','u','u','u','u','N','n','S','s','Y','y','y','Z','z','-','-','','','','','','','','','','','','-','-'];
	for (var y = 0; y < sLength; y++) {
	    if (accents.indexOf(sA[y]) != -1) {
	        sAO[y] = aPattern[accents.indexOf(sA[y])];
	    }
	    else
	        sAO[y] = sA[y];
	}
	s = sAO.join('');
	
	return s.toLowerCase().replace('/ /gi','-');	
}

function getSliderElementsQtd(){
	return parseInt($('.img-slider').children().length-3);
}

function hideInformationButton(effects){
	if(typeof(effects)=='undefined') $('.information-link').stop().animate({left: '-36px'},"medium");
	else $('.information-link').stop().css('left','-36px');
}

function showInformationButton(effects){
	if(typeof(effects)=='undefined') $('.information-link').stop().animate({left: '0px'},"medium");
	else $('.information-link').stop().css('left','0px');
}

function canType(target, limit){
	$('#'+target+'_counter').html(parseInt(limit - $('#'+target).val().length));
}

function goDownThings(){
	var things_position = parseInt($('.list-things .list-wrapper').css('top'));

	if (things_position < 0) {
		$('.list-wrapper').css('top',((things_position+5)+'px'));
	}
}

function goUpThings(){
	var list_things_height = parseInt($('.list-things').css('height'));
	var things_position = parseInt($('.list-things .list-wrapper').css('top'));
	total_things_height = 0;

	var things_total_height = 0;
	$('.list-things').children().each(function(index){
		things_total_height += parseInt($(this).css('height'));
	});

	if (things_position > (list_things_height-things_total_height)) {
		$('.list-wrapper').css('top',(things_position-5)+'px');
	}
}

function showFeedbackForm(){
	$('.the-hidden-background').css('opacity','0.9');
	$('.the-hidden-background').fadeIn('slow');
	
	var params = "";
		$.ajax({
	     type: 'POST',
	     url: template_url+"/do.form.feedback.php",
	     data: params,
	     success: function(data){
	     	$('.contact-form-wrapper').html(data);
	     	$('.contact-form-wrapper').css('height','445px').fadeIn('medium');
	     }
	});
}
