var PAGE_NAME = location.href.substring(location.href.lastIndexOf('/')+1,location.href.lastIndexOf('.asp')+4);
var CENTRAL_BANNER_TRANSITION_TIME = 5000;
var SLIDING_BANNER_WIDTH = 980;
var slide_counting = 6;
var INDICATOR_CLASS_REGEX = /bottom_banner_indicator_item_\d/;
var INDICATOR_REGEX = /\d/;
var current_slide = 1;
var acting = false;
var scrollThumbs;
var mOver = false;
var thumbWidth, thumbMargin;
var initXTouch = 0;
var workDetailsSlideCounting = 1;
var workDetailsSlideScrolling = false;
var workDetailWidth = 0;
var fullScreenGalleryScroll = 0; 
var fncTmOut;
var swiped = false;
var touchStrtTime = 0;
var pressedBoxWorkDetailId = 0;

var supportTouch = $.support.touch,
        scrollEvent = "touchmove scroll",
	gestureStartEvent = "gesturestart",
	gestureEndEvent = "gestureend",
	tapEvent = supportTouch ? "tap" : "click",
        touchStartEvent = supportTouch ? "touchstart" : "mousedown",
        touchStopEvent = supportTouch ? "touchend" : "mouseup",
        touchMoveEvent = supportTouch ? "touchmove" : "mousemove";


