$(document).ready(function() {
	/*
     * Pega todas as imagens com classe getthelook-container-img e transforma em getthelooks
     */
	if (jQuery.browser.safari && document.readyState != "complete"){
		//console.info('ready...');
		setTimeout( arguments.callee, 100 );
		return;
	} 			

	setTimeout('loadGTL()', 500);
	setTimeout('loadContainer()', 2000);

})

/*
 * Carrega informações do controlador
 */
function load(gtl) {
    
//    gtl.find('img[class^=hot]').parent('div').bind('mouseenter mouseleave', function() {
//        id = $(this).find('img').attr('class').replace(/[^0-9]/g, "");
//        $('.form-hotspot-'+id).toggle('fast');
//    })

}

function loadGTL() {
	$('.getthelook-container-img').each(function() {
		
		//if(width == 0 || width == undefined || height == 0 || height == undefined) {
		var img = $(this);
		var src = img.attr('src');
		var id = img.attr('id');
		
		src = src.replace('http://www.casadevalentina.com.br/', '').replace('/_midias', '_midias');
			
		$.ajax({
			url: "http://www.casadevalentina.com.br/imgsize.php",
			data: 'src=' + src,
			dataType : 'json',
			context: $(this),
			success: function(msg){
					
				//var srcimg = new Image();
				//srcimg.src = src;
				//var width = srcimg.width;
				//var height = srcimg.height;
					
				var width = msg.w;
				var height = msg.h;
					
				if(img.parent('a').size() > 0) {
					img.parent('a').after('<div id="div_'+ id +'" class="getthelook-container" style="width: '+ width +'px; height: '+ height +'px; background-image: url('+ src +'); background-repeat: no-repeat;"></div>')
					img.parent('a').remove();
				} else {
					img.after('<div id="div_'+ id +'" class="getthelook-container" style="position: relative; width: '+ width +'px; height: '+ height +'px; background-image: url('+ src +'); background-repeat: no-repeat;"></div>')
					img.remove();
				}
					
			},
			error: function() {
			}
		});

			
			
	//		} else {
	//		
	//			if(img.parent('a').size() == 1) {
	//				img.parent('a').after('<div id="div_'+ id +'" class="getthelook-container" style="width: '+ width +'px; height: '+ height +'px; background-image: url('+ src +'); background-repeat: no-repeat;"></div>')
	//				img.parent('a').remove();
	//			} else {
	//				img.after('<div id="div_'+ id +'" class="getthelook-container" style="position: relative; width: '+ width +'px; height: '+ height +'px; background-image: url('+ src +'); background-repeat: no-repeat;"></div>')
	//				img.remove();
	//			}
	//		}
		
	});
	
	
}

function loadContainer() {
	/*
     * Cria get the look ao clicar no target
     */
	$('.getthelook-container').each(function() {
		getthelook = $(this);
		getthelook.unbind();
		cd_midia = $(this).attr('id').replace(/[^0-9]/g, "");
		$(this).load('/index.php?id=/_templates/getthelook.loadfront.php&cd_midia='+cd_midia, function() {
			load($(this));
			$("a[rel^='prettyPhoto']").prettyPhoto({
				theme:'pp_default',
				deeplinking: false,
				social_tools: false,
				allow_resize: false
			});
		});
	});	
}

