/*

 *
 */
jQuery.miraProjectsCarousel = 
{
	
	build : function(options) {

		try {		
			// Default settings
			// Default			
			var _carousel = this;
			
			var floater = $('.floater');
			var shadow = $('<div class="shadow"></div>');
			_carousel.append(shadow);
			shadow.width(floater.width()+4);
			shadow.height(floater.height())
			shadow.css({'position': 'absolute','top':'-4px','right':floater.css('right'),'margin-right':'-4px','opacity':'0.2','background-color':'#6e6e6e', 'z-index':1 })
			
			var scrollPane = $('.content-scroll');
			var scrollContent = $('.content-holder');
			
			scrollContent.width('').width(scrollContent.children().length * scrollContent.children().width())
			//check if scrollbar is nodig	
			if(scrollContent.children().length > 5){		
				var scrollbar = $(".content-slider").slider({
					animate: true,
					orientation: 'horizontal'
					,slide: function(e, ui){		 
						  var maxScroll = $(".content-scroll").attr("scrollWidth") - $(".content-scroll").width();
						  $(".content-scroll").stop('true','true').animate({scrollLeft: ui.value * (maxScroll / 100) }, 500);
						}
				});
				
				var handleHelper = scrollbar.find('.ui-slider-handle')				
				.mousedown(function(){
					scrollbar.width( handleHelper.width() );
				})
				.mouseup(function(){
					scrollbar.width( '100%' );
				})
				.mouseleave(function(){
					scrollbar.width( '100%' );
				})
				.append('<span class="ui-icon ui-icon-grip-dotted-vertical"></span>')
				.wrap('<div class="ui-handle-helper-parent"></div>').parent();
								
				/*set size scrollbar to amount of items to scroll*/
				function sizeScrollbar(){
					var remainder = scrollContent.width() - scrollPane.width();
					var proportion = remainder / scrollContent.width();
					var handleSize = scrollPane.width() - (proportion * scrollPane.width());
								
					scrollbar.find('.ui-slider-handle').css({
						'width': handleSize,
						'margin-left': -handleSize/2
					});					
					handleHelper.width('').width( scrollbar.width() - handleSize);			
				}
				sizeScrollbar();

				
			}
            /*anni p*/
            scrollContent.children().each(function (i, item) {
                jQuery(item).find('p').attr("rel", jQuery(item).find('p').height())

                jQuery(item).find('p').height(0);
                jQuery(item).mouseenter(
            						function (e) {
            						    var _target = $(e.currentTarget);
            						    _target.css('cursor', 'pointer');
            						    _target.find('p').stop(true, true).animate({ height: '120px' }, 600);

            						}
            					);
                jQuery(item).mouseleave(
            						function (e) {
            						    var _target = $(e.currentTarget);
            						    _target.css('cursor', '');
            						    _target.find('p').animate({ height: '0' }, 200);
            						}
            					);
            });
				
		}
		catch(err) {			
			return false;
		}
	}
};


jQuery.miraStartup = 
{
	build : function(options) {

		try {	
			var defaults = {
				carouselWidth		:	637
				,width				:	52
				,height				:	53
				,slices				:	10
				,animSpeed			:	500
				,effect				:	'sliceDownLeft'
				,timeBuff			:	0
				,fadeSpeed			:	1000
			}
			// Move to options
			var options = jQuery.extend(defaults, options);
			
			//default
			var _carousel = this;
			
			/*startup*/
			var _startup = _carousel.children('div.startup');
			var _startupContainer = _startup.children('div.container');
			var _image = _startupContainer.children('img');
			
			for(var i = 0; i < options.slices; i++){ 
				var sliceWidth = Math.round(options.carouselWidth/options.slices);
				if(i == options.slices-1){
					_startupContainer.prepend(
						$('<div class="nivo-slice"></div>').css({ left:(sliceWidth*i)+'px', width:(_startupContainer.width()-(sliceWidth*i))+'px', opacity:'0' })
					);
				} else {	
					_startupContainer.prepend(
						$('<div class="nivo-slice"></div>').css({ left:(sliceWidth*i)+'px', width:sliceWidth+'px', opacity:'0' })
					);
				}
			}
			
			var  i = 0;
			$('.nivo-slice').each(function(){
				var sliceWidth = Math.round(options.carouselWidth/options.slices);
				$(this).css({'z-index':50,  height:'0px', opacity:'0', background: '#fff' });
				i++;
			});
			
			function startAnnim(){
				if ($.browser.msie && $.browser.version.substr(0,1) < 8){
					$('div.startup > div.statistics').animate({ opacity:'0' }, options.animSpeed);
				}				
				var timeBuff = options.timeBuff;
				var i = 0;
				var slices = $('.nivo-slice');
				if(options.effect == 'sliceDownLeft'){ slices = $('.nivo-slice').reverse()};
				slices.each(function(){
					var slice = $(this);
					slice.css('top','0px');
					if(i == options.slices-1){
						setTimeout(function(){
							slice.animate({ height:'100%', opacity:'1.0' }, options.animSpeed, '', function(){  animFinished() });
						}, (100 + timeBuff));
					} else {
						setTimeout(function(){
							slice.animate({ height:'100%', opacity:'1.0' }, options.animSpeed);
						}, (100 + timeBuff));
					}
					timeBuff += 50;
					i++;
				});
				
			}
			
			function animFinished(){
			  //$('div.campagne > div.statistics').css("opacity","1.0");
				$("div.campagne").css("background-image", "url(/img/campagne_samenaantafel_soft.jpg)");
			  $("div.campagne").css("background-repeat", "no-repeat");
			  
				setTimeout(function(){
							_startup.animate({ width:'0' }, options.fadeSpeed,'',  function(){  fadeAnimFinished() });
						}, (500));
			}
			
			function fadeAnimFinished(){
				_startup.css('display', 'none');
				  $("div.campagne > div.statistics").css('cursor','pointer');
					$("div.campagne > div.statistics").click(function(){
					  sLink = $("#statlink").attr("href");
				    document.location = sLink;
					});
				//$('.nivo-slice').css("display",'none');
			}			
			/*startup*/
			/*startAnnim*/
			setTimeout(function(){
				startAnnim();
			} , (4000));
			/**/
		}		
		catch(err) {
			return false;
		}	
	}
}

