function FocusInput(oInput,sText) {if ($(oInput).val() == sText) $(oInput).val(""); }
function BlurInput(oInput,sText) {if ($.trim($(oInput).val()) == "") $(oInput).val(sText); }

var Custom = {

	Template: null,

	Homepage: {
		Module: {
			Method: function() {
				
			},
			Init: function() {
				
			}
		},
		Ready: function() {
			Custom.RunLocalInitials(this,"Module");
			Custom.Global.Init("GlobalModule");
		}
	},

	Global: {

		Init: function(oInitials) {
			var This = this;
			if (typeof(oInitials) == "string")
				oInitials = oInitials.split(",");
			$.each(oInitials,function() { This[this].Init(); });
		},

		GlobalModule: {
			Init: function() {
				this.Method();
			},
			Method: function() {
				
			}
		}
		
	},

	RunLocalInitials: function(o,a) {
		if (typeof(a) == "string")
			a = a.split(",");
		var aLocalInitials = a;
		$.each(aLocalInitials,function() { o[this].Init(); });
	},

	Ready: function(sTemplate) {
		// Clears input onfocus
		$('.focusedInput').each(function() {
			$(this).attr("oldVal",$(this).val());								 
		}).bind("blur", function() {
			BlurInput(this,$(this).attr("oldVal"));
			$(this).removeClass('selected');
		}).bind("focus", function() {
			FocusInput(this,$(this).attr("oldVal"));
			$(this).addClass('selected');
		});

		
		
		ImgContainer.Init();
		
		
		this.Template = sTemplate;
		if (Custom[sTemplate] && Custom[sTemplate].Ready)
			Custom[sTemplate].Ready();
	}

}


/*var iPage = 1;
var iPageCount;
function ShowPage(iPage) {
	// todo: animation
	animate({
			marginLeft: (iPage*-233) + "px"
			})
	
	
}

function NextPage() {
	if (iPage < iPageCount) {
		iPage++;
		ShowPage(iPage);
	}
}

function PrevPage() {
	if (iPage > 1)
	{
		iPage--;
		ShowPage(iPage);
	}
}
*/
function SetNav(iNav) {
	$('.slide-gallery-nav').find('.nav-item').each(function(){
			$(this).removeClass('selected');
		});
	
	$('.slide-gallery-nav').find('.nav-item:eq('+iNav+')').addClass('selected');
}

