$(document).ready(function() {

	if ($(".hero .slideshow").size() > 0) {
		//	USE THIS INFO WHEN BUILDING NEW SLIDESHOW SCRIPT
		$(".hero .slideshow").nivoSlider({
			effect:'sliceDown',
			slices:3,
			animSpeed:100,
			pauseTime:12000,
			startSlide:0,
			directionNav:false,
			controlNav:true
		});
	}
	
	var APPSTORE =	{
						lightbox: $("#lightbox"),
						
						setupViewDemo:	function() {
											var arrLinkDemoWrappers = $(".wrapcq_LinkDemo"),
												intLinkDemoWrappers = arrLinkDemoWrappers.length,
												arrDemoLinks = $(arrLinkDemoWrappers).find(".linkdemo"),
												intDemoLinks = arrDemoLinks.length,
												i,
												thisDemoLink,
												thisDemoLinkId,
												thisDemo,
												strDemoLinkTargetSuffix = "-vid";
												
											for (i = 0; i < intDemoLinks; i++) {
												thisDemoLink = arrDemoLinks[i];
												$(thisDemoLink).bind("click",function(event) {
													event.preventDefault();
													var that = this;
													_gaq.push(['_trackEvent', 'View Demo', 'Clicked Demo: ' +$(that).attr("id")]);
													thisDemoLinkId = $(that).attr("id") + strDemoLinkTargetSuffix;
													thisDemo = $("#" + thisDemoLinkId);
													APPSTORE.viewDemo(thisDemo);
												});
											}
										},
						
						viewDemo:	function(thisDemo) {
										var lightbox = APPSTORE.lightbox,
											lightboxHeight = $(window.document).height(),
											lightboxWidth = $(window.document).width(),
											theDemo,
											theDemoLeftPos,
											theDemoWidth,
											lightboxCloseBtn = $('<a>Close</a>').attr("id","lightboxCloseBtn");
											
										//	prepare lightbox content (clone theDemo and set some css)
										theDemo = $(thisDemo).clone();
										$(theDemo).attr("id","lightboxContent");
										theDemoWidth = $(theDemo).width();
										theDemoLeftPos = (lightboxWidth / 2) - (theDemoWidth / 2);
										$(lightboxCloseBtn)
											.bind("click",function() {
												$(lightbox).addClass("hidden");
												$(theDemo).remove();
											});

										$(theDemo)
											.css({"left":theDemoLeftPos})
											.prepend(lightboxCloseBtn);
										
										
										//	show the lightbox
										$(lightbox).removeClass("hidden").append(theDemo);
									},
									
						setupViewInfo:	function() {
											var arrLinkInfoWrappers = $(".wrapcq_CTAMoreInfo"),
												intLinkInfoWrappers = arrLinkInfoWrappers.length,
												arrInfoLinks = $(arrLinkInfoWrappers).find("a"),
												intInfoLinks = arrInfoLinks.length,
												i,
												thisInfoLink,
												thisInfoLinkId,
												thisInfo,
												strInfoLinkTargetSuffix = "-desc";
												
											for (i = 0; i < intInfoLinks; i++) {
												thisInfoLink = arrInfoLinks[i];
												$(thisInfoLink).bind("click",function(event) {
													event.preventDefault();
													var that = this;
													_gaq.push(['_trackEvent', 'Read More', 'More Info for: ' +$(that).attr("id")]);
													thisInfoLinkId = $(that).attr("id") + strInfoLinkTargetSuffix;
													thisInfo = $("#" + thisInfoLinkId);
													APPSTORE.viewInfo(thisInfo);
												});
											}
										},
									
						viewInfo:	function(thisInfo) {
										var lightbox = APPSTORE.lightbox,
											lightboxHeight = $(window.document).height(),
											lightboxWidth = $(window.document).width(),
											theInfo,
											theInfoLeftPos,
											topPos = 100 + $("html").scrollTop(),
											theInfoWidth,
											lightboxCloseBtn = $('<a id="lightboxCloseBtn">Close</a>');
											
											
										//	prepare lightbox content (clone theInfo and set some css)
										theInfo = $(thisInfo).clone();
										$(theInfo).attr("id","lightboxContent");
										//alert($("#lightboxContent").find("img").size());
										theInfoWidth = $(theInfo).width();
										theInfoLeftPos = (lightboxWidth / 2) - (theInfoWidth / 2);
										$(lightboxCloseBtn)
											.bind("click",function() {
												$(lightbox).addClass("hidden");
												$(theInfo).remove();
											});

										$(theInfo)
											.css({
												"left":theInfoLeftPos,
												"top":topPos
												})
											.prepend(lightboxCloseBtn);
										
										
										//	show the lightbox
										$(lightbox).removeClass("hidden").append(theInfo);
									},
									
						setupLightbox:	function() {
										var lightbox = APPSTORE.lightbox,
											lightboxHeight = $(window.document).height(),
											lightboxWidth = $(window.document).width(),
											lightboxCloseBtn = $("#lightboxCloseBtn");
										
										//	set the dimensions
										$(lightbox)
											.css({"height":lightboxHeight,"width":lightboxWidth});
										$(lightboxCloseBtn)
											.bind("click",function() {
												$(lightbox).addClass("hidden");
												$("#lightboxContent").remove();
											});
											
										//	add close button
										
									}
					}//	end APPSTORE
	APPSTORE.setupViewDemo();
	APPSTORE.setupViewInfo();
	APPSTORE.setupLightbox();
	
});