$(function(){
	switch(PAGE_NAME.toString()){

		case "we_think.asp":
			$('.main-top-content-menu-list li').live(tapEvent, function(e){
				var _link = $(this).children('a').attr('href');
				location.href = _link;
			});
		break;
		case "services.asp":

			$('.main-top-content-menu-list li').live(tapEvent, function(){
				var serviceIdClassRegex = /service\-id\-\d+/;
				var serviceIdClass = serviceIdClassRegex.exec($(this).attr('class'));
				var serviceIdRegex = /\d+/;
				var serviceId = serviceIdRegex.exec(serviceIdClass);
				$.getJSON('ajax/json_service_info.asp',
					{ id_service : parseInt(serviceId) },
					function(json){
						$('#service_description, #service-image').fadeOut('fast', function(){
							$('#service_description').html('<p>' + json.serviceDescription + '</p>');
							$('.main-top-content-section').text(json.serviceName.replace(',',' + '));
							$('#service-image').html('<img src="images/classificacao_images/' + json.serviceImage + '" alt="' + json.serviceName + '" />');
							$('#service_description').fadeIn();
							$('#service-image').fadeIn();
						});
					}
				);
			});
 
			break;

		case "work.asp":

			if(!supportTouch){

				$('.middle-content-item').live('click',function(){

					var workDetailsRegex = /work\-detail\-box\-id\-\d+/;
					var workDetailsIdWorkClass = workDetailsRegex.exec($(this).attr('class'));
					var workIdRegex = /\d+/;
					var workId = workIdRegex.exec(workDetailsIdWorkClass);

					loadWorkDetail(workId, function(){
												$('#workDetailsDotBar').show();
												$('#box-work-details').animate({ height: 670 },1100,function(){
													$('#box-work-details div:not(.work-details-nav-arrow)').fadeIn(1000);
												});
												$(window).scrollTop(0);
											}
					);

				});

				$('.middle-content-item-image').mouseenter(function(event){
					event.preventDefault();
					$(this).parent().parent().find('.middle-content-item-image-blur').remove();
					$(this).parent().prepend('<div class="middle-content-item-image-blur"></div>');
					$('.middle-content-item-image-blur').show();

					$('.middle-content-item-image-blur').live(touchStartEvent,function(event){
						$(this).parent().find('img').removeClass('middle-content-item-image');
						$(this).parent().find('img').addClass('middle-content-item-image-clicked');
						$(this).parent().find('.middle-content-item-image-blur').addClass('middle-content-item-image-blur-clicked');
					});
					$('.middle-content-item-image-blur').live(touchStopEvent,function(event){
						$(this).parent().find('img').removeClass('middle-content-item-image-clicked');
						$(this).parent().find('img').addClass('middle-content-item-image');
						$(this).parent().find('.middle-content-item-image-blur').removeClass('middle-content-item-image-blur-clicked');
					});
					$('.middle-content-item-image-blur').mouseout(function(event){
						event.preventDefault();
						$(this).parent().find('.middle-content-item-image-blur').fadeOut('slow',function(){
								$(this).find('.middle-content-item-image-blur').remove();
						});
					});

					$('.itemTitle, .itemClientName, .itemFeatures').mouseover(function(event){
						$(document).find('.middle-content-item-image-blur').remove();						
					});

				});

					$(document).mouseleave(function(event){
						$(document).find('.middle-content-item-image-blur').remove();
					});

					$('#work-details-slides-container, .work-details-nav-arrow').mouseenter(function(){
						$('.work-details-nav-arrow').show();
					});
					$('#work-details-slides-container, .work-details-nav-arrow').mouseleave(function(){
						$('.work-details-nav-arrow').hide();
					});


			} else {

				$('.middle-content-item').live(touchStartEvent,function(event){
					//event.preventDefault();
					event.stopPropagation();
					$('#footer').text(new Date().getTime().toString());
					
					touchStrtTime = new Date();
					
					$(this).find('img').removeClass('middle-content-item-image');
					$(this).find('img').addClass('middle-content-item-image-clicked');
					$(this).find('.middle-content-item-image-blur').addClass('middle-content-item-image-clicked');

					var workDetailsRegex = /work\-detail\-box\-id\-\d+/;
					var workDetailsIdWorkClass = workDetailsRegex.exec($(this).attr('class'));
					var workIdRegex = /\d+/;
					var workId = workIdRegex.exec(workDetailsIdWorkClass);
					pressedBoxWorkDetailId = workId;


				});
				$('.middle-content-item-image-clicked, .middle-content-item').live(touchStopEvent,function(event){
					event.preventDefault();
					$(this).parent().find('img').removeClass('middle-content-item-image-clicked');
					$(this).parent().find('img').addClass('middle-content-item-image');
					
					
					if(new Date().getTime() - touchStrtTime.getTime() <= 200 ){
					
								loadWorkDetail(pressedBoxWorkDetailId, function(){
										$('#workDetailsDotBar').show();
										$('body').animate({ scrollTop : 0}, 500, function(){
											$('#box-work-details').animate({ height: 710 },600,function(){
												$('#box-work-details div').fadeIn(1000);
												$('.work-details-nav-arrow').show();
											});
										});
									}
								);

					}
					touchStrtTime = 0;
				});

			}

			$(document).keydown(function(event){
				processKeyCommands(event);
			});


			$('.main-top-content-menu-list li').live(tapEvent, function(e){
				var selectedClass = $(this).text();
				$('#workDetailsDotBar').hide();
				$('.main-top-content-section').text(selectedClass);
				$('#middle-content-box div').hide();
				$('#middle-content-box div').removeClass('last-item');

				if($(this).is('.main-top-content-menu-list-item-all-categs')){
					$('#middle-content-box div').fadeIn();
				} else { 
					$('#middle-content-box div').each(function(){
						if($(this).attr('class').indexOf(selectedClass) != -1){
							$(this).fadeIn();
						}
					});
				}
				var c = 0;
				$('#middle-content-box div:visible').each(function(){
					if(c == 3){
						 $(this).addClass('last-item');
						c = 0;
					} else {
					c++;
					}
				});

				$('#box-work-details div').fadeOut(1000);
				$('.work-details-nav-arrow').fadeOut(1000);
				$('#box-work-details').animate({ height: 0},1000,function(){
						$(window).scrollTop(0);
				});
					

			});


			$('#open_full_screen_gallery').live(tapEvent, function(e){
				$(window).scrollTop(0);
				$('#full_screen_gallery').showPopup();

				adjustFullScreenGallery();

				$(window).resize(function(){
					adjustFullScreenGallery();
				});
			});

			if(!supportTouch){
				$(document).mousemove(function(e){
					if($('#full_screen_gallery').is(':visible') && !$('.full_screen_info_bar').is(':visible')){
						if(parseInt(e.pageY) - parseInt($(document).scrollTop()) <= 40 || parseInt(e.pageY) - parseInt($(document).scrollTop()) >= parseInt($(window).height()) - 40){
							showfullScreenGalleryInfoBar();
						}
					}
				});
				$('.full_screen_info_bar').mouseleave(function(){
					hidefullScreenGalleryInfoBar();
				});
			} else {
				$('#full_screen_gallery').live('touchstart', function(event){
					event.preventDefault();
				});

			}

			$('#full_screen_gallery_footer').bind('tap',function(e){
				e.preventDefault();
				$('#full_screen_gallery').showPopup({ show : false });
			});


			break;

		case "contact.asp":
				$('#btnSendMessage').live(tapEvent, function(event){
					var txtName = $('#txtName').val();
					var txtEmail  = $('#txtEmail').val();
					var txtMsg = $('#txtMsg').val();

					$.post('ajax/ajax_send_msg.asp',
						{ txtName : txtName , txtEmail : txtEmail , txtMsg : txtMsg },
						function(result){
							if(parseInt(result) == 1){
								alert('E-mail enviado com sucesso.\nObrigado por entrar em contato com a 3mw.');
								 $('#txtName').val('');
								$('#txtEmail').val('');
								$('#txtMsg').val('');
							}else{
								alert('Erro ao enviar o e-mail.\nPor favor, tente novamente mais tarde.');
							}
						}
					);
				});
			break;


		case "default.asp":
		default:

			$('.bottom_banner_indicator').live(scrollEvent,function(event){ event.preventDefault(); });
			$('.bottom_banner_indicator').live(gestureStartEvent,function(event){ event.preventDefault(); });
			$('.bottom_banner_indicator').live(gestureEndEvent,function(event){ event.preventDefault(); });
			$('.bottom_banner_indicator').live(touchMoveEvent,function(event){ event.preventDefault(); });
			$('.bottom_banner_indicator').live(touchStopEvent,function(event){
					event.preventDefault();
			});
			$('.bottom_banner_indicator').live(touchStartEvent,function(event){
				clearTimeout(fncTmOut);
				clearInterval(fncTmOut);
				$('.centralBannerContentBlock').hide();

//				if(acting) return false;

				var iClass = INDICATOR_CLASS_REGEX.exec($(this).attr('class'));
				processIndicatorPress(event, parseInt(INDICATOR_REGEX.exec(iClass)));
			});

			$('#bottom_banner_current_item').touch({
				animate: false,
				sticky: false,
				dragx: true,
				dragy: false,
				rotate: false,
				resort: false,
				scale: false,
				maxLeft: $('#bottomBanner').width() - $('.bottom_banner_current_item').width()
			});

			$('#leftArrow').live(touchStartEvent,function(event){
				event.preventDefault();
				//if(acting) return false;
				if(current_slide > 1) processIndicatorPress(event,parseInt(current_slide)-1);
				else processIndicatorPress(event, slide_counting);
			});
			$('#rightArrow').live(touchStartEvent,function(event){
				event.preventDefault();
				//if(acting) return false;
				if(current_slide < slide_counting) processIndicatorPress(event,parseInt(current_slide)+1);
				else processIndicatorPress(event, 1);
			});

			$('.centralBannerContentBlock').live(touchStartEvent,function(event){
				//event.preventDefault();
				clearInterval(fncTmOut);
				clearTimeout(fncTmOut);

				swiped = false;
			});


			if(!supportTouch){
					$('.centralBannerContentBlock').live(touchStopEvent,function(event){
						event.preventDefault();

						if(!swiped){
							var mascoteIdRegEx = /content\d/;
							var mascoteId = mascoteIdRegEx.exec($(this).attr('id'));
							var _idRegex = /\d/;
							var _id = parseInt(_idRegex.exec(mascoteId)) - 1;
							if(_id> 0){
								$('#topMenu li').each(function(index){
									if(index + 1 == _id){
										$(this).trigger(tapEvent);
									}
								});
							}
						}
					});
			}

			$('.centralBannerContentBlock').swiperight(function(event){
				//event.preventDefault();
				if(acting || current_slide == slide_counting) return false;
				else processIndicatorPress(event, parseInt(current_slide) + 1);

				swiped = true;
			});

			$('.centralBannerContentBlock').swipeleft(function(event){
				//event.preventDefault();
				if(acting || current_slide == 1) return false;
				else processIndicatorPress(event, parseInt(current_slide) - 1);

				swiped = true;
			});

			if(supportTouch){
				 $('.navArrow').show();
			} else {
				$('.centralBannerContentBlock , .navArrow').mouseenter(function(){
					$('.navArrow').show();
				});
				$('.centralBannerContentBlock , .navArrow').mouseleave(function(){
					$('.navArrow').hide();
				});
			}


			$('.navArrow').live(touchStartEvent,function(e){
						clearInterval(fncTmOut);
						clearTimeout(fncTmOut);
			});

			$('.navArrow').live(touchStopEvent,function(e){
			});


			$(document).keydown(function(event){
				switch(event.keyCode){
					case 37:
						clearInterval(fncTmOut);
						clearTimeout(fncTmOut);
						if(acting || current_slide == 1) return false;
						else processIndicatorPress(event, parseInt(current_slide) - 1);

						break;
					case 39:
						clearInterval(fncTmOut);
						clearTimeout(fncTmOut);
						if(acting || current_slide == slide_counting) return false;
						else processIndicatorPress(event, parseInt(current_slide) + 1);

						break;
				}
			});


			setItemsVisibility(false);
			preloadImage();

	}//End Switch


	

	/**** General settings ****/
	$('#topMenu li a[href="' + PAGE_NAME + '"]').parent().addClass('active');
	$('#logo').live(tapEvent, function(event){ location.href = 'http://3mw.com.br'; });


	$('#topMenu li').live(tapEvent, function(){
		var url = $(this).find('a').attr('href');
		location.href = url;
	});

	$('#topMenu li a').live(tapEvent, function(){
		var url = $(this).attr('href');
		location.href = url;
	});

});