$(document).ready(function() {

	
	
	$('#header .nav li').hover(function() {
		if ( $(this).find('.sub-menu').length > 0 ) {
			$(this).addClass('selected');
		}
	}, function(){
		if ( $(this).find('.sub-menu').length > 0 ) {
			$(this).removeClass('selected');
		}
	});
	
	$('.resources-list, .sorezki-tools').find('li').bind('click', function(){
		if ( $(this).find('a').html() =="RSS Feed" ) {
			window.open($(this).find('a').attr('href'));
		}
			else {
				location.href = $(this).find('a').attr('href');
			}
	});
	
	var isIE = (navigator.appName=="Microsoft Internet Explorer");
	var IEversion = navigator.appVersion;
	if(isIE) {
	IEversion = parseInt(IEversion.substr(IEversion.indexOf("MSIE")+4));
	} else {
	IEversion = 0;
	}
	//alert("isIE=" + isIE + ", version=" + IEversion);
	

	$('#timeline').find('li').bind('click', function(e){
		//alert(1)
		
		if ( $(this).find('summary').length==0 || IEversion == 7 ) {
			//alert(1);
			//$(this).find('a').trigger('click');
			var tempLink = $(this).find('a').attr('href');
			window.open(tempLink, '_blank');
			return false;
		}
		$(this).addClass('opened');
		$(this).animate({
			height: '155px',
			marginBottom: '-110px'
		}, 450, function() {
		// Animation complete.
		});
		$(this).find('.timeline-summary').fadeIn();
		$(this).find('a').fadeIn();
		
		$("#timeline").find("LI.opened").not($(this)).find(".x-button").trigger("click");
		e.stopPropagation();

	});
	
	var oTime = $('#timeline').find('.x-button').bind('click', function(){
		$(this).addClass('clicked');
		$(this).parent('li').animate({
			height: '45px',
			marginBottom: '0px'
		}, 450, function() {
			$(this).removeClass('opened');
			$(this).find('.x-button').removeClass('clicked');
		});
		$(this).parent('li').find('.timeline-summary').fadeOut();
		
		return false;
	});

	if (oTime.length)
	{
		$(document.body).bind("click", function(e) {
			var oEl = $(e.target);
			var oButton = $("#timeline").find("LI.opened");
			oButton.find(".x-button").trigger("click");
		});
	}
	
	$('A[data-target="_blank"]').each(function(){
		$(this).attr('target', '_blank');
	});
						  
	$('#featured-in').each(function(){
											   
		var imagesCount = $(this).find('li').length;
		var tempHeight = $('#featured-in').find('li').height();
		var iLoaded = 0;
			
		$(this).find('img').bind('load', function(){
			iLoaded++;
			if(iLoaded == imagesCount) {
				
				$('#featured-in').find('img').each(function(){
					var imgHeight = $(this).height();
					$(this).css('marginTop', (tempHeight - imgHeight) / 2);
				});
			}
		}).each(function() {
			$(this).attr("src",$(this).attr("src")+"?r="+new Date().getTime());
		});
		
	});
	

	var oIntervalSlideGallery = null;
	
	$('#slide-gallery').each(function(){
		elNum = $(this).find('li').length;
		elWidth = 233;
		currentPosition = 0;
		pIndex = 0;
		
		// APPEND NAV ITEMS
		for (var i=1; i <= elNum; i++) {
			$(this).find('.slide-gallery-nav').append('<span class="nav-item"></span>');
		}
		
		$(this).find('.nav-item:eq(0)').addClass('selected');
		
		// SET UL LENGTH
		$(this).find('ul').css('width', elNum * elWidth + 'px');
		
		//alert((elNum-1) * elWidth);

		var bInInterval = true;
		function oDoClickNext() {
			if (Math.abs(currentPosition) == (elNum-1) * elWidth && !bInInterval) {
				return false;
			}
				else {

					if (Math.abs(currentPosition) == (elNum-1) * elWidth) {
						
						pIndex = 1;
						currentPosition = -1 * elWidth;
						oDoClickPrev();
						return;
					}

					pIndex++;
								
					
					$('.slide-gallery-left-button').removeClass('disabled');
					
					currentPosition -= elWidth;
					
					$('#slide-gallery').find('ul').animate({
						marginLeft: currentPosition
					}, 400, function(){
						if (Math.abs(currentPosition) == (elNum-1) * elWidth) {
							$('.slide-gallery-right-button').addClass('disabled');
						}
					});
					
					SetNav(pIndex);
				}
		}

		function oDoClickPrev() {
			if (currentPosition == 0) {
				return false;
			}
				else {
					pIndex--;
					
					$('.slide-gallery-right-button').removeClass('disabled');
					
					currentPosition += elWidth;
					
					$('#slide-gallery').find('ul').animate({
						marginLeft: currentPosition
					}, 400, function() {
						if (currentPosition == 0) {
							$('.slide-gallery-left-button').addClass('disabled');
						}
					});
					
					SetNav(pIndex);
				}
		}


		
		// RIGHT BUTTON ACTIONS
		$('.slide-gallery-right-button').bind('click', function(){
			try
			{
				clearInterval(oIntervalSlideGallery);
				bInInterval = false;
			}
			catch (e)
			{
			}
			oDoClickNext();
		});

		oIntervalSlideGallery = setInterval(oDoClickNext,10 * 1000);
		
		// LEFT BUTTON ACTIONS
		$('.slide-gallery-left-button').bind('click', function(){
			try
			{
				clearInterval(oIntervalSlideGallery);
				bInInterval = false;
			}
			catch (e)
			{
			}
			oDoClickPrev();
		});
		
		// SET CONTENT AREA HEIGHT
		
		
		

		
		$('.slide-gallery-nav .nav-item').bind('click', function(){

			try
			{
				clearInterval(oIntervalSlideGallery);
				bInInterval = false;
			}
			catch (e)
			{
			}

			var iIndex = getIndex(this);
			
			$('.slide-gallery-nav').find('.nav-item').each(function(){
				$(this).removeClass('selected');
			});
			
			$(this).addClass('selected');
			
			currentPosition = elWidth * -iIndex;
			//alert(currentPosition);
			$('#slide-gallery').find('ul').animate({
				marginLeft: currentPosition + 'px'
			}, 400, function(){
				if (iIndex == 0) {
					$('.slide-gallery-left-button').addClass('disabled');
					$('.slide-gallery-right-button').removeClass('disabled');
				}
					else {
						$('.slide-gallery-left-button').removeClass('disabled');
					}
					
				if (iIndex == elNum-1) {
					$('.slide-gallery-right-button').addClass('disabled');
					$('.slide-gallery-left-button').removeClass('disabled');
				}
					else {
						$('.slide-gallery-right-button').removeClass('disabled');
					}
			});
		});
		
	});
	
	
	var iIntervalPage = 1;
	/*var oIntervalBanner = window.setInterval(function() {
		BannerNav.iPage = ++iIntervalPage;
		
		if (BannerNav.iPage > BannerNav.iPages) {
			BannerNav.iPage = 1;
			iIntervalPage = 1;
		}
		BannerNav.ShowCurrentPage();
	}, 10 * 1000);*/
	
	/*$('#content-area').each(function(){
		var headerHeight = $('#header').height();
		//alert(headerHeight);
		var footerHeight = $('#footer').height();
		//alert(footerHeight);
		var pageHeight = document.documentElement.offsetHeight;
		//alert(pageHeight);
		$(this).css('height', pageHeight - headerHeight - footerHeight - 80 - 3 + 'px');
	});*/
	
	//alert(document.documentElement.clientHeight)
	
	
	
	
	$('#footer-nav>ul').each(function(){

		$(this).children('li').not('.last').children('ul').not('.two-cols').each(function(){
			$(this).css('width', $(this).parent('li').width() + 2 + 'px');
		});
		
		$(this).children('li').not('.last').each(function(){
			$(this).find('a').hover(function(){
				$(this).append('<span class="hover-left"></span><span class="hover-right"></span>')
				$(this).addClass('hover');
			}, 
			function(){
				$(this).find('.hover-left').remove();
				$(this).find('.hover-right').remove();
				$(this).removeClass('hover');
			});
		});
	});
	
	$('#browser').find('img').bind('load', function(){
		$(this).parent().find('.img-preloader').css('display', 'none');
		$(this).parent().find('.img-preloader').remove();
		$(this).css('display', 'block');
	});
	
	$('#banner-nav li').not('.quick-tour').bind('click', function(){
		var iPage = getIndex(this) + 1;
		try {clearInterval(oIntervalBanner)}
		catch(e) {}		
		BannerNav.ShowPage(iPage);
	});
	
	$("#BannerNav_Prev").bind("click", function() {
		try {clearInterval(oIntervalBanner)}
		catch(e) {}								
		BannerNav.Prev();	
		
	});
	
	$("#BannerNav_Next").bind("click", function() {
		try {clearInterval(oIntervalBanner)}
		catch(e) {}									
		BannerNav.Next();											
	});
	
	BannerNav.Init();
	//scrollFix();
	
	if (document.images)
	{
	  pic1 = new Image(476,435); 
	  pic1.src="images/sorezki-b02.jpg"; 
	
	  pic2 = new Image(476,435);
	  pic2.src="images/sorezki-b03.jpg"; 
	
	  pic3 = new Image(476,435);
	  pic3.src="images/sorezki-b04.jpg"; 
	}
	
	
});



