$(function(){
	
	// load the homepage billboard data
	if (document.getElementById('slideshow')) {
		
		if (typeof console!='undefined') {
			console.log('Initializing billboard');
		}
		// get main billboard elements
		$.ajax({
			url: '/html/wp-content/themes/invodo.com/billboard-data/billboard.xml',
			success: function(data){
				var pagerText = [];
				var pagerIcon = [];
				var $ss = $('#slideshow');
				var slideCount = 0;
				$(data).find("slide").each(function(i,el){
					slideCount++;
					$ss.append($(el).find('content').text());
					pagerText[i] = $(el).find('pagerText').text();
					pagerIcon[i] = $(el).find('pagerIcon').text();
				});
				console.log("slide count:"+slideCount);
				$ss.cycle({
					fx:'scrollLeft',
					timeout: 7000,
					pause: 1,
					nowrap: 1,
					pagerAnchorBuilder:function(i,el) {
						return '<li><span><a style="background-image:url(\''+pagerIcon[i]+'\');" href="#">'+pagerText[i]+'</a></span></li>';
					},
					pager:'#slideshowControls',
					requeueOnImageNotLoaded: true, 
				    requeueTimeout:  250
				});
			},
			dataType: 'xml'
		});
		
	}
	
	// Setup dropdown menus
	$("ul.sf-menu").superfish();
	
	// Setup contact sales form
	$("#contactSalesDialog").dialog({
									autoOpen: false,
									width: 607,
									height: 410,
									dialogClass: "contactSalesDialog",
									title: "<h2>Contact Sales</h2>",
									resizable: false,
									modal: true
									});
	$(".contactSalesButton").click(function(event){
		event.preventDefault();
		$("#contactSalesDialog").dialog('open');
		$("#firstName").focus();
	});
	
	// Setup contact form validation and submission
	var myForm = jQuery('#contactForm').validate({
		rules: {
			firstName: "required",
			lastName: "required",
			email: {
				required: true,
				email: true
			},
			telephone: "required",
			company: "required"
		},
		messages: {
			firstName: "",
			lastName: "",
			email: {
				required: "",
				email: ""
			},
			telephone: "",
			company: "",
			message: ""
		},
		highlight: function(element, errorClass) {
			jQuery(element).addClass(errorClass);
			if (jQuery(element).attr("type")==="checkbox") {
				jQuery(element).parent().parent().find("label[for=" + element.id + "]").removeClass("valid");
				jQuery(element).parent().parent().find("label[for=" + element.id + "]").addClass(errorClass);
			} else {
				jQuery(element).parent().find("img").remove();
				jQuery(element).parent().append("<img class='registerIcon' src='/html/wp-content/themes/invodo.com/images/exclamation.png' />");
			}
		},
		unhighlight: function(element, errorClass) {
			jQuery(element).removeClass(errorClass);
			jQuery(element).addClass("valid");
			if (jQuery(element).attr("type") === "checkbox") {
				jQuery(element).parent().parent().find("label[for=" + element.id + "]").removeClass(errorClass);
				jQuery(element).parent().parent().find("label[for=" + element.id + "]").addClass("valid");
			} else {
				jQuery(element).parent().find("img").remove();
				jQuery(element).parent().append("<img class='registerIcon' src='/html/wp-content/themes/invodo.com/images/check_sml.png' />");
			}
		},
		submitHandler: function(form) {
			jQuery(form).ajaxSubmit({
				url: "/html/wp-content/themes/invodo.com/web2lead-nospam.php",
				beforeSubmit: function(aFormData,form,opts) {
					$('#invodoLoader').fadeIn();
					$(form).find('input,textarea').attr('disabled','disabled');
				},
				success: function(sResponse,sStatus) {
					$("#sendAnEmailWrapper")
						.find("form")
							.animate({opacity:0})
							.css("display","none")
						.end()
						.append("<div class='contactFormResponse'><p>"+sResponse+"</p></div>")
						.animate({opacity:1})
						.animate({opacity:1},4000,function(){
							$("#contactSalesDialog").dialog('close');
							setTimeout(function(){
								$("#sendAnEmailWrapper").find('input,textarea').removeAttr('disabled');
								myForm.resetForm();
								$("#sendAnEmailWrapper").find("img").remove();
								$(".contactFormResponse").remove();
								$("#sendAnEmailWrapper>form").css("display","inline").animate({opacity:1});
							},1000);
						});
				}
			});
		}
	});
	
});

function ovp(podURL) {
	podID = podURL.split("?")[0].split("/")[podURL.split("?")[0].split("/").length-1];
	Invodo.showViewer(podID);
}