var stepsize = 50;
var manualStepsize = 200;
var steptime = 1000;

$(document).ready(function () {
	LinkPopups();
	AlignLabels();
	RenderGallery(".site_container #block_welcome");

	setTimeout(function(){
		RenderScroller(".site_container #block_customers", true);
	}, 250);
	
	$(".scrollto_content").disableSelection();
});

function RenderPopupContent(){
	LinkPopups();
	AlignLabels();
	RenderFormPosts();
	RenderGallery(".popup_content_container");
}

function LinkPopups(target_container){
	if(!target_container){
		$(".site_content a, a.popup, .sitemap a, .popup_content_container a").not(".nopopup, .inline").fancybox({
			'hideOnContentClick':false,
			'onComplete':function(){
				RenderPopupContent();
			},
			'padding':0
		});
	}else{
		$(target_container + " a").not(".nopopup, .inline").fancybox({
			'hideOnContentClick':false,
			'onComplete':function(){
				RenderPopupContent();
			},
			'padding':0
		});
	}

	$((target_container ? target_container : "") + " a.inline").click(function(){
		var inlinetarget = $(this).attr("target");
	
		$("a.inline").parent().removeClass("active");
		$(this).parent().addClass("active");

		$.ajax({
		  url: $(this).attr("href"),
		  success: function(data){
			$(inlinetarget).html(data);
			LinkPopups(inlinetarget);
		  }
		});

		return false;
	});
	
	$((target_container ? target_container : "") + " a.inline.default").trigger("click");
}

function RenderScroller(galleryId, autoScroll){
	var scrollerGallery = $('#' + galleryId);
	var scrollerWidth = scrollerGallery.outerWidth(true);

	$('#' + galleryId + " .filler").css("width", scrollerWidth + "px");
	$('#' + galleryId + " .filler").css("height", "50px");
	$('#' + galleryId + " .filler").css("float", "left");
	$('#' + galleryId + " .scrollto_content").scrollTo('+=' + scrollerWidth);

	$('#' + galleryId + " .scrollto_prev").click(function(){
		$('#' + galleryId).stopTime();
		$('#' + galleryId + " .scrollto_content").scrollTo('-=' + manualStepsize, steptime - 1, {easing:'linear', onAfter:function(){StartGalleryTimer(galleryId, stepsize);}});
	});
	
	$('#' + galleryId + " .scrollto_next").click(function(){
		$('#' + galleryId).stopTime();
		GalleryNext(galleryId, manualStepsize, function(){StartGalleryTimer(galleryId, stepsize);});
	});

	if(autoScroll){
		StartGalleryTimer(galleryId, stepsize);
	}
}

function StartGalleryTimer(galleryId, stepsize){
	$('#' + galleryId).everyTime(steptime, function(){
		GalleryNext(galleryId, stepsize);
	});
}

function GalleryNext(galleryId, stepsize, callback){
	var scrollerGallery = $('#' + galleryId);
	var scrollerWidth = scrollerGallery.outerWidth(true);

	if(!callback){
		callback = function(){};
	}

	var width = 0;
	$('#' + galleryId + " .scrollto_content img").each(function() {
		width += $(this).outerWidth( true );
	});

	var scrollerGalleryContent = $('#' + galleryId + " .scrollto_content");
	if((scrollerGalleryContent.scrollLeft() - scrollerWidth) < width){
		$('#' + galleryId + " .scrollto_content").scrollTo('+=' + stepsize, steptime - 1, {easing:'linear', onAfter: callback});
	}else{
		$('#' + galleryId + " .scrollto_content").scrollTo('0', {onAfter: callback});
	}
}

function RenderGallery(galleryId){
	$('#' + galleryId + ' .slides_container .slides').cycle({ 
		delay:  -2000, 
		timeout:  10000, 
		speed: 2000,
		fx: 'scrollHorz',
		prev: '#' + galleryId + ' .slides_container .prev',
		next: '#' + galleryId + ' .slides_container .next',
		pager: '#' + galleryId + ' .slides_container .pager',
		pagerAnchorBuilder: pagerFactory
	});
}

function pagerFactory(idx, slide) {
	return '<a href="#" class="nopopup"></a>';
}

function RenderFormPosts(){
	$("form").submit(function(){
		$(".cms_submit").attr("disabled", "true");
		var formid = $(this).attr("id");
		PostData($(this).attr("action"), $("#" + formid + ' :input'), function(data){
			$(".cms_submit").attr("disabled", "false");
			$(".popup_content_container").parent().html(data);
			RenderPopupContent();
		});
		return false;
	});
}

function AlignLabels(){
	// align form labels
	var max = 0;
	$("label").each(function(){
	    if ($(this).width() > max)
	        max = $(this).width();    
	});
	$("label").width(max);
}

function PostData(url, data, callback, sync){
	$.ajax({
		type: 'post',
		url: url,
		dataType: 'html',
		async: !sync,
		data: data,
		success: function(data){
			callback(data);
		}
	});
}

jQuery.fn.extend({ 
        disableSelection : function() { 
                return this.each(function() { 
                        this.onselectstart = function() { return false; }; 
                        this.unselectable = "on"; 
                        jQuery(this).css('user-select', 'none'); 
                        jQuery(this).css('-o-user-select', 'none'); 
                        jQuery(this).css('-moz-user-select', 'none'); 
                        jQuery(this).css('-khtml-user-select', 'none'); 
                        jQuery(this).css('-webkit-user-select', 'none'); 
                }); 
        } 
});