var imCounter = 0;
var arrImages = new Array("images/central_banner/content1.jpg",
				"images/central_banner/content2.jpg",
				"images/central_banner/content3.jpg",
				"images/central_banner/content4.jpg",
				"images/central_banner/content5.jpg",
				"images/central_banner/content6.jpg",
				"images/central_banner/mascote1.jpg",
				"images/central_banner/mascote2.jpg",
				"images/central_banner/mascote3.jpg",
				"images/central_banner/mascote4.jpg",
				"images/central_banner/mascote5.jpg",
				"images/central_banner/mascote6.jpg"
			);

function preloadImage(){
	$('<img />')
		.load(function()
			{
				$('#loadingProgress').progressbar("value",parseInt(((imCounter * 100) / (arrImages.length - 1))));
				if(imCounter < arrImages.length - 1){
					++imCounter;
					preloadImage();
				}else{
					setItemsVisibility(true);
					fncTmOut = setTimeout("autoChangeBanner();",CENTRAL_BANNER_TRANSITION_TIME);
				}
			}
		)
		.attr('src', arrImages[imCounter]);
}


function setItemsVisibility(visible){
	if(visible){
		$('#wrapper').fadeIn();
		$('#footer_shadow').fadeIn();

		$('#loadingProgress').hide();
	}else{
		$('#wrapper').hide();
		$('#footer_shadow').hide();

		$('#loadingProgress').progressbar({ value:0 });
		$('#loadingProgress').popup({ show : true });
	}
}


