jQuery.fn.debug = function() {
  return this.each(function(){
    $.log(this);
  });
};
jQuery.log = function(message) {
	
  if( window.console ) {
//	console.debug(message);
  } else {
 //    alert(message);
  }
  
};

// Create Namespace
Plan2 = {};
// MainClass
Plan2.Main = function(){
	$.log( 'Plan2.Main' );
	this.initialize();
}

Plan2.Main.prototype = {
	initialize: function(){
		$.log( 'Plan2.Main.initialize' );
		
		switch( window.template ){
			case "home":
				this.showHome();
				this.showActie();
				break;
			case "werk":
				this.showWerk();
				this.showActie();
				break;
			case "standaard":
				this.showStandaard();
				this.showActie();
				break;
			case "memory":
				this.showMemory();
				this.showActie();
		}
		this.onResize();
		$(window).resize(function(){
		 	$.log( 'resize' );
			window.site.onResize();
		});
		this.fixLinks();
	},
	fixLinks: function(){
		$(".text a").each(function(i,e){
			if( $(e).attr("href").substring(0,5)=="http:" && $(e).attr("href").indexOf("plan2ontwerp") == -1 && $(e).attr("href").indexOf("beta.xd.nl") == -1 ){
				$(e).attr("target","_blank");
			} else {
				$(e).attr("target","_self");
			}
			if( $(e).attr("href").indexOf( "plan2ontwerp.blogspot") > -1 ){
				$(e).attr("target","_blank");
			}
			if( !$(e).attr("title") ){
			//	$(e).attr("title","Bezoek: "+$(e).html());
			}
		});
	},
	showActie: function(){
		$('#actie').hide();
		$('#actie').show('slow');
		this.onResize();
	},
	onResize: function(){
		var w = $('body').width();
		var x = Math.ceil(( w - 990 )/2)-10;
		if( x<0 ) x=0;
		$('#actie').css('left', x+'px' );
	},
	showHome: function(){
		$.log( 'showHome' );
		//$( 'div.home_image' ).hide();
		//$( 'div.home_image' ).fadeIn( 'slow' );
		this.initSlideShow();
	},
	initSlideShow: function(){
		if( $('#slideshow .item').length == 1 ){
			$('#slideshow').hide();
			$('#slideshow').fadeIn('slow');
			return;
		}
		setTimeout( this.showNextSlide, 100 );
		setInterval( this.showNextSlide, 3000 );
		var slideCount = 0;
		
		$('#slideshow .item').each(function(i,e){
			$(e).hide();
			$(e).css('position','absolute');
			$(e).attr('id','slide'+i);
			slideCount++;
		});
		this.slideCount = slideCount;
		this.currentSlide = -1;
	},
	showNextSlide: function(){
		$.log( 'showNextSlide ' + window.site.currentSlide + ' ' + window.site.slideCount );
		$('#slide'+window.site.currentSlide).fadeOut('slow');
		if(window.site.currentSlide++ > window.site.slideCount-2){
			window.site.currentSlide = 0;
		}
		$('#slide'+window.site.currentSlide).fadeIn('slow');
	},
	showWerk: function(){
		this.showStandaard();
	},
	showMemory: function(){
		$.log( 'showMemory' );
		var playerVersion = swfobject.getFlashPlayerVersion(); // returns a JavaScript object
		if( playerVersion.major < 6 ){
			this.flash = false;
		} else {
			this.flash = true;	
		}
		if( this.flash ){
			var w=550;
			var h=361;
			var flashvars = {
				rootURL: "http://www.plan2ontwerp.nl/"
			};
			var params = {
				menu: "false"
			};
			var attributes = {
			  id: "memory_swf",
			  name: "memory_swf"
			};
			swfobject.embedSWF("memory01.swf?x=1", "memory_flash", w, h, "8.0.0", "js/swfobject/expressInstall.swf", flashvars, params, attributes);
		}
	},
	showStandaard: function(){
		$.log( 'showStandaard' );
		$( 'div.thumb_menu' ).hide();
		$( 'div.thumb_menu' ).fadeIn( 'slow' );
		$( 'div.thumb_menu span' ).each(function( i, e ){
			$.log( i + e );
			$(e).click( function(){
				$.log( 'click:' + i );
//				window.site.showDetail( i );
			});
			$(e).hover(
					function(){
						$.log( 'hover' + e );
						$(this).addClass("hover");
						window.site.showDetail( i );
					},
					function(e){
						$(this).removeClass("hover");
					}
			);
		} );
		this.showDetail(0);
		
		$('div.detail_image').css("cursor", "pointer");
		
		var holder = this;
		$('div.detail_image').click( function(){
								$.log('click on image');
							holder.showPopupImage();	

							 } );
		
		
	},
	showPopupImage: function(){
		$.log('showPopupImage');
		$('#blinds').show();
		$('#blinds').click( function(){
							holder.hidePopupImage();								 
							} );
		
		// find visible image index
		
		
		// find images array from thumb menu
		
		
		// display popup
		var arr = $('div.detail_image img');
		$('div.detail_image img').each( function(i,e){
			var img_str = ($(arr[i]).attr('src')).replace('w=400&h=330','w=667&h=553');
			$("#popupwin").append('<img src="'+img_str+'" />');
		} );
		$("#popupwin img").hide();
		$( '#popupwin' ).show();
		$("#popupwin").css('left',$('body').width()/2 - 200 );
		var holder = this;
		$("#popupwin .loading").show();
		
		$("#popupwin").click(function(){
//			holder.showNextPopupImage();			  
			holder.hidePopupImage();
		});
		this.currentDetailIndex--;
		this.showNextPopupImage();
		
	},
	hidePopupImage: function(){
		$( '#blinds' ).hide();
		$( '#popupwin' ).hide();
	},
	showNextPopupImage: function(){

		this.currentDetailIndex++;
		if( this.currentDetailIndex > $('#popupwin img').length-1 ){
			this.currentDetailIndex = 0;
		}
		$.log( 'showNextPopupImage' + this.currentDetailIndex );
		var index = this.currentDetailIndex;
		$('#popupwin img').each( function(i,e){
			if( i==index ){
				$(e).fadeIn('slow');
			} else {
				$(e).fadeOut('slow');
			}
		} );
		
	},
	showDetail: function(index){
		this.currentDetailIndex=index;
		$('div.detail_image img').each(function( i, e){
			if( i==index ){
//				$(e).removeClass( 'displaynone' );
//				$(e).hide();
				$(e).fadeIn('slow');
			} else {
				$(e).fadeOut('slow');
//				$(e).addClass( 'displaynone' );
			}
		} );
	}
}