var BannerNav = {
	Items: [
		{html:"",image:"",url:""},
		{html:"",image:"images/sorezki-b02.jpg",url:"sorezki.com/linkr/link-building-services/"},
		{html:"",image:"images/sorezki-b03.jpg",url:"sorezki.com/linkr/tracking-analysis/"},
		{html:"",image:"images/sorezki-b04.jpg",url:"sorezki.com/linkr/tracking-analysis/"}
	],
	iPage: 1,
	iPages: null,
	Init: function() {
		
		this.iPages = this.Items.length;
		this.Items[0].html = $("#BannerNav_Content").html();
		this.Items[0].image = $("#Img").attr("src");
		this.Items[0].url = $("#BannerNav_Url").html();
		
		
		

		
		
	},
	ShowCurrentPage: function() {
		var oCurrentItem = this.Items[this.iPage-1];
		
		// oCurrentItem.html
		// oCurrentItem.image
		// oCurrentItem.url
		
		ImgContainer.SetImg(oCurrentItem.image);
		
		$("#BannerNav_Url").html(oCurrentItem.url);
		$("#BannerNav_Content").html(oCurrentItem.html);
	
		
		
		if (this.iPage == 1) {
			$("#BannerNav_Prev").addClass("disabled");	
		} else {
			$("#BannerNav_Prev").removeClass("disabled");	
		}
		
		if (this.iPage == this.iPages) {
			$("#BannerNav_Next").addClass("disabled");	
		} else {
			$("#BannerNav_Next").removeClass("disabled");	
		}	
		
		$('#banner-nav li').removeClass("selected").filter(":eq("+(this.iPage-1)+")").addClass("selected");
		
		$('#banner-items li').fadeOut(300).filter(":eq("+(this.iPage-1)+")").fadeIn(300);
		
	},
	Next: function() {

		if (this.iPages > this.iPage) {
			this.iPage++;
			this.ShowCurrentPage();
		}
	},
	Prev: function() {
		if (this.iPage > 1) {
			this.iPage--;
			this.ShowCurrentPage();
		}
	},
	ShowPage: function(iPage) {
		this.iPage = iPage;
		this.ShowCurrentPage();
	}
}