function showfullScreenGalleryInfoBar(){
	$('.full_screen_info_bar').show();
}

function hidefullScreenGalleryInfoBar(){
	$('.full_screen_info_bar').hide();
}

function setupWorkDetails(){

	thumbWidth = $('#main-top-content-gallery-thumbs-thumb-container div').css('width');
	thumbMargin = parseInt($('#main-top-content-gallery-thumbs-thumb-container div').css('margin-left')) > 0 ? parseInt($('#main-top-content-gallery-thumbs-thumb-container div').css('margin-left')) * 2 : parseInt($('#main-top-content-gallery-thumbs-thumb-container div').css('margin-right')) * 2;
	var thumbsNumber = $('#main-top-content-gallery-thumbs-thumb-container div').length;
	var containerWidth = thumbsNumber * (parseInt(thumbWidth)+ thumbMargin);

	$('#main-top-content-gallery-thumbs-thumb-container').css('width', containerWidth);


	workDetailWidth = $('#work-details').width();
	var slidesWidth = $('#work-details-slides-container div.work-detail-slide').length * parseInt(workDetailWidth);
	$('#work-details-slides-container').css('width', slidesWidth);

	$('#main-top-content-gallery-thumbs-thumb-container div').live(touchStartEvent, function(event){

		var counterItemClassRegex = /main\-top\-content-gallery\-thumb\-item\-\d+/;
		var currentItemClass = counterItemClassRegex.exec($(this).attr('class'));
		var counterItemRegex = /\d+/;
		var counterItem = counterItemRegex.exec(currentItemClass);
		$('#work-details').animate({ scrollLeft : counterItem * parseInt(workDetailWidth) });
		$('#full_screen_gallery_slides').animate({ scrollLeft : counterItem * $(window).width() });
		workDetailsSlideCounting = parseInt(counterItem) + 1;

		setGalleryThumbsCurrentItem();

	});

	$('#full_screen_gallery_thumbs td').live(touchStartEvent, function(event){
		var counterItemClassRegex = /full\-screen\-gallery\-thumb\-item\-\d+/;
		var currentItemClass = counterItemClassRegex.exec($(this).attr('class'));
		var counterItemRegex = /\d+/;
		var counterItem = counterItemRegex.exec(currentItemClass);
		$('#full_screen_gallery_slides').animate({ scrollLeft : counterItem * $(window).width() });
		$('#work-details').animate({ scrollLeft : counterItem * parseInt(workDetailWidth) });
		workDetailsSlideCounting = parseInt(counterItem) + 1;
	});

	setGalleryThumbsCurrentItem();

	adjustFullScreenGallery();

	if(!supportTouch){

			$('#main-top-content-gallery-thumbs-right-arrow').unbind();
			$('#main-top-content-gallery-thumbs-left-arrow').unbind();

			$('#main-top-content-gallery-thumbs-right-arrow').mouseenter(function(){
				mOver = true;
				scrollThumbsToLeft();

			});


			$('#main-top-content-gallery-thumbs-right-arrow').mouseleave(function(){
				mOver = false;
				clearTimeOut(scrollThumbs);
			});

			$('#main-top-content-gallery-thumbs-left-arrow').mouseenter(function(){
				mOver = true;
				scrollThumbsToRight();
			});

			$('#main-top-content-gallery-thumbs-left-arrow').mouseleave(function(){
				mOver = false;
				clearTimeOut(scrollThumbs);
			});
	} else {

			$('#main-top-content-gallery-thumbs').processTouch();
			$('#work-details').processTouch({ centerCurrentSlide : true , itemWidth : workDetailWidth });
			$('#full_screen_gallery_thumbs').processTouch();
			$('#full_screen_gallery_slides').processTouch({ centerCurrentSlide : true , itemWidth : $(window).width() });

	}
	$('#main-top-content-gallery-thumbs-left-arrow').live(touchStartEvent, function(event){
		event.preventDefault();
		jumpThumbToRight();
	});

	$('#main-top-content-gallery-thumbs-right-arrow').live(touchStartEvent, function(event){
		event.preventDefault();
		jumpThumbToLeft();
	});
	
	$('#work-details-right-arrow, #full_screen_gallery_slides_right_arrow').live(touchStartEvent,function(event){
		workDetailsSlidesIncreaseSLeft();
	});

	$('#work-details-left-arrow, #full_screen_gallery_slides_left_arrow').live(touchStartEvent,function(event){
		workDetailsSlidesDecreaseSLeft();
	});



}


