Rot13 = {
    map: null,
    convert: function(a) {
        Rot13.init();
        var s = "";
        for (i=0; i < a.length; i++) {
            var b = a.charAt(i);
            s += ((b>='A' && b<='Z') || (b>='a' && b<='z') ? Rot13.map[b] : b);
        }
        return s;
    },
    init: function() {
		if (Rot13.map != null) return;
		var map = new Array();
		var s   = "abcdefghijklmnopqrstuvwxyz";
		for (i=0; i<s.length; i++) map[s.charAt(i)] = s.charAt((i+13)%26);
        for (i=0; i<s.length; i++) map[s.charAt(i).toUpperCase()] = s.charAt((i+13)%26).toUpperCase();
        Rot13.map = map;
    }
}

function validate( email ) {
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	return ( !reg.test(email) )? false : true;
}

soberscove = {};

soberscove.Application = new Class({ 
    initialize: function(){
    	var slideshows = $$("div.slideshow");
    	if( slideshows ) this.initSlideshows( slideshows );
    	var slideshows = $$("div.largeSlideshow");
    	if( slideshows ) this.initSlideshows( slideshows );
    },
    initSlideshows: function( slideshows ){
		slideshows.each( function( aSlideshowElement ){
			var slideshow = new soberscove.Gallery( aSlideshowElement );
		}, this );
	}
});

soberscove.Gallery = new Class({
	
	element: null,
	prevLink: null,
	nextLink: null,
	imageContainer: null,
	imageIndex: 0,
	scroll: null,
	activeImage: null,
	scrollIncrement: 25,
	
	initialize: function( anElement ){
		this.element = anElement;
		// console.log( this.element );
		this.imageContainer = anElement.getElement( ".images" );
		this.images = this.imageContainer.getElements( "li" );
		this.prevLink = this.element.getElement( ".slideshowPrev" );
		this.nextLink = this.element.getElement( ".slideshowNext" );
		var count = ( anElement.hasClass( "largeSlideshow" ) )? 1 : 3;
		if( this.images.length <= count ){
			this.prevLink.setStyle( "display", "none" );
			this.nextLink.setStyle( "display", "none" );
		return;
		}
		var width = 0;
		this.images.each( function( anImage, anIndex ){
		    var measure = anImage.getSize().x + ( parseInt( anImage.getStyle( "marginRight" ) + parseInt( anImage.getStyle( "marginLeft" ) ) ) );
			width +=  measure;
		}, this );
		console.log( ":: measure :: ", width, anElement.getSize().x );
		this.imageContainer.getElement( "ul" ).setStyle( "width", width );
		this.prevLink.addEvent( "mousedown", this.startScroll.bindWithEvent( this, -1 ) );
		this.nextLink.addEvent( "mousedown", this.startScroll.bindWithEvent( this, 1 ) );
		this.prevLink.addEvent( "mouseup", this.endScroll.bindWithEvent( this ) );
		this.nextLink.addEvent( "mouseup", this.endScroll.bindWithEvent( this ) );
		this.prevLink.addEvent( "click", this.endScroll.bindWithEvent( this ) );
		this.nextLink.addEvent( "click", this.endScroll.bindWithEvent( this ) );            
		this.scroll = new Fx.Scroll( this.imageContainer );

	},


	startScroll: function( e, dir ){
		//console.log( "startScroll", this.scrollDiv, dir );
		e.preventDefault();
		$clear( this.scrollInterval );
		this.scrollInterval = this.scrollDiv.periodical( 30, this, dir );
	},
	
	endScroll: function( e ){
		e.preventDefault();
		$clear( this.scrollInterval );
	},
	
	scrollDiv: function( dir ){
		var destination = this.imageContainer.getScroll().x + ( ( this.scrollIncrement + 1 ) * dir );
		this.scroll.set( destination, 0 );
	}

});

window.addEvent( "domready", function(){
    
	$("emailLink").empty();
	$("emailLink").set( "html", Rot13.convert('<n uers="znvygb:vasb@fborefpbircerff.pbz">vasb@fborefpbircerff.pbz</n>') );

	if( $("recipient") ){
	    var recipient = $("recipient");
	    var result = Rot13.convert( recipient.get( "value" ) );
	    recipient.set( 'value', result );
//	    console.log( "&&&&", recipient, recipient.get( "value") );
	}

	var subLink = $("subscribeLink");
	subLink.addEvent( "click", function(e){
		var field = $("emailField");
		var result = validate( field.get("value") );
		if(!result){
			alert('Please enter a valid email address.');
			field.set( "value", "" );
			field.focus();
			field.addClass("invalid");
			e.preventDefault();
		}else{
			field.removeClass("invalid");
		}
	});
	
	var unsubLink = $("unsubscribeLink");
	if( unsubLink ){
		unsubLink.addEvent( "click", function(e){
			var field = $("emailField");
			var result = validate( field.get("value") );
			if(!result){
				alert('Please enter a valid email address.');
				field.set( "value", "" );
				field.focus();
				field.addClass("invalid");
				e.preventDefault();
			}else{
				field.removeClass("invalid");
			}
		});
	}
	
	soberscove.app = new soberscove.Application();
	
	var _gaq = _gaq || [];
	_gaq.push(['_setAccount', 'UA-22540079-1']);
	_gaq.push(['_trackPageview']);

	(function() {
		var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
		ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
		var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
	})();
	
});

