/* Sushi script for bootstrap */
window.onload = function(){
	sushi_load();
//	$('.fitmask > img').css("opacity",'0');
	setTimeout('sushi_imagecontainer()',100);
};
window.resize = function(){
  sushi_sizes();
};
$(window).on('resize', function(){
	sushi_sizes();
	
});
$(window).on('scroll', function(){
	sushi_scroll_check();
});
$( "img" ).load(function() {
  // Handler for .load() called.
  setTimeout('sushi_sizes();',500);
});
var last_sushi_scroll_check = -1;
function sushi_scroll_check(){
    var currYOffSet = window.pageYOffset;
	if(last_sushi_scroll_check != currYOffSet){
		sushi_scroll_checked();
	}
	last_sushi_scroll_check = currYOffSet;
	
}
function sushi_scroll_checked(){
	//console.log('sushi_scroll_checked()');
	sushi_scroll();
}
/*******/
function sushi_load(){
	//console.log('sushi_load()');
	sushi_sizes();
	$("#footer").css('visibility','visible');
	$('#content .sr').css('visibility','visible');
	$('.sr-bg').css("display",'block');
	$('.sr-ov').fadeTo( "slow" , 1);
	sushi_smoothscroll();
	//start_lazyload();
	//setTimeout('start_lazyload();',100);
	setTimeout('start_lazyload();',200);
}
function start_lazyload(){
	//console.log('start_lazyload()');
	$("img.lazy").lazyload({
		effect : "fadeIn"
	});
}
function gourl(url){
	document.location.href = url;		
}
var sushi_sizes_lasttime = 0;
var sushi_sizes_wait = 1000;
var sushi_sizes_window_w = 0;
function sushi_sizes(){
	var window_w = window.innerWidth;
	console.log('sushi_sizes()');
	if(sushi_sizes_window_w != window_w){
		
		//sushi_sizes_window_w = window_w;
		setTimeout('go_sushi_sizes();',40);
	}
}
function pre_sushi_sizes(){
	//console.log('pre_sushi_sizes()');
	var d = new Date();
	var currentTime = d.getTime();
	console.log('pre_sushi_sizes() currentTime = ' + currentTime + 'sushi_sizes_lasttime = ' + sushi_sizes_lasttime + 'sushi_sizes_wait = ' + sushi_sizes_wait);
	if(currentTime - sushi_sizes_lasttime > sushi_sizes_wait){
		go_sushi_sizes();
	} else {
		setTimeout('pre_sushi_sizes();',sushi_sizes_wait);
	}
}
var go_sushi_sizes_loop = false;
function go_sushi_sizes(){
	if(go_sushi_sizes_loop == false){
		go_sushi_sizes_loop = true;
		console.log('go_sushi_sizes()');
		sushi_stickynavbar();
		sushi_row_sizes();
		sushi_carousel();
		sushi_imagemasks();
		sushi_fitmasks();
		sushi_bgvideo();
		sushi_labels();
		sushi_content_size();
		sushi_scroll();
		sushi_dimensions();
		sushi_shrinktofit();
		sushi_aligncards();
		check_tab_alignment();
		try {
		 bootedit_sizes();
		} catch(err) {
		  
		}	
		try {
		 multirowcarousel();
		} catch(err) {
		  
		}	
		
		go_sushi_sizes_loop = false;
	} else {
		console.log('skipped go_sushi_sizes()');
	}
	
}
function sushi_rem_resize(){
	//console.log('sushi_rem_resize()');
	var window_w = window.innerWidth;
	var window_h = window.innerHeight;	
	var fs_min = 11;
	var fs_max = 18;
	var window_w_min = 320;
	var window_w_max = 1440;
	if(window_w < window_w_min){
		fs = fs_min;
	} else if(window_w > window_w_max){
		fs = fs_max;
	} else {
		var p = (window_w-window_w_min)/(window_w_max - window_w_min)
		fs = parseInt(100*(fs_min * (1-p) + fs_max * p))/100;
	}
	////console.log('fs = '+fs_min);
	$('html').css("fontSize",fs);
}