jQuery.miraCarousel = 
{
	
	
	build : function(options) {

		try {		
			// Default settings
			var defaults = {
				width				:	52
				,height				:	53
				,expandedWidth		:	200
				,expandedHeight		:	200
				,speedIn			:	200
				,speedOut			:	300
				
			}
			// Move to options
			var options = jQuery.extend(defaults, options);

			
			// Default			
			var _carousel = this;
			
			/*gallery*/
			var _gallery =	_carousel.children('div.gallery');	
			_gallery.children('ul').children('li').css('overflow', 'visible')
			var _child_div = _gallery.children('ul').children('li').children('div.container');	
			
			_child_div.each(function(i, item) {
				var _hover = $('<div class="hover"></div>');
				var _child_img = jQuery(item).children('a').children('img');
				var _info = jQuery(item).children('div.info');
				_hover.append(_info);
				_hover.attr("rel", jQuery(item).children('a').attr('href'))
				_hover.css('background-image','url('+_child_img.attr("src")+')');
				_child_img.hide();
				jQuery(item).prepend(_hover);								 
			});			
			var object = _child_div.children('div.hover');
			/*gallery*/	
			
			// Add custom functions
			var extensions = {
				miraMouseenter		:function(e) { 	
					var _target = $(e.currentTarget);
					if ($.browser.msie && $.browser.version.substr(0,1) < 8){
						var _parent = _target.parent().parent();
						_parent.css('z-index', 99999 );	
					}
					_target.css('cursor', 'pointer' );
					_target.css('z-index', 99999 );
								
					_target.stop(true,true).animate(
						{ width:options.expandedWidth, height:options.expandedHeight }, options.speedOut, function(){
							_target.children('div.info').css('visibility', 'visible');	
						} ,false);				
				},
				miraMouseleave		:function(e) { 
					var _target = $(e.currentTarget);
					_target.children('div.info').css('visibility', 'hidden');
					if ($.browser.msie && $.browser.version.substr(0,1) < 8){
						var _parent = _target.parent().parent();
						_parent.css('z-index', 1 );	
					}
					_target.css('z-index', 1 );						
					_target.css('cursor', '' );
					_target.stop(true,true).animate({ width:options.width, height:options.height}, options.speedIn, function(){
						_target.children('div.info').css('visibility', 'hidden');
						}, false);							
				},
				miraClick			:function(e) { 
					var _target = $(e.currentTarget);
					window.location.href = _target.attr('rel');
				}				
			}		
			
			jQuery(object).mouseenter(function(e) {
				extensions.miraMouseenter(e);
				return false;		
			});
			jQuery(object).mouseleave(function(e) {
				extensions.miraMouseleave(e);
				return false;		
			});
			jQuery(object).click(function(e) {
				extensions.miraClick(e);
				return false;		
			});
			
			
		}
		catch(err) {
			return false;
		}
	}
};

jQuery.fn.reverse = [].reverse;

jQuery.fn.extend({
		miraStartup				:	jQuery.miraStartup.build,
		miraCarousel			:	jQuery.miraCarousel.build,
		miraProjectsCarousel	:	jQuery.miraProjectsCarousel.build
});
$(window).load(function(){
setSizeAble();

});
function setSizeAble(){
 // equalize sizes for homepage
	// padding fro A is 48 px;
	/*var paddingA = parseInt($("#sizequalizeA").css("padding-top"))
	              +parseInt($("#sizequalizeA").css("padding-bottom"))
								+parseInt($("#sizequalizeA").css("margin-top"))
	              +parseInt($("#sizequalizeA").css("margin-bottom"));
								//alert(paddingA);*/
	var paddingA = 	$("#sizequalizeA").parent().parent().find("h2").height()+10;	
	//alert( $("#sizequalizeB").height()+" "+ ($("#sizequalizeA").height()+paddingA));	
	if($("#sizequalizeB") && $("#sizequalizeA")
	   && $("#sizequalizeB").height() != $("#sizequalizeA").height()+paddingA){
		 if(  $("#sizequalizeB").height()> $("#sizequalizeA").height()+paddingA){
		   $("#sizequalizeA").css("height", $("#sizequalizeB").height()-paddingA);
			  $("#sizequalizeA").css("overflow","hidden");
		 }else{
		   $("#sizequalizeB").css("height", $("#sizequalizeA").height()+paddingA);
			 $("#sizequalizeB").css("overflow","hidden");
		 }
	  
	}
	
}
$(document).ready(function(){
//setSizeAble();
 // random background image 
	var imageTotal = 5;
	var randomBg = Math.floor(Math.random()*imageTotal)+1;
	$('body').addClass('bg'+randomBg);
	//homepage carousel	
	$('div.campagne').miraStartup();
	$('div.campagne').miraCarousel();
	//homepage projects scroll
	$('div.projects-carousel').miraProjectsCarousel();
	//gallery detail page
	$('#photos').galleryView({
		panel_width: 564,
		panel_height: 300,
		frame_width: 100,
		frame_height: 100
	});	
	$('#photos').css('border-top','5px solid black');	
	//form validation
	$("#contactform").validate({
		rules: {
			email: {
				required: true,
				email: true
			}
		}
	});
});