var ImgContainer = {

	Elements: {
		Container: null,
		Scroller: null,
		Img: null
	},

	SetImg: function(sImage) {
		this.Elements.Img.attr("src",sImage);
	},

	Properties: {
		iContainerHeight: 0,
		iImgHeight: 0,
		iMovePos: 0,
		iMaxSpeed: 20,

		iMinMargin: 0,
		iMaxMargin: 0
	},

	IntervalScroll: null,

	Calculate: function() {

		
		var iMargin = Math.abs(this.Elements.Img.css("marginTop").replace(/px/gi,"")*1);
		
	
		iMargin += this.Properties.iMovePos * this.Properties.iMaxSpeed;

		if (iMargin < this.Properties.iMinMargin)
		{
			this.Elements.Img.css("marginTop",(this.Properties.iMinMargin)+"px");
		} else if (iMargin > this.Properties.iMaxMargin)
		{
			this.Elements.Img.css("marginTop",(this.Properties.iMaxMargin*-1)+"px");
		} else {
			this.Elements.Img.css("marginTop",(iMargin*-1)+"px");
		}			
		

	},

	SetEvents: function() {

		var This = this;

		this.Properties.iContainerHeight = this.Elements.Scroller.height();
		this.Properties.iImgHeight = this.Elements.Img.height();
		this.Properties.iMovePos = 0;

		this.Properties.iMaxMargin = this.Properties.iImgHeight - this.Properties.iContainerHeight;
		
		

		this.Elements.Scroller.hover(
			function() {

				try {clearInterval(This.IntervalScroll);}
				catch (e) { }

				This.IntervalScroll = setInterval(function() {
					This.Calculate();
				},20);
			
			},
			function() {
				try {clearInterval(This.IntervalScroll);}
				catch (e) { }
			}
		);

		this.Elements.Scroller.mousemove(function(e) {
			
			
			var iPos = ((e.clientY + $(document).scrollTop()) - $(this).offset().top);

			This.Properties.iMovePos = iPos/This.Properties.iContainerHeight - 0.5;
		});

	},

	Init: function() {

		var This = this;


		this.Elements.Container = $("#ImgContainer");
		this.Elements.Scroller = $("#ImgContainerScroller");
		this.Elements.Img = $("#Img");
		
		this.Elements.Img.css("marginTop","0px");

		this.Elements.Img.bind("load", function() {
			This.SetEvents();
		});

		this.SetImg("images/sorezki-b01.jpg?rnd="+new Date().getTime());

	}
}
			
			