function adjustFullScreenGallery(){


	var nTd = $('#full_screen_gallery_slides table td').length;
	$('#full_screen_gallery_slides table').width(parseInt($(document).width()) * nTd);
	$('#full_screen_gallery_slides table').height($(window).height());
	$('#full_screen_gallery_slides table td').width($(document).width());
	$('#full_screen_gallery_slides table td').height($(window).height());

	$('#full_screen_gallery_slides table img').each(function(){
		$(this).removeAttr('height');


		var newImg = new Image();
		newImg.src = $(this).attr('src');
		var imgH = newImg.height;

		if(imgH > $(window).height()){
			$(this).height($(window).height());
		}else{
			$(this).height(imgH);
		}

	});

	fullScreenGalleryScroll = $(document).width();
	$('#full_screen_gallery_slides').scrollLeft((workDetailsSlideCounting - 1) * fullScreenGalleryScroll);

}

function workDetailsSlidesDecreaseSLeft(){
	if(!workDetailsSlideScrolling && workDetailsSlideCounting > 1){
		workDetailsSlideCounting--;
		workDetailsSlideScrolling = true;
		$('#work-details').animate({ scrollLeft : (workDetailsSlideCounting - 1) * workDetailWidth }, function(){
			setCurrentWorkDetailSlideFooterLabel();
			workDetailsSlideScrolling = false;
		});
		$('#full_screen_gallery_slides').animate({ scrollLeft : (workDetailsSlideCounting - 1) * $(window).width() });

		setGalleryThumbsCurrentItem();

	}
}


function workDetailsSlidesIncreaseSLeft(){
	if(!workDetailsSlideScrolling && workDetailsSlideCounting < $('#work-details-footer p').length){
		workDetailsSlideCounting++;
		workDetailsSlideScrolling = true;
		$('#work-details').animate({ scrollLeft : (workDetailsSlideCounting - 1) * workDetailWidth }, function(){
			setCurrentWorkDetailSlideFooterLabel();
			workDetailsSlideScrolling = false;
		});
		$('#full_screen_gallery_slides').animate({ scrollLeft : (workDetailsSlideCounting - 1) * $(window).width() });

		setGalleryThumbsCurrentItem();

	}
}

