$(document).ready(function(){
	
	//template code
	
	var code_html = '<div id="lbox_btcode"><div class="left"><p>For detailed instructions please click <a target="_blank" href="http://www.blogcrowds.com/how-to-blog/3-blogger-templates-download-instructions">here</a></p><p>For template help please visit <a href="http://www.blogcrowds.com/forum/">the forum</a>, <a href="http://www.blogcrowds.com/#">the FAQ</a>, or send us an <a href="mailto:&#104;&#101;&#108;&#115;&#64;&#98;&#108;&#111;&#103;&#99;&#114;&#111;&#119;&#100;&#115;&#46;&#99;&#111;&#109;">Email</a>.</p></div><div class="right"></div></div>';
	
	$('.bt_demo').click(function(){
		var data = {
			action: 		'view',
			template_id: 	$(this).attr('id')
		}
		$.post('http://www.blogcrowds.com/includes/aj_btemplate.php', data);
	});
	
	$('#lbox_content').append(code_html);
	$('#lbox_ad').css(hide_css);
	
	$('.bt_xml_download').click(function(){
		show_code();
	});
	
	$('.bt_package_download').click(function(){
		if ($('.bt_package_download').attr('href') == 'javascript:void(0)'){
			show_code();
		}
	});
	
	$('#lbox_close').click(function(){
		$('#lbox_overlay').fadeOut('slow');
		$('#lbox_container').fadeOut('fast');
		$('#lbox_ad').css(hide_css);
	});

	
	// rating
	$('.rating').each(function(){
		$(this).append('<div class="overlay"></div>');
		$('.overlay',this).css('width', $(this).attr('title') * 16 + 'px');
		
		$(this).mousemove(function(e){
			if ($(this).attr('title') != 'rated'){
				w = 'width:' + Math.ceil((e.pageX - $(this).offset().left)/16) * 16 + 'px';
				$('.overlay',this).attr('style',w);
			}
		});
		
		$(this).mouseleave(function(){
			if ($(this).attr('title') != 'rated'){
				w = 16 * $(this).attr('title') + 'px';
				$('.overlay',this).animate( {width: w},500 );
			}
		});
		
		$(this).click(function(e){
			if ($(this).attr('title') != 'rated'){		
				var data = {
					action: 		'rate',
					template_id: 	$(this).attr('id'),
					rating: 		Math.ceil((e.pageX - $(this).offset().left)/16)
				}
				
				$.post('http://www.blogcrowds.com/includes/aj_btemplate.php', data);
			}
			$(this).attr('title','rated');
		});
	}); 
	
});

var hide_css = {
	display: 			'none',
	visibility: 		'hidden' 
};

var show_css = {
	display: 			'block',
	visibility: 		'visible'
}

function show_code(){
	var height = $('body').css('height');
	
	var left = ($(window).width() - 856) / 2;
	var top = $(window).scrollTop() + 65;
	var lbox_container_css = {
		width: 	800,
		left: 	left,
		top: 	top
	};
	
	var lbox_ad_css = {
		display: 	'block',
		visibility: 'visible',
		position: 	'absolute',
		left: 		left + 40,
		top: 		top + 170
	}
	
	$('#lbox_overlay').css('height', height);
	$('#lbox_overlay').fadeIn('slow');
	$('#lbox_container').css(lbox_container_css);
	$('#lbox_login').css(hide_css);
	$('#lbox_btcode').css(show_css);
	
	var data = {
		action: 		"download", 
		template_id: 	$('.bt_xml_download').attr('id')
	}
	
	$.post('http://www.blogcrowds.com/includes/aj_btemplate.php', data, function(data_out){
		if (data_out != ''){
			$('#lbox_btcode .right').html('<p id="code"><a href="javascript:selectAll(\'template.code\')">Click Here [Then copy &amp; paste to Edit HTML]</a></p><form name="template"><textarea name="code">' + data_out + '</textarea></form>');
		}
	});
	$('#lbox_container').fadeIn('slow');
	$('#lbox_ad').css(lbox_ad_css);
	$('#lbox_ad').css('z-index', 100);
}

function selectAll(theField) {
	var tempval=eval("document."+theField)
	tempval.focus()
	tempval.select()
}