function sushi_scroll(){
	//console.log('sushi_scroll()');
	sushi_bgparallax();	
	sushi_imagecontainer();
}

function sushi_row_sizes(){
	//console.log('sushi_row_sizes()');
	//resets
	$('.sr .center-vertical').each(function(index, element) {
		$(this).parent().css("paddingTop",0);
	});
	//per row
	$('.sr').each(function(index, element) {
		try {
			var id = $(this).context.id;
			sushi_row_sizes_id(id);
		}
		catch(error) {
		 // console.error(error);
		}

	});	
	//console.log('sushi_centervertical()');
	$('.sr .center-vertical').each(function(index, element) {
		var pt = parseInt( $(this).closest('.sr-container').css('paddingTop'));
		var pb = parseInt( $(this).closest('.sr-container').css('paddingBottom'));
		var ch = $(this).parent().height();
		var ih = $(this).height();
		var mt = ((ch + pt + pb) - ih) / 2 -pt; 
		/* - ( pt + pb ))/2;	*/
		if(mt<0){mt=0;}
		//console.log('pt = ' + pt + 'pb = ' + pb + ', ch = ' + ch + ', ih = ' + ih  + ', mt = ' +  mt);
		$(this).parent().css("paddingTop",mt);
		//$(this).css("marginBottom",-mt);
	});
}
function sushi_row_sizes_id(rowid){
	//console.log('sushi_row_sizes_id()');
	
	/*
	$('.sr-container').height(0);
	$('.full-height').height('auto');
	*/

	var window_w = window.innerWidth;
	var window_h = window.innerHeight;	
	var header_h = $("#headers").height();
	var full_h = window_h - header_h;
	$('#'+rowid+' .full-height').each(function(index, element) {
		 $(this).height(0);
		 $(this).css('minHeight',0);
	});
	$('#'+rowid+' .full-height').each(function(index, element) {
		var pt = parseInt( $(this).closest('.sr-container').css('paddingTop'));
		var pb = parseInt( $(this).closest('.sr-container').css('paddingBottom'));
		var h = full_h - pt - pb;
		//$(this).height(h);
		
		var ah =  $(this).height();
		//console.log('fullheight = ' + h + ', ah = ' + ah + ' , pt = ' + pt + ', pb = ' +pb);
		//if(ah < h){
		 $(this).css('minHeight',h);
		  $(this).css('height','auto');
		//}
	});	
	
	$('#'+rowid+' .sr-container').each(function(index, element) {
		var pt = parseInt( $(this).css('paddingTop'));
		var pb = parseInt( $(this).css('paddingBottom'));
		var rh =  $(this).height() ;
		var ch = rh + pt + pb;
		$('#'+rowid+' .sr-bg').height(ch);
		$('#'+rowid+' .sr-bg').css("marginBottom",(-ch));
		$('#'+rowid+' .sr-ov').height(ch);
		$('#'+rowid+' .w_content').height(ch);
	});
	
	
	
}
function sushi_content_size(){
	//console.log('sushi_content_size()');
	 $("#content").height('auto');	
	var window_w = window.innerWidth;
	var window_h = window.innerHeight;	
	var body_h = $("#body").height();
	var header_h = $("#headers").height();
	var footer_h = $("#footer").height();
	var footer_pt =  parseInt($("#footer").css('paddingTop'));
	var footer_pb =  parseInt($("#footer").css('paddingBottom'));
	var leftover_h = parseInt(window_h - header_h - footer_h - footer_pb - footer_pt - 1.5);
	var content_h = $("#content").height();
	if(content_h < leftover_h){
		 $("#content").height(leftover_h);	
	}
	
}
function sushi_bgparallax(){
	var window_w = window.innerWidth;
	var window_h = window.innerHeight;	
	$('.bgparallax > img').each(function(index, element) {
		var parallax_lev = 60;
		var ch = $(this).closest('.bgparallax').height();
		var cw = $(this).closest('.bgparallax').width();
		
		var cot = $(this).closest('.bgparallax').offset().top; 
  		var ow = $(window).scrollTop();
		
		var parat = 1 * (((  cot +  ch  - ow) /(window_h + 1 * ch))  );
		
		ch = ch + 2 * parallax_lev;
		
		var ih = $(this).context.naturalHeight;
		var iw = $(this).context.naturalWidth;	
		if(ih == 0){
			setTimeout('sushi_bgparallax()',100);
		}
		
		var mt =0; var ml=0;
		if((iw / ih) < (cw / ch)){
			var nw = cw;
			var nh = cw * ih/iw;
			mt = 1/2 * (ch - nh);
		} else {
			var nh = ch;
			var nw = ch * iw / ih;
			ml = (cw - nw)/2;
		}
		mt = mt - (parallax_lev * parat)
		$(this).height(nh);
		$(this).width(nw);
		$(this).css("marginTop",mt);
		$(this).css("marginLeft",ml);
    });	
	
}
var window_w_last = -1;
var window_h_last = -1;
var ow_last = -1;
var sushi_imagecontainer_count = 0;
function sushi_imagecontainer(){
	var window_w = window.innerWidth;
	var window_h = window.innerHeight;	
	var ow = $(window).scrollTop();
	//console.log('ow = ' + ow);
//	sushi_imagecontainer_count++;
	if(	window_h_last != window_h || window_w_last != window_w || ow_last != ow || sushi_imagecontainer_count < 10){
		if(	window_h_last != window_h || window_w_last != window_w || ow_last != ow){
			  sushi_imagecontainer_count =0;
		}
		$('.imagecontainer > img').each(function(index, element) {
			$(this).height(1);
			$(this).width(1);
			var parallax_lev = 60;
			var ch = $(this).closest('.imagecontainer').height();
			var cw = $(this).closest('.imagecontainer').width();
			
			var cot = $(this).closest('.imagecontainer').offset().top; //get the offset top of the element
			
			//window_h
			
			
			var parat = 1 * (((  cot +  ch  - ow) /(window_h + 1 * ch))  );
			
			//console.log('cot = ' + cot + ',  ow = '+ ow + ',  parat = '+parat);
			
			ch = ch + 2 * parallax_lev;
			
			var ih = $(this).context.naturalHeight;
			var iw = $(this).context.naturalWidth;	
			if(ih == 0){
				setTimeout('sushi_imagecontainer()',100);
			}
			//console.log('ih = ' + ih + ',  iw = '+ iw + ',  ch = '+ch+ ',  cw = '+cw);
		
			var mt =0; var ml=0;
			if((iw / ih) < (cw / ch)){
				var nw = cw;
				var nh = cw * ih/iw;
				mt = 1/2 * (ch - nh);
			} else {
				var nh = ch;
				var nw = ch * iw / ih;
				ml = (cw - nw)/2;
			}
			mt = mt - (parallax_lev * parat)
			$(this).height(nh);
			$(this).width(nw);
			$(this).css("marginTop",mt);
			$(this).css("marginLeft",ml);
		});	
	}
	window_h_last = window_h;
	window_w_last = window_w;
	ow_last = ow;
}

