// JavaScript Document
var stopSlides = false;

$(window).load(function(){

    setInterval( "rotateSlides()", 6000 );
});

$(document).ready(function(){

    $(".dot").click(function(){
        stopSlides = true;
        dotClick($(this));
        return false;
    });

    $(".popUpForm").click(function(){
        $('#demoModal').modal();
        return false;
    });

    $("#DLButton").click(function(){

        var DLFormatError = $("#DLFormatError");
        DLFormatError.hide();

        var email = document.getElementById($("#DLemail").attr('id'));
        var emailPattern = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
        if(email.value == "" || !emailPattern.test(email.value.replace(/ /g,""))){
                DLFormatError.show();
                return false;
        }
        $("#DLForm").submit();



        return false;
    });

    $('#DLForm').ajaxForm(function() {
        $("#DLAck").show();
        $("a.modalCloseImg").click();
    });
	
	// show/hide script for company page
    var showText='Read more about ';
    var hideText='Minimize [-]';
    var is_toggled = false;

    $('.contentExtend').next().append(' <a href="#" class="toggleLink">'+showText+'</a><a href="#" class="dummyLink"></a>');
    $('.contentExtend').hide();
    $('a.toggleLink').click(function() {
    // change the link depending on whether the element is shown or hidden
    if ($(this).html()==showText+$(this).parent().attr('title')) {
    $(this).html(hideText);
    }
    else {
    $(this).html(showText+$(this).parent().attr('title'));
    }
    $(this).parent().prev('.contentExtend').toggle('fast');
    return false;
    });
	$('a.dummyLink').click(function() {
    // change the link depending on whether the element is shown or hidden    
   		$(this).prev('.toggleLink').html(showText+$(this).parent().attr('title')); 
		return false;   
    });
	$('a.dummyLink').click();
});

function dotClick(dot){

    var oldSlide = $(".dot.active").attr("theSlide");


    $(".dot").removeClass("active");

    $(".slide" + oldSlide).fadeOut('slow', function() {

        var newSlide = dot.attr("theSlide");

        $(".slide" + newSlide).fadeIn('slow');

        dot.addClass("active");
    });

}

function rotateSlides(){

    if(!stopSlides){

        $(".dot").each(function(index){

            if($(this).hasClass('active')){

                if(index == 4){
                    index = 0;
                } else {
                    index++;
                }

                dotClick($(".dot:eq(" + index + ")"));
                return false;
            }

        });
    }
}


/*SimpleCarousel.implement({
	page: function(direction){
		var current = this.currentSlide;
		this.stop();
		if(direction == 'previous'){
			var previous = (current-1 < 0) ? this.slides.length-1 : current-1;
			this.showSlide(previous);
			this.fireEvent('onRotate', previous);
			return this;
		}else{
			this.rotate();
		}
	}
});

function slidePage(direction){
	homeSlide.page(direction);
}

function toggleInit(){
   var winScroller = new Fx.Scroll(window);
   
   $$('.expandComponent').each(function(item){
		var thisSlider = new Fx.Slide(item.getElement('.contentExtend')).hide();
      	item.getElement('.contentExtend').style.display = "block";
		item.getElement('.contentToggle').addEvent('click', function(){
            thisSlider.toggle();
            if(thisSlider.open){
               item.getElement('.contentToggle').set('html', 'Read more...');
            }else{
               item.getElement('.contentToggle').set('html', 'Minimize');
            }
            winScroller.toElement(item);
            return false;
         });})
}*/

function nospam(user){
	//Contact page does not yet handle emailSubject=user.
	var domain = "networkedinsights.com" ;
	var address = user.replace("[..DOT..]",".") + "@" + domain; //assembles address
	var linkText = address
	var noSpamLink = "<a href='/contact/?emailSubject=user&address=" + address + "'>" + linkText + "</a>"; //creates a tag
	document.open();
	document.write(noSpamLink); //writes link to page
	document.close;	
}
