$(function(){
    $('.print').click(function(e){
        window.print()
        e.preventDefault()
    })
	$('#highlight-banner').tinycarousel({interval: true, intervaltime: 5000})
	$('#gallery').tinycarousel({display: 4})
	$('#gallery-full').tinycarousel({
		display: 1,
		callback: function(obj, n){
			$('#gallery-current-photo').html(n + 1)
		}
	})
	$('#gallery-thumbs').tinycarousel({display: 5})
	$('#gallery-thumbs a').click(function(){
		$('#gallery-full').tinycarousel_move($(this).attr('rel'))
		return false
	})
	$('.zoom').click(function(e){
	    var zoom = Number($('#post-content').css('zoom'))
	    zoom = (this.innerText == "[+]") ? zoom + 0.2 : zoom - 0.2
	    if(zoom < 1){
	        zoom = 1
	    }
	    if(zoom > 1.6){
	        zoom = 1.6
	    }
	    $('#post-content').css('zoom', zoom)
	    e.preventDefault()
	})
	$('#post-content img').each(function(i, o){
	    var desc = $(o).attr('alt')
	    if(desc != null){
	        var jO = $(o)
	        var src = o.src
	        var title = o.title
	        if(typeof(title) == "undefined"){
	            title = ""
	        }
	        var width = jO.width()
	        if(width > 600){
    	        width = 600
    	    }
	        var parent = jO.parent()
	        $('<div class="legenda" style="width:'+width+'px"><img src="'+src+'" title="'+title+'"/><label>'+desc+'</label></div>').insertBefore(o)
    	    jO.remove()
	        $('#post-content').append('<br class="clear"/>')
	    }
	})
	$('#post-content .legenda img').load(function(e){
	    var jO = $(this)
	    var legenda = jO.parent()
	    var largura = jO.width()
	    if(largura > 600){
	        largura = 600
	    }
	    legenda.css('width', (largura) + "px")
	})
	var pop = $('#pop-ad')
	if(pop.length > 0){
	    setTimeout(function(){
	        pop.css('top', ($('#menu').offset().top + 70) + "px")
	        pop.css('left', ($('#menu .container:first').offset().left + 100) + "px")
    	    pop.show()
    	    pop.find('.close').click(function(e){
    	        e.preventDefault()
    	        pop.remove()
    	    })
	    }, 1500)
	}
	$('.fb-share').click(function(e){
	    var caption = encodeURIComponent("www.propmark.com.br")
	    var desc = $('#post p:first').text()
	    var href = window.location.href
	    var titulo = $('#post h1:first').text()
	    var CLOSE_POP = "http://www.propmark.com.br/close.html"
	    var FACEBOOK_ID = "131666626956117"
	    var url = "http://www.facebook.com/dialog/feed?display=popup&app_id="+FACEBOOK_ID+"&redirect_uri=" + encodeURIComponent(CLOSE_POP) + "&link=" + href +"&caption="+ caption +"&description="+ desc +"&name=" + titulo
	    var img = $('#post img:first')
	    if(img.length > 0){
	        url += "&picture=" + encodeURIComponent(img.attr('src'))
	    } else {
	        url += "&picture=" + encodeURIComponent("http://www.propmark.com.br/images/propmark.jpg")
	    }
	    var compartilhar = window.open(url, null, "width=700,height=300")
	    e.preventDefault()
	})
})

function showMyVideos(data){
	var feed = data.feed;
	var entries = feed.entry || [];
	if(entries.length == 0){
	    return;
	}
	var entry = entries[0];
	var link = entry.link[entry.link.length - 1].href
	link = link.replace(/.*\uploads\/(.*?)/, '$1')
    $('#player-propmark-tv').html('<iframe id="canal-youtube" width="299" height="225" src="http://www.youtube.com/embed/'+ link +'?showinfo=0&rel=0" frameborder="0" allowfullscreen></iframe>')
    var ul = $('<ul class="player"></ul>')
	for (var i = 1; i < entries.length; i++) {
	    var entry = entries[i];
		var link = entry.link[entry.link.length - 1].href
		link = link.replace(/.*\uploads\/(.*?)/, '$1')
		var title = entry.title.$t.replace(/"/g, "'")
		var thumbs = entry.media$group.media$thumbnail
		var thumb = thumbs[thumbs.length - 1].url
	    $('<li><a href="http://www.youtube.com/embed/'+link+'?showinfo=0&rel=0" title="'+ title +'" target="_blank"><img src="'+thumb+'" /></a></li>').appendTo(ul)
	}
	ul.appendTo($('#player-propmark-tv'))
	ul.find('a').click(function(e){
	    $('#canal-youtube').attr('src', this.href)
	    e.preventDefault()
	})
}

function top_read(results){
    if(typeof(results) == "undefined"){return;}
    var ul = $("<ul></ul>")
    for(var i = 0; i < results.length; i++){
        ul.append("<li class=\"top-"+ (i + 1) +"\"><a href=\""+ results[i].url +"\">"+results[i].titulo+"</a></li>")
    }
    var top_conteudo = $('#top-content')
    top_conteudo.html("")
    ul.appendTo(top_conteudo)
}
function trend_topics(queries){
    var container = document.getElementById("trends-topics")
    container.innerHTML = ""
    var numberOfPopularQueriesToShow = queries.popularQueries.length;
    if ((typeof queries.maxNumberOfPopularQueries == "number") && (queries.maxNumberOfPopularQueries < numberOfPopularQueriesToShow)) {
        numberOfPopularQueriesToShow = queries.maxNumberOfPopularQueries;
    }
    for (var i = 0; i < numberOfPopularQueriesToShow; i++){
        var a = document.createElement("a");
        a.setAttribute("href", "http://www.propmark.com.br/busca/?q=" + queries.popularQueries[i].query);
        a.appendChild(document.createTextNode(queries.popularQueries[i].query));
        container.appendChild(a);
        container.appendChild(document.createTextNode(" "));
    }
    if(numberOfPopularQueriesToShow == 0){
        $('#trends-topics, .trends-topics').hide()
    }
}