function setGalleryThumbsCurrentItem(){
	$('#main-top-content-gallery-thumbs-thumb-container div img').removeClass('current-slide');
	$('#main-top-content-gallery-thumbs-thumb-container div').each(function(index, domEle){
		if(parseInt(index)+1 == parseInt(workDetailsSlideCounting)){
			$(this).find('img').addClass('current-slide');
		}
	});
	if(((parseInt(workDetailsSlideCounting) % 5) / 5) == 0.2 || parseInt(thumbWidth) * (workDetailsSlideCounting - 1) < $('#main-top-content-gallery-thumbs').scrollLeft()){
		$('#main-top-content-gallery-thumbs').animate({ scrollLeft : parseInt(thumbWidth) * (workDetailsSlideCounting - 1) });
	}

}

function loadWorkDetail(workId, f){

	$.getJSON('ajax/json_work_details.asp',
				{
					id_work : parseInt(workId)
				},
				function(json){
					$('#main-top-content-gallery-thumbs-thumb-container div').remove();
					$('#work-details-slides-container .work-detail-slide').remove();
					$('#work-details-footer p').remove();
					$('#full_screen_gallery_thumbs td').remove();
					$('#full_screen_gallery_slides td').remove();
					counterItem = 1;
					workDetailsSlideCounting = 1;

					$.each(json, function(index, array){
							if(index == 0){
								$('#box-work-details .main-top-content-title').text(array['titulo']);
								$('#box-work-details .main-top-content-client').text(array['cliente']);
								var classif = "";
								for(var i = 0 ; i < array['classificacao'].split(',').length ; i++){
									if(i == 0) classif += array['classificacao'].split(',')[i];
									else classif += " + " + array['classificacao'].split(',')[i];
								}
								$('#box-work-details .main-top-content-section').text(classif);
								$('#port_det').attr('href',encodeURI('http://www.facebook.com/sharer.php?u=http://www.3mw.com.br/work.asp?id_work=')+(array['id_projeto'])+'&t='+encodeURI(array['titulo']+" | Work | 3mw.com.br")+'&i='+encodeURI('http://www.3mw.com.br/images/client_images/'+array['arquivo']));
								$('#port_det_like').attr('src','http://www.facebook.com/plugins/like.php?href='+encodeURI('http://www.3mw.com.br/work.asp?id_work=')+(array['id_projeto'])+'&layout=button_count&show_faces=true&width=40&action=like&font&colorscheme=light&height=20');
								$('#port_det_like').css('height',22);
							}
							switch(index){
								case 0:
									classToAppend = "first-thumb";
									break;
								case json.length - 1:
									classToAppend = "last-thumb";
									break;
								default:
									classToAppend = "";
							}

							//Making up the main gallery
							$('#main-top-content-gallery-thumbs-thumb-container').append('<div class="' + classToAppend + ' main-top-content-gallery-thumb-item-' + index + '"><img src="images/client_images/' + array['arquivo'] + '" alt="' + array['descricao'] + '" /></div>');
//							$('#work-details-slides-container').append('<div class="work-detail-slide"><img src="images/client_images/' + array['arquivo'] + '" alt="' + array['descricao'] + '" /></div>');
							$('#work-details-slides-container').append('<div class="work-detail-slide"><img src="images/client_images/' + array['arquivo'] + '" alt="' + array['descricao'] + '" /></div>');
							$('#work-details-footer').append('<p>' + array['descricao'] + '</p>');


							//Making up the full screen gallery
							$('#full_screen_gallery_thumbs table tr').append('<td class="full-screen-gallery-thumb-item-' + index + '"><img src="images/client_images/' + array['arquivo'] + '" alt="' + array['arquivo'] + '" /></td>');
							$('#full_screen_gallery_slides table tr').append('<td><p>' + array['descricao'] + '</p><img src="images/client_images/' + array['arquivo'] + '" alt="' + array['arquivo'] + '" /></td>');

					});

					setupWorkDetails();
					setCurrentWorkDetailSlideFooterLabel();

					if(typeof(f) == 'function') f();
				}
	);

}

function autoChangeBanner(){
	
	//console.log("TAMANHO: ")
	if(!acting){
		//$('.centralBannerContentBlock').hide();
		if(current_slide == slide_counting) {
			processIndicatorPress(null, 1);
		} else {
			processIndicatorPress(null, parseInt(current_slide)+1);
		}
	}
}

