//initialize global variables
var curroffset=1;
var totaloffsets = 0;
var buttonwidth;
var buttonheight;
var isiPad = navigator.userAgent.match(/iPad/i) != null;
var isiPhone = navigator.userAgent.match(/iPod/i) != null;

function init_slider(cols,rows,total,mincols) {
	 $(".right_container").show();
	 if (cols==0) cols=1;
	 totaloffsets = Math.ceil(total/cols-rows);
	 $("#slider").css("background-image","none");
	 $(".item").css("visibility","visible");
	 $(".item").css("display","inline");
	 curroffset=1;
		 buttonwidth= $("#scrollup").width();
		 buttonheight= $("#scrollup").height();
		 adjustScrollers();
	 if ( $("#scrollup").size() != 0 && $("#scrolldown").size() != 0) {
		 // For use within normal web clients 
		if ( !(isiPad || isiPhone) ) {
			scrollerEvents();
		}
		

	 }
	$(".imagetitle, .offer").hide();
	$(".imagetitle, .offer").css("visibility","visible");
	$(".itemZoom").hover(function() {$('.imagetitle:visible, .offer:visible').fadeOut(); $(this).find('.imagetitle, .offer').stop(true,true).fadeIn();},function() {$(this).find('.imagetitle, .offer').stop(true,true); $(this).find('.imagetitle, .offer').fadeOut();});
	 $("#images_table").hide();
	 $("#images_table").fadeIn('fast');
	 if ($("#scrollup") && $("#scrolldown")) {
		 $('#scrollup, #scrolldown').show();
		 $('#scrollup, #scrolldown').css("visibility","visible");
		if ( !(isiPad || isiPhone) ) {
			 $('#scrollup a').hide();
			 $('#scrolldown a').hide();
		} else {
			 $('#scrollup a').hide();
		}
	 }
	
}

$(window).resize(function() {
	var bodywidth = $("#slider").width(); 
	$("#main").css({width:bodywidth});
	$("body").css({overflow:"hidden"});
    if(this.resizeTO) clearTimeout(this.resizeTO);
    this.resizeTO = setTimeout(function() {
        $(this).trigger('resizeEnd');
    }, 500);
});
$(window).bind('resizeEnd', function() {
	$("body").css({overflow:"visible"});
	$("#main").css("width",$("html").width());
		adjustScrollers();
	$(".right_container").css({"left":$("#images_table").width()-$(".right_container").width()+$("#images_table").position().left-6})
});
function adjustScrollers() {
	var maxtw=parseInt($(".item").css("max-width"));//maximum thumbnail width
	var tspacing = parseInt($(".item").css("margin-right"))+1;
	maxtw=maxtw+tspacing;
	var totalwidth = $(window).width();
	var minwidth = parseInt($("#main").css("min-width"));
	var maxwidth = maxtw*mincols;
	if (totalwidth>maxwidth) {
		colspace = Math.floor(totalwidth/maxtw);
		$(".item").width(maxtw-tspacing);
		if ( colspace > total ) colspace=total;
		if ( colspace != cols ) cols = colspace;
		$("#images_table").width((cols)*maxtw);
			
	} else {
		cols = mincols;
		if (totalwidth<minwidth) totalwidth=minwidth;
		itemwidth = totalwidth/cols-tspacing;
		$(".item").width(itemwidth);
		$("#images_table").width(cols*(itemwidth+tspacing));
	}
	if ($(".rectangle")) $(".rectangle").height($(".item").height());
	if (totaloffsets > Math.ceil(total/cols-rows) && curroffset==totaloffsets+1) {
		curroffset--;
	}
	if (totaloffsets < Math.ceil(total/cols-rows) && curroffset==totaloffsets+1) {
		curroffset++;
	}
	var sliderwidth=$("#slider").width();
	$("#partners_title, #contact_title").width(3*($(".item").width()+tspacing)-45)
	var margin = (sliderwidth-buttonwidth)/2;
	var cellheight=$("#images_table .item:eq(1)").height()+5;
	totaloffsets = Math.ceil(total/cols-rows);
	var index;
	var sliderheight;
	if (totaloffsets<=0) {
		index=total-1;
		sliderheight=$("#images_table .item:eq("+(index)+")").position().top-5+cellheight;
	}
	else {
		index=rows*cols;
		sliderheight=$("#images_table .item:eq("+(index)+")").position().top-5;
	}
	$('#slider').height(sliderheight);
	var scrolldowntop = sliderheight-buttonheight+1;

	$('#scrollup, #scrolldown').css({"marginLeft":margin,"marginRight":margin});
	$('#scrolldown').css({"top":scrolldowntop});
	if (curroffset>0) {
		$('#images_table').css({"top":(-curroffset+1)*cellheight});
		$('#scrollup').show();
	}
	$(".right_container").width($(".item").width());
	var right_content_height = Math.max($("#contact").height()+40,$("#partners").height()+40);
	var right_height = Math.ceil(right_content_height/cellheight)*cellheight-5;
	$(".right_container").height(right_height);
	$(".right_container").css({"left":$("#images_table").width()-$(".right_container").width()+$("#images_table").position().left-6});
		$("#main_overlay").height(sliderheight+5)
}