function sushi_bgvideo(){
	var window_w = window.innerWidth;
	var window_h = window.innerHeight;	
	$('.bgvideocontainer > video').each(function(index, element) {
		var parallax_lev = 0;
		var ch = $(this).closest('.bgvideocontainer').height();
		var cw = $(this).closest('.bgvideocontainer').width();
		
		var ttt = $(this);
		
	
		
		//var ih = $(this).context.dataset.height;
		//var iw = $(this).context.dataset.width;	
		var ih = $(this).context.videoHeight;
		var iw = $(this).context.videoWidth;	
	/*	console.log('ih = ' + ih + ',  iw = '+ iw + ',  ch = '+ch+ ',  cw = '+cw);*/
		if(ih == 0){
			setTimeout('sushi_bgvideo()',100);
		}
	
		var mt =0; var ml=0;
		if((iw / ih) < (cw / ch)){
			var nw = cw;
			var nh = cw * ih/iw;
			mt = 1/2 * (ch - nh);
		} else {
			var nh = ch;
			var nw = ch * iw / ih;
			ml = (cw - nw)/2;
		}
		$(this).height(nh);
		$(this).width(nw);
		$(this).css("marginTop",mt);
		$(this).css("marginLeft",ml);
   
   		/*console.log('nh = ' + nh + ',  nw = '+ nw + ',  mt = '+mt+ ',  ml = '+ml);*/

   
    });	
	
}
function sushi_imagemasks(){
	$('.mask > img').each(function(index, element) {
		var ch = $(this).closest('.masksize').height();
		var cw = $(this).closest('.masksize').width();
		var ih = $(this).context.naturalHeight;
		var iw = $(this).context.naturalWidth;	
		var mt =0; var ml=0;
		if(iw / ih < cw/ch){
			var nw = cw;
			var nh = cw * ih/iw;
			mt = (1/3) * (ch - nh);
		} else {
			var nh = ch;
			var nw = ch * iw / ih;
			ml = (cw - nw)/2;
		}
		$(this).height(nh);
		$(this).width(nw);
		$(this).css("marginTop",mt);
		$(this).css("marginLeft",ml);
    });	
}
function sushi_fitmasks(){
		
	$('.fitmask > img').each(function(index, element) {
		var ch = $(this).closest('.fitmask').height();
		var cw = $(this).closest('.fitmask').width();
		var ih = $(this).context.naturalHeight;
		var iw = $(this).context.naturalWidth;	
		var mt =0; var ml=0;
		if(iw / ih > cw/ch){
			var nw = cw;
			var nh = cw * ih/iw;
			mt = (1/2) * (ch - nh);
		} else {
			var nh = ch;
			var nw = ch * iw / ih;
			ml = (cw - nw)/2;
		}
		$(this).height(nh);
		$(this).width(nw);
		$(this).css("marginTop",mt);
		$(this).css("marginLeft",ml);
		
		//alert('f');
		//.css("visibility","visible");
		
//		$(this).css({ opacity: 100% });
    });	
}
function sushi_labels(){
	$('.card-label').each(function(index, element) {
		var lh = $(this).height();
		var pt = parseInt($(this).css('paddingTop'));
		var pb = parseInt($(this).css('paddingBottom'));
		ch = lh + pt + pb;
		$(this).css("marginTop",-ch);
		$(this).css("marginBottom",0);

		});	
		
}
function sushi_stickynavbar(){
	//
	
	var h=0;
	$('.fixed-top').each(function(index, element) {
		h = h + $(this).outerHeight(true);
	});
	if(h > 0 ){
		$('#navbarspacer').height(h);
		$('.s-anchor').css("marginTop",-h);
		$('.s-anchor').css("marginBottom",h);

	}
}
function sushi_smoothscroll(){
	//console.log('sushi_smoothscroll()');
}

