var viewFixer = [];
var eventsJson = null;

jQuery(function(){
	jQuery('ul.sf-menu').superfish({
		autoArrows: false,
		dropShadows: false
	});

	$.each(viewFixer, function(index, value) {
		switch(value) {
			case "Calendar":
				if($.browser.msie) {
					//alert("idty");
					if(!isRTL) {
						$("#Sidebar").css({
							"height": $("#ColumnsWrapper").height(),
							"background-color": $(".EventsListWrapper .EventsList").css("background-color")
						});
						$(".fc-day-number").each(function() {
							$me = $(this);
							$me.html($me.html());
						});
					}
				}
				break;
		}
	});
});

function SubmitHomePageContactform(linkBtn) {
	$($(linkBtn).parents("form")[0]).submit();
}

var wasCalendarPrevNextButtonsInit = false;
function updateFCEventsView() {
	$(".fc .fc-event").each(function(){
		var $me = $(this);
		var height = parseInt($me.children("a").css("height").replace("px", ""));
		var minHeight = 30;//65; //83
				
		if(!$.browser.msie) {
			if(!isRTL) {
				minHeight = 30;//64;
			}

			$me.css({
				"top": parseInt($me.css("top").replace("px", "")) - 2,
				"left": parseInt($me.css("left").replace("px", "")) - 2,
				"width": parseInt($me.css("width").replace("px", "")) + 4,
				"height": (height >= minHeight ? height : minHeight)
			});
		}
		else{
			var left = 0;
			if(!isRTL) {
				minHeight = 30;//65;
				left = 2;
			}
		
			$me.css({
				"top": parseInt($me.css("top").replace("px", "")) - 2,
				"left": parseInt($me.css("left").replace("px", "")) - left,
				"width": parseInt($me.css("width").replace("px", "")) + 4,
				"height": (height >= minHeight ? height : minHeight)
			});		
		}
	});
	
	hideLoader();
	
	if(!wasCalendarPrevNextButtonsInit) {
		$(".fc-button-prev, .fc-button-next").click(function() {
			showLoader();
		});
	}	
}


var mycarousel_initCallback = function(carousel) {
	//console.log(carousel);
    // Disable autoscrolling if the user clicks the prev or next button.
    carousel.buttonNext.bind('click', function() {
        carousel.startAuto(0);
    });
 
    carousel.buttonPrev.bind('click', function() {
        carousel.startAuto(0);
    });
 
    // Pause autoscrolling if the user moves with the cursor over the clip.
    carousel.clip.hover(function() {
        carousel.stopAuto();
    }, function() {
        carousel.startAuto();
    });
};


function initFeaturedClientsJcarousel() {
	jQuery(document).ready(function() {

		var $theGallery = jQuery(jQuery('.ClientsGallery')[0]);
		var theItemsHtml = $theGallery.html();
		for(var i = 0; i <= 2; i++) {
			theItemsHtml += theItemsHtml;
		}
		$theGallery.append(theItemsHtml);
		
		jQuery('.ClientsGallery').jcarousel({
			scroll: 0,
			rtl: false,
			start:1,
			visible:4
			//,auto: 5000
			//,
			//wrap: 'circular',
			//initCallback: mycarousel_initCallback
		});
	});
}

function initClientsJcarousel() {
	jQuery(document).ready(function() {
		var numberOfItems = 6;
		var initFinished = function(carousel) {
			/*
			if($("li", carousel.list[0]).size() <= numberOfItems) {
				(carousel.buttonPrev).addClass("jcarousel-next-disabled");
				(carousel.buttonNext).addClass("jcarousel-next-disabled");
			}
			
			console.log(carousel);
			*/
		};

		var $clientsGallery = jQuery('.ClientsGallery');
		$clientsGallery.each(function(index) {
			var $theGallery = jQuery(this);
			var theItemsHtml = $theGallery.html();
			for(var i = 0; i <= 2; i++) {
				theItemsHtml += theItemsHtml;
			}
			$theGallery.append(theItemsHtml);
			
			var $lis = $("li", this);
			//if($lis.length > numberOfItems) {
			if(true) {
				var $me = jQuery(this);
				jQuery(this).jcarousel({
					//scroll: 1,
					scroll: 0,
					rtl: false,
					//start:1,
					//auto: 5000,
					//wrap: 'circular',
					initCallback: function(carousel) {
						mycarousel_initCallback(carousel);
						initFinished(carousel);
						if($clientsGallery.length > 0 && $clientsGallery.length - 1 == index) {
							$clientsGallery.each(function(index) {
								jQuery(this).parent().SetScroller({
									velocity: 975,
									direction: 'horizontal'
								});
							});
						}
					}
				});				
			}
			else {
				jQuery(this).jcarousel({
					rtl: false,
					start:1,
					initCallback: initFinished
				});
			}
		});
	});
}

