

			

			



$(document).ready(function() {
	
	/*$('#flickr-bt').one('click', function(e) {
		$('.jcarousel-next-horizontal').click();
		//$('.jcarousel-prev-horizontal').click();
	});*/
	

	$('#accord').accordion({
		//active: false,
		collapsible: true,
		autoHeight: false,
		active: 2,
		/*
		change: function(event, ui) {
			console.log(event);
			console.log(ui);
			$('.jcarousel-next-horizontal').click();
		}
		*/
		navigation: true
		
	});
	
	setTimeout(function(){ 
		$('#accord').accordion({
			active: false
		});
	}, 2500 );
	
	
	
	mycarousel_itemLoadCallback = function(carousel, state) {
	    if (carousel.prevFirst != null) {
	        // Remove the last visible items to keep the list small
	        for (var i = carousel.prevFirst; i <= carousel.prevLast; i++) {
	            // jCarousel takes care not to remove visible items
	            carousel.remove(i);
	        }
	    }

	    var per_page = carousel.last - carousel.first + 1;
	    var currPage = 0;
	    var f,l;
	    var cr = carousel;

	    for (var i = carousel.first; i <= carousel.last; i++) {
	        var page = Math.ceil(i / per_page);

	        if (currPage != page) {
	            currPage = page;

	            f = ((page - 1) * per_page) + 1;
	            l = f + per_page - 1;

	            f = f < carousel.first ? carousel.first : f;
	            l = l > carousel.last ? carousel.last : l;

	            if (carousel.has(f, l)) {
	                continue;
	            }

	            mycarousel_makeRequest(carousel, f, l, per_page, page);
	            
	        }
	    }
	};
	
	mycarousel_makeRequest = function(carousel, first, last, per_page, page) {
	    // Lock carousel until request has been made
	    carousel.lock();

	    $.get(
	        'ajax/flickr',
	        {
	            'per_page': per_page,
	            'page': page
	        },
	        function(data) {
	            mycarousel_itemAddCallback(carousel, first, last, data, page);		            
	        },
	        'xml'
	    );
	    
	};

	mycarousel_itemAddCallback = function(carousel, first, last, data, page) {
	    // Unlock
	    carousel.unlock();

	    // Set size
	    carousel.size($('photos', data).attr('total'));

	    var photos = $('photo', data);
	    var per_page = carousel.last - carousel.first + 1;

	    for (var i = first; i <= last; i++) {
	        var pos = i - 1;
	        var idx = Math.round(((pos / per_page) - Math.floor(pos / per_page)) * per_page);

	        carousel.add(i, mycarousel_getItemHTML(photos.get(idx)));
	    }
	    
	    $("#mycarousel a").lightBox();
	    //$("a[rel^='prettyPhoto']").prettyPhoto({theme:'facebook'});
	};
	
	mycarousel_getItemHTML = function(photo) {
		var url = 'http://farm'+$(photo).attr('farm')+'.static.flickr.com/'+$(photo).attr('server')+'/'+$(photo).attr('id')+'_'+$(photo).attr('secret')+'_s.jpg';
	    return '<a rel="prettyPhoto" class="tip" href="http://farm'+$(photo).attr('farm')+'.static.flickr.com/'+$(photo).attr('server')+'/'+$(photo).attr('id')+'_'+$(photo).attr('secret')+'_b.jpg"><img src="' + url + '" border="0" width="100" height="100" alt="'+$(photo).attr('title')+'" /></a>';
	};
	
	$('#mycarousel').jcarousel({
		itemLoadCallback: mycarousel_itemLoadCallback
	});	
	
	
	/*$('#mycarousel').jcarousel({
        size: mycarousel_itemList.length,
        itemLoadCallback: {onBeforeAnimation: mycarousel_itemLoadCallback}
    });*/
	
	

	$('#twitter-bt').one('click', function(e) {
		$.ajax({
			url: 'ajax/twitter',
			dataType: 'html',
			type: 'get',
			
			beforeSend: function() {
				$('#lista-twitter').html('<li>Carregando...</li>');
			},
			complete: function() {
				
				
				//$('#mycarousel').jcarousel();
			},
			success: function(data) {
				$('#lista-twitter').html(data);
				$('.retweet').hide();
				$('#lista-twitter li').hover(
					function() {
						$('.retweet', this).show();
					},
					function() {
						$('.retweet', this).hide();
					}
				);
			},
			error: function(xhr, er) {
				alert('Error ' + xhr.status + ' - ' + xhr.statusText);
			}
		});
	});

	

	$('#picasa').jcarousel();
	
	//$('#mycarousel').jcarousel({});
	
	
	$("#picasa a").lightBox();
	// Garelias prettyPhoto
	//$("a[rel^='prettyPhoto']").prettyPhoto({theme:'facebook'});
	////////////////////////////////
	
	$('#thumbs-youtube').jcarousel({
        vertical: true,
        scroll: 2
    });
	
	// tipTip
	$('.tip').tipTip({delay: 50});
	////////////////////////////////
	
	$('#aba').hover(
		function() {
			$(this).css({right: -120});
			$('#aba #desc-design').show(500);
		},
		function() {
			$('#aba #desc-design').hide(500);
			$(this).css({right: -38});
		}
	);
	
	
	
	$('.btn-siga-pt, .btn-siga-en').parent('a').click(function() {
		//window.location = $(this).attr('href');
		window.open($(this).attr('href'));
		return false;
	});
	
	$('.btn-siga-pt, .btn-siga-en').css({opacity: 0.5});
	$('.btn-siga-pt, .btn-siga-en').hover(function() {
		$(this).css({opacity: 1});
	},
	function() {
		$(this).css({opacity: 0.5});
	});
	
	// Google maps
	loadMaps();	
	$('#gmapa').dialog({
		autoOpen: false,
		width: 460,
		minHeight: 400,
		modal: true,
		resizable: false,
		title: 'Onde estamos'
	});
	
	$('#abre-maps').click(function(e) {
		e.preventDefault();
		$('#gmapa').dialog('open');
	});
	////////////////////////////////
	
	$('#form-contato label').addClass('ui-state-default').css({background: 'none', border: 'none'});
	$('#Cancelar-label, #Enviar-label').remove();
	$('#form-contato input[type="text"], #form-contato textarea').addClass('ui-widget-content ui-corner-all');
	$('#form-contato input[type="submit"]').button({}).css('fontSize', 12);
	
	/*$('#form-contato').dialog({
		width: 450,
		minHeight: 300,
		modal: true
	});*/
	
	// Refresh
	$('#header #refresh').click(function(e) {
		e.preventDefault();
		$.ajax({
			url: baseUrl+'/ajax/refresh',
			dataType: 'json',
			type: 'post',
			
			beforeSend: function() {
			
			},
			complete: function() {
				
			},
			success: function(data) {	
				$('body').css({background: '#'+data['background']});
				$('#language, #language a').css({color: '#'+data['cor_language']});
				$('#header img').attr('src', baseUrl+'/default/uploads/visuais/'+data['arquivo']);
				$('#aba #avatar').attr('src', baseUrl+'/default/uploads/visuais/'+data['avatar']);
				$('#aba #desc-design div strong a').attr('href', data['siteurl']).text(data['visual']);
				if (data['twitter'] != '') {
					$('#aba #desc-design twitter-visual').html('<a href="http://twitter.com/'+data['twitter']+'" target="_blank">@'+data['twitter']+'</a>');
				}
				console.dir(data);				
			},
			error: function(xhr, er) {
				alert('Error ' + xhr.status + ' - ' + xhr.statusText);
			}
		});
	});
	
	
	
	// Form mail
	$('#form-contato').submit(function(e) {
		e.preventDefault();		
		validator = $('#form-contato').validate({
			rules: {
				nome: {
					required : true							
				},
				assunto: {
					required : true
				},
				cidade: {
					required : true
				},
				email: {
					required : true,
					email: true
				},
				mensagem: {
					required : true,
					minlength : 10
				}
			}
		});
		
		if($("#form-contato").valid()) {
			$.ajax({
				url: $('#form-contato').attr('action'),
				dataType: 'html',
				type: 'post',
				data: $('#form-contato').serialize(),
				
				beforeSend: function() {
				
				},
				complete: function() {
					
				},
				success: function(data) {					
					if (data == 'enviado') {
						$('#retorno').html('Email enviado com sucesso!').dialog({
							title: 'Sucesso',
							resizable: false,
							draggable: false,
							width: 200,
							buttons: {							
							Fechar: function() {
									$('*').dialog('close');											
								}
							}
						});
					}
					else {
						$('#retorno').html('Erro no envio, tente novamente!').dialog({
							title: 'Erro',
							resizable: false,
							draggable: false,
							width: 200,
							buttons: {
								Fechar: function() {
									$(this).dialog('close');											
								}
							}
						});
					}
				},
				error: function(xhr, er) {
					alert('Error ' + xhr.status + ' - ' + xhr.statusText);
				}
			});
		}
	});
	
	
	
	if (language == 'pt') {
		$('#form-contato').dialog({
			autoOpen: false,
			width: 450,
			minHeight: 300,
			modal: true,
			buttons: {
				Enviar: function() {
					$('#form-contato').submit();
				},
				Cancelar: function() {				
					$(this).dialog('close');
					
				}			
			}
		});
	}
	else {
		$('#form-contato').dialog({
			autoOpen: false,
			width: 450,
			minHeight: 300,
			modal: true,
			buttons: {
				Send: function() {
					$('#form-contato').submit();
				},
				Cancel: function() {				
					$(this).dialog('close');
					
				}			
			}
		});
	}
	
	$( "#form-contato" ).dialog({
		close: function(event, ui) {
		event.preventDefault();
			validator.resetForm();
			$('#form-contato input[type="text"], #form-contato textarea').val('');			
		}
	});
		
	$('#mail').click(function(e) {
		e.preventDefault();
		$('#form-contato').dialog('open');
	});
	////////////////////////////
	
	$('#designer').dialog({
		autoOpen: false,
		modal: true,
		height: 100,
		resizable: false
	});
	
	$('#abre-designer').click(function(e) {
		e.preventDefault();
		$('#designer').dialog('open');
	});		
});


