$(function(){

	// validate
	$(".validate").each(function(){
		$(this).validate();
	});
		   
		   
	/*$("#quoteRequestCaptcha").submit(){
		$(this).validate({errorElement: 'span', function(){
			var img = $('#loading');
			$.ajax({
			  type:"POST",
			  url: 'send_quote.php',
			  data: $(this).formSerialize(),
			  success:function(data){
				if(data == "1"){
					alert(data);
				}else{
					alert(data);
				}
			  },
			  complete: function(){
				img.hide();
			  },
			  beforeSend: function(){
				img.show();
			  }
			});
			return false;
		}});
	});*/
	//Preload Images
	$(window).bind('load', function() {
		var preload = new Array();
		$(".hover").each(function() {
			s = $(this).attr("src").replace(/\.(.+)$/i, "_f2.$1");
			preload.push(s)
		});
		var img = document.createElement('img');
		$(img).bind('load', function() {
			if(preload[0]) {
				this.src = preload.shift();
			}
		}).trigger('load');
	});
	
	//Rollover Images
	$(".hover").hover(function(){
		if($(this).is("img")){
			var temp = $(this).attr("src").split(".");
			$(this).attr("src", temp[0]+"_f2."+temp[1]);
		}
	},function(){
		if($(this).is("img")){
		  $(this).attr("src", $(this).attr("src").replace("_f2", ""));
		}
	});
	
	$.ajax({
		type: "GET",
		url: "xml/portfolio.xml",
		dataType: "xml",
		success: function(xmlData)
		{
			folioXML(xmlData);
		}
	});
	
	$.ajax({
		type: "GET",
		url: "xml/testimonials.xml",
		dataType: "xml",
		success: function(xmlData)
		{
			testimonialsXML(xmlData);
		}
	});
	
	//Load Blog RSS
	$.ajax({
		type: "GET",
		url: "/news/feed/",
		dataType: "xml",
		success: function(xmlData)
		{
			postsXML(xmlData);
		}
	});
});

//Featured Portfolio XML
itemsPerPage3 = 5;
var blogPosts = 1
curPos3 = blogPosts;

function postsXML(xmlDataSet)
{

	resultSetLength = $("item",xmlDataSet).length;

	if (curPos3 + itemsPerPage3 > resultSetLength){
		showingThrough = resultSetLength;
	}else{
		showingThrough = parseInt(curPos3 + itemsPerPage3);
	}
	
	strToAppend3 = "";
	
	$("item:lt(" + parseInt(curPos3 + itemsPerPage3) + ")",xmlDataSet).filter(":gt(" + parseInt(curPos3 - 1) + ")").each(function(i) {
		strToAppend3 += "<li><big><a href='" + $("link:eq(" + parseInt(curPos3 + i) + ")",xmlDataSet).text() + "' target='_blank'>" + $("title:eq(" + parseInt(curPos3 + i) + ")",xmlDataSet).text() + "</a></big><p>"+ $("description:eq(" + parseInt(curPos3 + i) + ")",xmlDataSet).text() +"</p>";
	});

	$("#news_home").html(strToAppend3);
}


//Featured Portfolio XML
itemsPerPage = 2;
var folioImages=Math.floor(Math.random()*8)
curPos = folioImages;

function folioXML(xmlDataSet)
{

	resultSetLength = $("pic",xmlDataSet).length;
	strToAppend = "<p>";

	if (curPos + itemsPerPage > resultSetLength){
		showingThrough = resultSetLength;
	}else{
		showingThrough = parseInt(curPos + itemsPerPage);
	}

	strToAppend += "</p>";
	
	$("pic:lt(" + parseInt(curPos + itemsPerPage) + ")",xmlDataSet).filter(":gt(" + parseInt(curPos - 1) + ")").each(function(i) {
		strToAppend += "<li><a href='" + $("link:eq(" + parseInt(curPos + i) + ")",xmlDataSet).text() + "' target='_blank'> <img src='" + $("img:eq(" + parseInt(curPos + i) + ")",xmlDataSet).text() +"'/> " + $("titulo:eq(" + parseInt(curPos + i) + ")",xmlDataSet).text() + "</a>";
	});

	$("#featuredFolioV ul").html(strToAppend);
	$("#featuredFolioH ul").html(strToAppend);
}

//Testimonials Left Random
itemsPerPage2 = 1;
var testimonialText=Math.floor(Math.random()*11)
curPos2 = testimonialText;

function testimonialsXML(xmlDataSet2)
{
	resultSetLength2 = $("testimonial",xmlDataSet2).length;
	//strToAppend2 = "&nbsp;";

	if (curPos2 + itemsPerPage2 > resultSetLength2){		
		showingThrough2 = resultSetLength2;
	}else{
		showingThrough2 = parseInt(curPos2 + itemsPerPage2);
	}

	//strToAppend2 += "</p>";
	
	$("testimonial:lt(" + parseInt(curPos2 + itemsPerPage2) + ")",xmlDataSet2).filter(":gt(" + parseInt(curPos2 - 1) + ")").each(function(i) {
		 strToAppend2  = "<img src='" + $("image:eq(" + parseInt(curPos2 + i) + ")",xmlDataSet2).text() + "'/>";
		 strToAppend2 += "<p>" + $("text:eq(" + parseInt(curPos2 + i) + ")",xmlDataSet2).text() + "</p>";
		 strToAppend2 += "<p class='signature'>" + $("signature:eq(" + parseInt(curPos2 + i) + ")",xmlDataSet2).text() + "</p>";
		 strToAppend2 += "<p class='signature'><a href='" + $("link:eq(" + parseInt(curPos2 + i) + ")",xmlDataSet2).text() + "' target='_blank'>" + $("client:eq(" + parseInt(curPos2 + i) + ")",xmlDataSet2).text() + "</a></p>";
	});

	$("#leftText div").html(strToAppend2);
}

//Bookmark Website
function bookmark(){
    var title="DBurns Design"
    var url="http://www.dburnsdesign.com"

    if (window.sidebar) window.sidebar.addPanel(title, url,"");

    else if( window.opera && window.print )  {
	document.write("Press Ctrl+D and Bookmark our Site!")
    var mbm = document.createElement('a');
    mbm.setAttribute('rel','sidebar');
    mbm.setAttribute('href',url);
    mbm.setAttribute('title',title);
    mbm.click();
    }
    else if( document.all ) window.external.AddFavorite( url, title);
}

//Popup
function popup(u, w, h, n, s, r, ext){
	var janela;
	var topo 	= (screen.availHeight - h) / 2;
	var esq 	= (screen.availWidth - w) / 2;
	var scrl 	= (s) ? 'yes' : 'no';
	var red 	= (r) ? 'yes' : 'no';
	janela = window.open (u, n, 'width=' + w + ', height=' + h + ', top=' + topo + ', left=' + esq + ', scrollbars=' + scrl + ', resizable=' +  red+ ', statusbar=no');
	janela.focus();
}