$(document).ready(function(){
  // Add smooth scrolling to all links
$('a[href^="#"]').click(function () {
    $('html, body').animate({
        scrollTop: $('[name="' + $.attr(this, 'href').substr(1) + '"]').offset().top
    }, 900);

    return false;
});

});
function sushi_dimensions(){

	$('.size_3by2').each(function(){
			 var cw = $(this).width() ;
			ch = parseInt(cw * 2/3);
			 $(this).height(ch) ;
	});
	$('.size_1by1').each(function(){
			 var cw = $(this).width() ;
			ch = parseInt(cw );
			 $(this).height(ch) ;
	});
}

function sushi_shrinktofit(){
		//console.log('sushi_shrinktofit()');

//alleen verkleinen en niet vergroten
$('.imshrinkfit').each(function(){
		  var cw = $(this).width() ;
		  var ch = $(this).height() ;
		
			$('#'+this.id+ ' img').each(function(){
					var ih = $(this).context.naturalHeight;
					  var iw = $(this).context.naturalWidth;	
					  var mt =0; var ml=0;
					  if(iw/ih > cw/ch){
						  var nw = cw;
						  var nh = cw * ih/iw;
						  mt = (1/2) * (ch - nh);
					  } else {
						  var nh = ch;
						  var nw = ch * iw / ih;
						  ml = (cw - nw)/2;
					  }
					  if(iw < nw){
						nw = iw;
						nh = ih;
						 mt = (1/2) * (ch - nh);
						ml = (cw - nw)/2;
					  }
					  ml=0;
					   $(this).height(nh);
					    $(this).width(nw);
					  $(this).css("marginTop",mt);
					  $(this).css("marginLeft",ml);
					    $(this).css("visibility",'visible');
			 });
			
	});
	
	
$('.imfit').each(function(){
		  var cw = $(this).width() ;
		  var ch = $(this).height() ;
		
			$('#'+this.id+ ' img').each(function(){
					var ih = $(this).context.naturalHeight;
					  var iw = $(this).context.naturalWidth;	
					  var mt =0; var ml=0;
				 if(iw/ih == cw/ch){
						  var nw = cw;
					  var nh = ch;
				 } else if(iw/ih > cw/ch){
						  var nw = cw;
						  var nh = cw * ih/iw;
						 
						if(Math.abs(nh-ch) < 2){
					  	nh = ch;
					   } else {
						    mt = (1/2) * (ch - nh);
					   }
				
					  } else {
						  var nh = ch;
						  var nw = ch * iw / ih;
					if(Math.abs(nw-cw) < 2){
					  	nw = cw;
					   } else {
						  ml = (cw - nw)/2;
					   }
					  }
					 /*
					  if(iw < nw){
						nw = iw;
						nh = ih;
						 mt = (1/2) * (ch - nh);
						ml = (cw - nw)/2;
					  }
					  */
					  ml = 0;
					   $(this).height(nh);
					    $(this).width(nw);
					  $(this).css("marginTop",mt);
					  $(this).css("marginLeft",ml);
				$(this).css("visibility",'visible');
				$(this).css("opacity",'1');
				//$(this).fadeIn( 3000);
//				
				
			 });
			
	});	
	
	$('.imfittop').each(function(){
		  var cw = $(this).width() ;
		  var ch = $(this).height() ;
		
			$('#'+this.id+ ' img').each(function(){
					var ih = $(this).context.naturalHeight;
					  var iw = $(this).context.naturalWidth;	
					  var mt =0; var ml=0;
					  if(iw/ih > cw/ch){
						  var nw = cw;
						  var nh = cw * ih/iw;
						  mt = (1/2) * (ch - nh);
					  } else {
						  var nh = ch;
						  var nw = ch * iw / ih;
						  ml = (cw - nw)/2;
					  }
					 /*
					  if(iw < nw){
						nw = iw;
						nh = ih;
						 mt = (1/2) * (ch - nh);
						ml = (cw - nw)/2;
					  }
					  */
					  mt = 0;
					  ml = 0;
					   $(this).height(nh);
					    $(this).width(nw);
					  $(this).css("marginTop",mt);
					  $(this).css("marginLeft",ml);
					    $(this).css("visibility",'visible');
			 });
			
	});	
	
}