Plan2.Form = {
	initialize: function(){
	},
	check: function( id, autosubmit ){
		$.log( 'checking form' );

		if( autosubmit != false ){
			var autosubmit = true;	
		}
		var check = true;
		
		$( "label.checkboxlabel:contains('*')" ).each( function(index,element){
			$.log("checkbox found");
			var cb = $('input.formcheckbox', $(element).parent() );
			if( !$(cb).attr('checked') ){
				$(element).addClass('error');	
				check = false;
			} else {
				$(element).removeClass('error');	
			}
		} );
		
		
		$( "td.key:contains('*')" ).each( function(index,element){
			//var id =( $(element).attr('for') );
			var input = $('input', $(element).parent());
			
			if( $(input).hasClass("formtext") ){
				$.log('hasclass formtext');
				if( $(input).val()=="" ){
					$(element).addClass("error");
					check = false;
				} else {
					$(element).removeClass("error");
				}
			}
			
			// email
			if( $(element).text().indexOf( "mail" ) > -1 ){
				if( Plan2.Form.validateEmail( $(input).val() ) ){
					$(element).removeClass("error");
				} else {
					$(element).addClass("error");
					check = false;
				}
			}
		});
		if( check && autosubmit ){
			Plan2.Form.submitForm( id );
		}
		return check;
	},
	validateEmail: function( email ){  
		var RE_EMAIL = /^[a-zA-Z.0-9]+[a-zA-Z0-9_-]+@[a-zA-Z0-9]+[a-zA-Z0-9.-]+.[a-z]{2,4}$/;
		return RE_EMAIL.test( email );
	}

};


/* ----- Start Vandergoes ----- */

$(document).ready(function() {
	$.log( 'ready' );
	window.site = new Plan2.Main();
});