function setCurrentWorkDetailSlideFooterLabel(){
	var i = 1;
	$('#work-details-footer p').hide();
	$('#work-details-footer p').each(function(){
		if(i == workDetailsSlideCounting) $(this).fadeIn();

		i++;
	});
}


function scrollThumbsToLeft(){
	clearTimeout(scrollThumbs);
	if(mOver){
		var sLeft = $('#main-top-content-gallery-thumbs').scrollLeft();
		var sum = sLeft + 1;
		$('#main-top-content-gallery-thumbs').scrollLeft(sum);
		scrollThumbs = setTimeout("scrollThumbsToLeft()",10);
	}
}

function scrollThumbsToRight(){
	clearTimeout(scrollThumbs);
	if(mOver){
		var sLeft = $('#main-top-content-gallery-thumbs').scrollLeft();
		var subtraction = sLeft - 1;
		$('#main-top-content-gallery-thumbs').scrollLeft(subtraction);
		scrollThumbs = setTimeout("scrollThumbsToRight()",10);
	}
}


function jumpThumbToLeft(){
	var sLeft = $('#main-top-content-gallery-thumbs').scrollLeft();
	var sum = sLeft + parseInt(thumbWidth) + parseInt(thumbMargin);
	$('#main-top-content-gallery-thumbs').animate({ scrollLeft : sum });
}

function jumpThumbToRight(){
	var sLeft = $('#main-top-content-gallery-thumbs').scrollLeft();
	var sum = sLeft - (parseInt(thumbWidth) + parseInt(thumbMargin));
	$('#main-top-content-gallery-thumbs').animate({ scrollLeft : sum });
}

function processIndicatorPress(event, indicator){

	if(event != null)
		event.preventDefault();

	setCurrentSlide(parseInt(indicator));
}

function setCurrentSlide(i){
//	if(!acting){

//console.log("TAMANHO: " + timeouts.lengh);

		acting = true;
 
		$('.mascote').css('z-index',0);
 
		//Avançando
		if(i > current_slide){
			$('#content' + current_slide).fadeOut(500, function(){
				$('#mascoteImg' + current_slide).css('z-index',500);
				$('#content' + current_slide).css('z-index',501);

				$('#mascoteImg' + i).css('z-index',498);
				$('#content' + i).css('z-index',499);
				$('#mascoteImg' + i).css('width', 580);
				$('#mascoteImg' + i).show();

				$('.bottom_banner_current_item').animate({
						'margin-left' : (i-1) * 144
				});

				$('#mascoteImg' + current_slide).animate(
					{ 'width' : 0 },
					1000,
					function(){
						$('#content' + i).fadeIn(500);
						$('#mascoteImg' + i).css('z-index',500);
						$('#content' + i).css('z-index',501);
						acting = false;
						current_slide = i;
						clearTimeout(fncTmOut);
						clearInterval(fncTmOut);
						fncTmOut = setTimeout("autoChangeBanner();",CENTRAL_BANNER_TRANSITION_TIME);
					}
				);
			});
		//Retornando
		} else{

			$('#content' + current_slide).fadeOut(500, function(){

				$('#mascoteImg' + i).css('z-index',500);
				$('#content' + i).css('z-index',501);
				$('#mascoteImg' + i).css('width', 0);
				$('#mascoteImg' + current_slide).css('z-index',498);
				$('#content' + current_slide).css('z-index',499);
				$('#mascoteImg' + i).show();

				$('.bottom_banner_current_item').animate({
						'margin-left' : (i-1) * 144
				});

				$('#mascoteImg' + i).animate(
					{ 'width' :  $('#mascoteImg' + current_slide).width()  },
					1000,
					function(){
						$('#mascoteImg' + current_slide).hide();
						$('#content' + i).fadeIn(500);
						acting = false;
						current_slide = i;
						clearTimeout(fncTmOut);
						clearInterval(fncTmOut);
						fncTmOut = setTimeout("autoChangeBanner();",CENTRAL_BANNER_TRANSITION_TIME);
					}
				);
			});
			
		}
//	}
}

function processKeyCommands(event){

	switch(event.keyCode){
		case 37:
			workDetailsSlidesDecreaseSLeft();
			break;
		case 39:
			workDetailsSlidesIncreaseSLeft();
			break;
	}
}