function sushi_aligncards(){
	$((' .card_space_text')).height('auto');
	$((' .card_space_button')).height('auto');
	$((' .card_inner')).height('auto');
	var topoffset_ref = 0;
	var cardcounter = 0;
	var collected_ids = [];
	$( ".card" ).each(function(){
		if(this.id){
			cardcounter++;
			var p = $('#'+this.id );
			var offset = p.offset();
			var topoffset = parseInt(offset.top);
			if(topoffset_ref == 0){topoffset_ref = topoffset;}
			if(topoffset != topoffset_ref){
				topoffset_ref = topoffset;
				if(collected_ids.length > 1){
					sushi_aligncards_by_ids(collected_ids);
				}
				collected_ids = [];
			}
			collected_ids.push(this.id);
		}
	});
	sushi_aligncards_by_ids(collected_ids);
}

function sushi_aligncards_by_ids(collected_ids){
	var maxh_kop = 0;
	var i;
	for (i = 0; i < collected_ids.length; i++) {
		var id = collected_ids[i];//console.log(id);
		var pih = $('#'+id+' .card_heading' );
		var hi =  pih.outerHeight();
		if(maxh_kop < hi){maxh_kop = hi;}
	}
	for (i = 0; i < collected_ids.length; i++) {
		var id = collected_ids[i];
		var kih = $('#'+id+' .card_heading' );
		var hk =  kih.outerHeight();
		var htext = maxh_kop - hk;
		$('#'+id+' .card_space_text' ).height(htext);
	}
	var maxh = 0;
	for (i = 0; i < collected_ids.length; i++) {
		var id = collected_ids[i];
		var p = $('#'+id );
		var h = p.innerHeight();
		if(maxh < h){maxh = h;}
	}
	for (i = 0; i < collected_ids.length; i++) {
		var id = collected_ids[i];
			var pih = $('#'+id+' .card_inner' );
			var hi =  pih.innerHeight();
			
			//var kih = $('#'+this.id+' .card_space_text' );
			//var hk =  kih.outerHeight();
			var lefthh= (maxh - hi) ;
			//var lefthh = lefth;// - hk;
			//var lefthh = parseInt(lefth/2);
			//
			$('#'+id+' .card_inner' ).height(maxh);
			//console.log(this.id + ' maxh=' + maxh + ' hi=' + hi + ' hk=' + hk + ' lefth=' + lefth + ' lefthh=' + lefthh);
			$('#'+id+' .card_space_button' ).height(lefthh);
	}
}