function initClientsCategoryJcarousel() {
	$(".ClientsGallery li a").click(function(e) {
		e.preventDefault();
	});
	
	jQuery(document).ready(function() {
		showLoader();
		
		jQuery('.ClientsGallery').jcarousel({
			scroll: 1,
			rtl: false,
			start:1,
			visible:6,
			initCallback: function() {
				$(".ClientsGallery li").click(function() {
					showLoader();
					
					var $me = $(this);
					$(".ClientsGallery li").removeClass("selected");
					$me.addClass("selected");

					$(".ClientBlockWrapper").load($("a", $me).attr("href") + " .ClientBlock", function(data){
						jQuery('.ClientBlockWrapper').tinyscrollbar();
						hideLoader();
					});
				});
			}
		});
		
		// Client change
		jQuery(window).hashchange(function(){
			var hash = location.hash;
			if(hash == "") {
				var $firstClient = $(".ClientsGallery li:first");
				if($firstClient.length > 0) {
					location.hash = $firstClient.attr("id");
				}
			}
			else{
				$(".ClientsGallery li#" + hash).click();
			}
		});
		
		// For the start
		jQuery(window).hashchange();
	});
}

function showLoader() {
	if($(".blockUI").length == 0) {
		jQuery.blockUI({ 
			message: $('#LoaderBox'),
			css: {
	            border: 'none',
	            padding: '0',
	            backgroundColor: 'transparent'
			}
		});
	}
}

function hideLoader() {
	if($(".blockUI").length > 0) {
		jQuery.unblockUI();
	}
}

function initMagazinesPagination(selector) {
	if(!selector) {
		selector = "";
	}
	else {
		selector += " ";
	}

	$(selector + ".MagazinesPagination " + " a").each(function() {
		var $me = $(this);
		
		var thePageNum = parseInt(this.innerText);
		if(!isNaN(thePageNum)) {
			$me.addClass("P_" + thePageNum);
		}
		
		$me.click(function(e) {
			e.preventDefault();

			showLoader();
			
			var theHref = $me.attr("href");
			var $categoryItemParent = $me.parents(".CategoryItem");
			var categoryItemSelector = "#" + $categoryItemParent.attr("id");

			var linkPageIndex = "1";
			try {
				linkPageIndex = theHref.split("page")[1].replace("=", "");
			}
			catch(err) {
				linkPageIndex = "1";
			}
			window.magazinesHashArr[$categoryItemParent.index()] = linkPageIndex;
			location.hash = window.magazinesHashArr.join('_');
			
			$categoryItemParent.load(theHref + " " + categoryItemSelector + " .InnerContent", function() {
				$categoryItemParent.css("width", "323px");
				initMagazinesPagination(categoryItemSelector);
			});

			return(false);
		});
	});

	hideLoader();
}


function initMagazinesHash() {
	window.magazinesHashArr = [];
	$(".CategoryItem").each(function() {
		window.magazinesHashArr.push("");
	});
	
	var hash = location.hash.replace("#", "");
	if(hash != "") {
		var hashSplit = hash.split("_");
		$.each(hashSplit, function(index, value) {
			window.magazinesHashArr[index] = value;
			var $paginationLink = $(".CategoryItem:eq(" + index + ")").find("a.P_" + value);
			if($paginationLink.length > 0) {
				$paginationLink.click();
			}
		});
	}
}

function fbs_click() {u=location.href;t=document.title;window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');return false;}