function scrollerEvents() {
	var tablewidth=$("#images_table").width();
	var cellheight=$("#images_table .item").height();
	var prevpos_up=0;
	var prevpos_down=0;
	
	$('#slider').mousemove(function(e){
		if (curroffset>1) {
			var ypos_up=$("#scrollup").offset().top+$("#scrollup").height()-e.pageY;
			if (prevpos_up<0 && ypos_up>=0) {
				$('#scrollup a').stop(true,true).fadeIn();
			} else if (prevpos_up>=0 && ypos_up<0) {
				$('#scrollup a').stop(true,true).fadeOut();
			}
			prevpos_up=ypos_up;
		}
		if (curroffset<totaloffsets+1) {
			var ypos_down=e.pageY-$("#scrolldown").offset().top;
			if (prevpos_down<0 && ypos_down>=0) {
				$('#scrolldown a').stop(true,true).fadeIn();
			} else if (prevpos_down>=0 && ypos_down<0) {
				$('#scrolldown a').stop(true,true).fadeOut();
			}
			prevpos_down=ypos_down;
		
		}

	});
	$('#slider')
		.mousewheel(function(event, delta) {
			if (delta > 0) {
				if (curroffset>1) {
					$("#scrolldown").show();
					var cellheight=$("#images_table .item:eq(1)").height()+5;
					$("#images_table").animate({top:"+="+cellheight},200,"linear");
					if (--curroffset==1) $("#scrollup a, #scrollup").hide();
					var scrollheight = (($("#slider").height()-$("#scrollpos").height())/totaloffsets)*(curroffset-1);
					$("#scrollpos").stop(true,true).animate({top:scrollheight, 'opacity':1},200,"linear").animate({'opacity':1},1000).animate({'opacity':0});
					
					return false; // prevent default
				}


			}
			else if (delta < 0) {
				if (curroffset<totaloffsets+1) {
					$("#scrollup").show();
					var cellheight=$("#images_table .item:eq(1)").height()+5;
					var scrollheight = (($("#slider").height()-$("#scrollpos").height())/totaloffsets)*curroffset;
					$("#scrollpos").stop(true,true).animate({top:scrollheight, 'opacity':1},200,"linear").animate({'opacity':1},1000).animate({'opacity':0});
					if (++curroffset==totaloffsets+1) $("#scrolldown a, #scrolldown").hide();
					
					
					$("#images_table").animate({top:"-="+cellheight},200,"linear");
					return false; // prevent default
				}
			}
			
		});
	$('#slider').mouseleave(function(e){
		if (curroffset>1) {
			$('#scrollup a').stop(true,true).fadeOut();
		}
		if (curroffset<totaloffsets+1) {
			$('#scrolldown a').stop(true,true).fadeOut();
		}
	});
	$('#slider').mouseenter(function(e){
		if (curroffset>1) {
			var ypos_up=$("#scrollup").offset().top+$("#scrollup").height()-e.pageY;
			if (ypos_up>=0) {
				$('#scrollup a').stop(true,true).fadeIn();
			}
		}
		
		if (curroffset<totaloffsets+1) {
			var ypos_down=e.pageY-$("#scrolldown").offset().top;
			if (ypos_down>=0) {
				$('#scrolldown a').stop(true,true).fadeIn();
			}
		}
	});
}

function scrollDown() {
	$("#scrollup").show();
	if (isiPad || isiPhone) $("#scrollup a").show();
	var cellheight=$("#images_table .item:eq(1)").height()+5;
	$("#images_table").animate({top:"-="+cellheight});
	var scrollheight = (($("#slider").height()-$("#scrollpos").height())/totaloffsets)*curroffset;
	$("#scrollpos").stop(true,true).animate({top:scrollheight, 'opacity':1},200,"linear").animate({'opacity':1},1000).animate({'opacity':0});
	if (++curroffset==totaloffsets+1) $("#scrolldown a, #scrolldown").hide();
}
function scrollUp() {
	$("#scrolldown").show();
	if (isiPad || isiPhone) $("#scrolldown a").show();
	var cellheight=$("#images_table .item:eq(1)").height()+5;
	$("#images_table").animate({top:"+="+cellheight});
	if (--curroffset==1) $("#scrollup a, #scrollup").hide();
	var scrollheight = (($("#slider").height()-$("#scrollpos").height())/totaloffsets)*(curroffset-1);

	$("#scrollpos").stop(true,true).animate({top:scrollheight, 'opacity':1},200,"linear").animate({'opacity':1},1000).animate({'opacity':0});
}