var sushi_carousel_padding = 30;
var sushi_carousel_minwidth = 300;
function sushi_carousel(){
	$( ".sushi-carousel" ).each(function(){
		
		var carouselID = this.id;
		var dPad = sushi_carousel_padding ;
		var carousel = $('#'+this.id+ '');
		var carouselWidth = parseInt( carousel.width() );
		//console.log(carousel);
		var carouselPosition =  $(this).attr('data-cardposition');//carousel.context.dataset.cardposition;
		var cardwidth =  parseInt($(this).attr('data-cardwidth'));//carousel.context.dataset.cardposition;
		
		var minWidth = sushi_carousel_minwidth;
		if(cardwidth > 0){
			minWidth = cardwidth;
		}
		
		var columnWidth = 0;var cols = 1;
		if(carouselWidth / 6 > minWidth){
			columnWidth = Math.floor((carouselWidth + dPad ) / 6);
			cols = 6;
		} else if(carouselWidth / 4 > minWidth){
			columnWidth = Math.floor((carouselWidth + dPad ) / 4);
			cols = 4;
		} else if(carouselWidth / 3 > minWidth){
			columnWidth = Math.floor((carouselWidth + dPad ) / 3);
			cols = 3;
		} else if(carouselWidth / 2 > minWidth){
			columnWidth = Math.floor((carouselWidth + dPad ) / 2);
			cols = 2;
		} else{
			columnWidth = Math.floor((carouselWidth + dPad) );
		}
		columnWidth = columnWidth - dPad;
		 var totalItems = $(("#"+carouselID+ " .sushi-carousel-item") ).length;
		var innerWidth = (totalItems + cols)  * columnWidth;
		
		//console.log('carouselID = ' + carouselID);
		//console.log('carouselWidth = ' + carouselWidth);
		//console.log('columnWidth = ' + columnWidth);
		//console.log('totalItems = ' + totalItems);
		//console.log('carouselPosition = ' + carouselPosition);
		$(this).attr('data-columnwidth', columnWidth);
		$(this).attr('data-visablecolumns', cols);

		$( ("#"+carouselID+ " .sushi-carousel-innercontainer") ).width(innerWidth);
		$( ("#"+carouselID+ " .sushi-carousel-item") ).width(columnWidth);
		var maxHeight = 0;
		$( ("#"+carouselID+ " .sushi-carousel-item") ).each(function(){
				var h = $(this).outerHeight();
				if(h > maxHeight){maxHeight = h;}
		});
		maxHeight = maxHeight + dPad/2;
		$( ("#"+carouselID+ " .sushi-carousel-container") ).height(maxHeight);
		var iheight = $( ("#"+carouselID+ " .sushi-carousel-control-prev") ).height();
		var iwidth = $( ("#"+carouselID+ " .sushi-carousel-control-prev") ).width();
		var window_w = window.innerWidth;
		var marginLeft =  -1 * (window_w - carouselWidth - dPad) / 2;
		if(marginLeft < (-1 * iwidth - .5 * dPad)){marginLeft = (-1 * iwidth - .5 * dPad);}
		$( ("#"+carouselID+ " .sushi-carousel-control-prev") ).css("marginLeft",(marginLeft+'px'));
		$( ("#"+carouselID+ " .sushi-carousel-control-next") ).css("marginRight",(marginLeft+'px'));
		var marginTop = -1 *  (maxHeight + iheight)/2;
			$( ("#"+carouselID+ " .sushi-carousel-control-prev") ).css("marginTop",(marginTop+'px'));
		$( ("#"+carouselID+ " .sushi-carousel-control-next") ).css("marginTop",(marginTop+'px'));
	
		sushi_carousel_move(carouselID);
	});
}
function sushi_carousel_move(carouselID){
	$( ('#'+carouselID) ).each(function(){
		var carouselID = this.id;
		var dPad = sushi_carousel_padding;
		var carousel = $('#'+this.id+ '');
		var carouselWidth =  carousel.width();
		var carouselPosition =  parseInt($(this).attr('data-cardposition'));//carousel.context.dataset.cardposition;
		var columnWidth = parseInt( $(this).attr('data-columnwidth'));//carousel.context.dataset.cardposition;
		//carousel.context.dataset.cardposition;
	
		var cMl = parseInt( $( ("#"+carouselID+ " .sushi-carousel-innercontainer") ).css("marginLeft"));
		var to_Ml = -1 * ((columnWidth + dPad) * carouselPosition +15);
		//console.log('columnWidth = ' + columnWidth);
		//console.log('carouselPosition = ' + carouselPosition);
		//console.log('cMl = ' + cMl);
		//console.log('to_Ml = ' + to_Ml);
		var ml = ((cMl + to_Ml)/2);
		//console.log('ml = ' + ml);
		if(ml != cMl){
		
			$( ("#"+carouselID+ " .sushi-carousel-innercontainer") ).css("marginLeft",(to_Ml+'px'));
			//if(ml != cMl){
			//setTimeout(sushi_carousel_move(carouselID),400);
		}
	
		
		
	});
}
function sushi_carousel_next(carouselID){sushi_carousel_dir(carouselID,1)}
function sushi_carousel_prev(carouselID){sushi_carousel_dir(carouselID,-1)}
function sushi_carousel_dir(carouselID,dir){
	$( ('#'+carouselID) ).each(function(){
		var carouselID = this.id;
		var dPad = 30;
		var carousel = $('#'+this.id+ '');
		var carouselWidth =  carousel.width();
		var carouselPosition =  $(this).attr('data-cardposition');
		var visablecolumns = parseInt( $(this).attr('data-visablecolumns'));
		var totalItems = $(("#"+carouselID+ " .sushi-carousel-item") ).length;
		var newPos = parseInt(carouselPosition);
		if(dir > 0){newPos++;}
		if(dir < 0){newPos--;}
		if(newPos < 0){newPos = totalItems-visablecolumns;}
		if(newPos > totalItems-visablecolumns){newPos = 0;}
		$(this).attr('data-cardposition', newPos);	
		sushi_carousel_move(carouselID);	
	});
}