function registerUser(){
	var regForm = $('#loginForm');
	var isValid = validateForm(regForm);
	if(isValid){
		var emailObj = document.getElementById("emailAddress");
		var fnameObj = document.getElementById("firstName");
		var lnameObj = document.getElementById("lastName");
		var email = emailObj.value;
		var firstName = fnameObj.value;
		var lastName = lnameObj.value;
		var poststr =  encodeURI("email=" + email) + encodeURI("&fname=" + firstName) + encodeURI("&lname=" + lastName);
	
	    var options = {
				url: '/tlcconstantcontact/tlc/userRegistration',
				type: "POST",
				data: poststr,
				dataType: "html",
				success: function(data){
    				if(data == 'success'){
    					alert("User with email '"+email+"' successfully registered");
    					fnameObj.value = "";
    					lnameObj.value = "";
    					emailObj.value = "";
    				}else if(data == 'exists'){
    					alert("User with email '"+email+"' already registered");
    				}else if(data == 'error'){
    					alert("Connectivity error !!!");
    				}
    			},
    			error: function(xhr, status, error){
        			alert("failure :: "+error);
        		}
		};
		jQuery.ajax(options);
	}
}

function validateForm(theForm){
    var arrTextFields = $(theForm).find('input[type="text"]'),
        intTextFields = arrTextFields.length,
        arrEmailFields = $(theForm).find('input[type="email"]'),
        intEmailFields = arrEmailFields.length,
        arrErrorFields = [],
        isValid = false,
        thisField,
        thisFieldId,
        thisFieldValue,
        thisFieldErrorMsg = "",
        thisFieldError = {},
        strEmailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/,
        regexEmail = new RegExp(),
        hasError = false,
        i;
        
    regexEmail = strEmailPattern;
        
    //  validate text fields
    for (i = 0; i < intTextFields; i++) {
        thisField = arrTextFields[i];
        thisFieldId = $(thisField).attr("id");
        thisFieldValue = $(thisField).val();
        
        //  clear any errors for this field
        $(thisField).removeClass("error");
        $(thisField).parent().find(".error").remove();
        
        //  verify required fields completed
        if (thisFieldValue.length == 0) {
            hasError = true;
            thisFieldErrorMsg = "required field.";
        }
        
        
        //  add text field error if necessary
        if (hasError) {
            thisFieldError = {"target":thisField,"message":thisFieldErrorMsg};
            arrErrorFields.push(thisFieldError);                                                 
        }
        
        hasError = false;
    }
    
    //  validate email fields
    for (i = 0; i < intEmailFields; i++) {
        thisField = arrEmailFields[i];
        thisFieldValue = $(thisField).val();
        
        //  clear any errors for this field
        $(thisField).removeClass("error");
        $(thisField).parent().find(".error").remove();
        
        thisFieldErrorMsg = "";
        
        //  verify required fields completed
        if (thisFieldValue.length == 0) {
            hasError = true;
            thisFieldErrorMsg = "required field.";
        }
        
        //  verify email address in correct format
        if (!regexEmail.test(thisFieldValue)) {
            hasError = true;
            thisFieldErrorMsg += (thisFieldErrorMsg == "") ? "invalid email address." : " invalid email address.";
        }
        
        //  add text field error if necessary
        if (hasError) {
            thisFieldError = {"target":thisField,"message":thisFieldErrorMsg};
            arrErrorFields.push(thisFieldError);
        }
        
        hasError = false;
    }
    
    //  set value of isValid
    isValid = (arrErrorFields.length > 0) ? false : true;
    
    if (!isValid) {
        showErrors(arrErrorFields);
    }
    return isValid;

}

function showErrors(arrErrorFields) {
    for (i = 0; i < arrErrorFields.length; i++) {
        errorField = arrErrorFields[i].target;
        $(errorField).addClass("error");
        
        errorMsg = arrErrorFields[i].message;
        errorMsgObj =   $("<span style='color:#b60000; padding-left:1.5em;'></span>")
                            .addClass("error")
                            .append(errorMsg);
        
        $(errorField).after(errorMsgObj);
    }
}

