head.ready(function(){
    $(document).ready(function(){

    	$(".tweet").tweet({
    		username: "modices",
    		join_text: "auto",
    		avatar_size: 0,
    		count: 4,
    		auto_join_text_default: "", 
    		auto_join_text_ed: "",
    		auto_join_text_ing: "",
    		auto_join_text_reply: "",
    		auto_join_text_url: "",
    		loading_text: "carregando tweets..."
    	});

    	//Detecta se é mac ou iphone e remove os objetos em flash ativando a imagem gif.
    	if($.client.os == "iPhone/iPod"){
    		$('object').each(function (){$(this).remove();});
    		$('.advert').find('.no-flash').css('display', 'block');
    	}

    	$('form').submit(function(){
    		document.location = 'http://modices.com.br/?s=' + $(this).find(':input[type=text]').val();
    		return false;
    	});

    	// Galeria de imagem do das ruas.
    	$('.das-ruas').each(function(){
    		var curLink = $(this).find('.post-title > a').attr('href');
    		$(this).children('.post-content').each(function(){
    			var img = new Array();
    			var position = 0;
    			var total = 0;
    			$(this).children('div').each(function(){
    				img[img.length] = $(this).html();
    				total++;
    			});
    			$(this).html('<div class="gallery-position" style="position:absolute;right:0;top:0;">' + (position + 1) + ' / ' + total + '</div>');
    			$(this).append('<div class="gallery-cur-image" style="overflow:auto;text-align:center;"><img src="' + img[0] + '" /></div>');
    			$(this).append('<div class="gallery-nav text-center" style="cursor:pointer;font-family:\'JollyRegular\';font-size:18px;"><span class="gallery-prev" style="color:#763e8f">&laquo; anterior</span> | <span class="gallery-next" style="color:#763e8f">próxima &raquo;</span></div>');
    			$(this).append('<div class="gallery-buffer" style="display:none;"><img src="' + img[1] + '" /></div><br />');
    			$(this).append('<div><a href="' + curLink + '">Clique aqui</a> para ver todas as fotos no tamanho original.</a></div>');

    			var ref = $(this);

    			$(this).find('.gallery-prev').fadeTo(1, 0.3);

    			$(this).find('.gallery-prev').click(function(){
    				if(position != 0){
    					position--;
    					ref.find('.gallery-cur-image > img').attr('src', img[position]);
    					ref.find('.gallery-buffer > img').attr('src', img[position + 1]);
    					ref.find('.gallery-next').fadeTo(1, 1);
    				}
    				if(position == 0){
    					$(this).fadeTo(1, 0.3);
    				}
    				ref.find('.gallery-position').html((position + 1) + ' / ' + total);
    			});

    			$(this).find('.gallery-next').click(function(){
    				if(position < total - 1){
    					position++;
    					ref.find('.gallery-cur-image > img').attr('src', img[position]);
    					ref.find('.gallery-buffer > img').attr('src', img[position + 1]);
    					ref.find('.gallery-prev').fadeTo(1, 1);
    				}
    				if(position == total - 1){
    					$(this).fadeTo(1, 0.3);
    				}
    				ref.find('.gallery-position').html((position + 1) + ' / ' + total);
    			});
    		});
    	});

    	$('.post-content').each(function(){
    		var ref = $(this);
    		$(this).find('.more-link').click(function(){
    			ref.find('.more-content').css('display','block');
    			ref.find('.more-content-curtain').fadeOut(2000);
    			$(this).remove();
    			return false;
    		});
    	});

    	// Open links that are not from the site.
    	$('a[href]').each(function(){
    		if($(this).attr('href').indexOf('modices.com.br') == -1){
    			$(this).attr('target', '_blank');
    		}
    	});

    });

    var curImageGalleryPos;
    function changeGalleryPic(images, command){
    	if(curImageGalleryPos == 0 && command == -1) return;
    	if(curImageGalleryPos == (images.length - 1) && command == 1) return;
    	$.ajax({
    		url: images[curImageGalleryPos + (1 * command)],
    		dataType: 'html',
    		success: function(data){
    			$("#wrapper_image_container").html(data);
    		}
    	});
    	curImageGalleryPos = curImageGalleryPos + (1 * command);
    	$(document).scrollTop(0);
    }
});