var tabsshifted = false;
var tabsoriginal = '';
function check_tab_alignment(){

	var activetop = 0;
	var activetab = false;
	$( ".navigation_tabs .nav-tabs li.active" ).each(function(){
		var p = $('#'+this.id+ '');
		var offset = p.offset();
		var top = offset.top;
		if(activetop < top){activetop = top;}
		activetab = true;
	});
	if(activetab == true){
		var maxtop = 0;
		$( ".navigation_tabs .nav-tabs li" ).each(function(){
			var p = $('#'+this.id+ '');
			var offset = p.offset();
			var top = offset.top;
			if(maxtop < top){maxtop = top;}
		});
		if(activetop != maxtop){
	
			var tabul_lis = $(".navigation_tabs .nav-tabs").html() ;
			
			var tabitems = tabul_lis.split('</li>');
			if(tabsshifted == false){
				tabsoriginal = tabul_lis;
			//	tabitems[0] = '<span style="float:left;">&nbsp;&nbsp;&nbsp;</span><div style="width:100%;float:left;"></div>' + tabitems[0];
			}
		
			//console.log('re ordening tabs');
			var firsttab = tabitems[(tabitems.length - 2)];
			tabsshifted = true;
			i = tabitems.length -2;
			while(i--){
				tabitems[(i+1)] = tabitems[(i)];
			}
			tabitems[0] = firsttab;
			newhtml = tabitems.join('</li>')
			$(".navigation_tabs .nav-tabs").html(newhtml);
	if(tabsoriginal != ''){
		$(".widget-type-navigation_tabs .nav-tabs").html(tabsoriginal);
		tabsshifted = false;
	}
	 check_tab_alignment();
			check_tab_alignment();
		}
	}
}