jQuery.fn.processTouch = function(settings){
	// DEFINE DEFAULT TOUCH SETTINGS
	settings = jQuery.extend({
		animate: true,
		sticky: false,
		dragx: true,
		dragy: true,
		rotate: false,
		resort: true,
		scale: false,
		maxLeft : 0,
		centerCurrentSlide: false,
		itemWidth : 0
	}, settings);
	
	// BUILD SETTINGS OBJECT
	var opts = [];
	opts = $.extend({}, $.fn.touch.defaults, settings);
	var o = $(this)[0];
	var _moved = false;
	var movementSum = 0;
	var _initialTime = 0;
	var _swipedToLeft = false;


	// ADD METHODS TO OBJECT
	this.each(function(){
		this.opts = opts;
		this.ontouchstart = function(e){
				event.preventDefault();

				_moved = false;
				initXTouch = e.targetTouches[0].pageX;
				_initialTime = new Date();
		};

		this.ontouchmove = function(e){
			$(e.changedTouches).each(function(){
				e.preventDefault();
				movementSum = Math.round(Math.abs(initXTouch - this.pageX));
				//var movementSum = 8;

				_moved = true;

				//Swiping to right (should decrement the scrolling)
				if(parseInt(this.pageX) > parseInt(initXTouch)){
					_swipedToLeft = false;
					$(o).scrollLeft($(o).scrollLeft() - movementSum);
				}
				//Swiping to left (should increment the scrolling)
				else {
					_swipedToLeft = true;
					$(o).scrollLeft($(o).scrollLeft() + movementSum);
				}
				initXTouch = this.pageX;
			});
		};
		this.ontouchend = function(e){

					var timeDifference = parseInt(new Date().getTime()) - parseInt(_initialTime.getTime());

					var _centerCurrentSlide = this.opts.centerCurrentSlide;
					var containerWidth = $(o).children('div, td').width();
					var _itemWidth = this.opts.itemWidth;
					var numSlides = containerWidth / parseInt(_itemWidth);

					$(e.changedTouches).each(function(){

							if(_moved){

									//Swiped
									if(timeDifference <= 500){
										if(_swipedToLeft){
											workDetailsSlidesIncreaseSLeft();
										}else{
											workDetailsSlidesDecreaseSLeft();
										}
									}else{
											if(_centerCurrentSlide){
												workDetailsSlideCounting = Math.round($(o).scrollLeft() / parseInt(_itemWidth));
												$(o).animate({ scrollLeft : _itemWidth * workDetailsSlideCounting}, function(){
													setGalleryThumbsCurrentItem();
												});
											}
									}
							}else{

									if($(o).attr('id') == 'full_screen_gallery_slides'){
										if($('.full_screen_info_bar').is(':visible'))
											hidefullScreenGalleryInfoBar();
										else
											showfullScreenGalleryInfoBar();
									}

							}

							_moved = false;

					});
		};
	});
};


jQuery.fn.showPopup = function(){
	var o = $(this)[0];
	var args = arguments[0];

	if(typeof(args) != 'undefined' && args != null){
		if(typeof(args.show) != 'undefined' && args.show != null){
			if(!args.show){
				$('#modal-popup-mask , #' + $(o).attr('id')).fadeOut();
				return;
			}
		}
	}

	if($('#modal-popup-mask').length == 0){
		$('body').prepend('<div id="modal-popup-mask"></div>');
	}
	$('#modal-popup-mask').width($(document).width());
	$('#modal-popup-mask').height($(window).width());

	var w = $(window).width();
	var h = $(window).height();
	var objTop = h/2 - $(o).height() / 2;
	var objLeft = w/2 - $(o).width() / 2;

	$(o).css('position','fixed');
	$(o).css('z-index',1000);
	$(o).css('top',objTop);
	$(o).css('left',objLeft);

	$('#modal-popup-mask , #' + $(o).attr('id')).fadeIn();
	$('#modal-popup-mask').height($(document).height());
 
	$('#modal-popup-mask').live(tapEvent, function(){
		$('#modal-popup-mask , #' + $(o).attr('id')).fadeOut();
	});

	$(document).keydown(function(event){
		switch(event.keyCode){
			case 27:
				$('#modal-popup-mask , #' + $(o).attr('id')).fadeOut();
				break;
		}
	});

};

function trim(str){
	return str.replace(/[^\s]/,"");
};

function load_share(){
$.getScript('http://static.ak.fbcdn.net/connect.php/js/FB.Share', function() {
});
}