$(document).ready(function(){

/**
 * This object controls the nav bar. Implement the add and remove
 * action over the elements of the nav bar that we want to change.
 *
 * @type {{flagAdd: boolean, elements: string[], add: Function, remove: Function}}
 */
var myScrolledElement = {

    flagAdd: true,

    elements: [],

    init: function (elements) {
        this.elements = elements;
    },

    add : function() {
        if(this.flagAdd) {
            for(var i=0; i < this.elements.length; i++) {
                document.getElementById(this.elements[i]).className += " fixed-theme";
            }
			setTimeout('sushi_sizes();',1000);
            this.flagAdd = false;
        }
    },

    remove: function() {
        for(var i=0; i < this.elements.length; i++) {
            document.getElementById(this.elements[i]).className =
                    document.getElementById(this.elements[i]).className.replace( /(?:^|\s)fixed-theme(?!\S)/g , '' );
        }
		setTimeout('sushi_sizes();',1000);
        this.flagAdd = true;
    }

};

/**
 * Init the object. Pass the object the array of elements
 * that we want to change when the scroll goes down
 */
myScrolledElement.init(  [
    "body"
]);

/**
 * Function that manage the direction
 * of the scroll
 */
 var last_currYOffSet = -1;
function offSetManager(){
	
	//console.log('offSetManager()');
    var yOffset = 0;
    var currYOffSet = window.pageYOffset;
	if(last_currYOffSet != currYOffSet){
		if(yOffset < currYOffSet) {
			myScrolledElement.add();
		}
		else if(currYOffSet == yOffset){
			myScrolledElement.remove();
		}
	}
	last_currYOffSet = currYOffSet;
}

/**
 * bind to the document scroll detection
 */
window.onscroll = function(e) {
    offSetManager();
}

/**
 * We have to do a first detectation of offset because the page
 * could be load with scroll down set.
 */
offSetManager();
});

	

var pleaseWaitDiv = [];
function bs_pleaseWait(message){
	if(message == false){message = 'Please wait...';}
	var pleaseWaitDiv = $('<div class="modal fade" id="pleaseWaitDialog" tabindex="-1" role="dialog" aria-labelledby="bsModelPopupLabel" aria-hidden="true"><div class="modal-dialog"><div class="modal-content"><div class="modal-header"><h4 class="modal-title" id="bsModelPopupLabel">' + message + '</h4></div><div class="modal-body"><div class="progress"><div class="progress-bar progress-bar-striped active" role="progressbar" aria-valuenow="45" aria-valuemin="0" aria-valuemax="100" style="width: 45%"><span class="sr-only">45% Complete</span></div></div</div></div></div></div>');
	pleaseWaitDiv.modal('show');	
}
function bs_pleaseWait_close(){
	$('#pleaseWaitDialog').modal('hide');
	$('.modal-backdrop').modal('hide');
	//modal('hide')
}
