﻿var TabSet={
	Last:null,
	Show:function (name) {
		if (name==this.Last) return;
		if (this.Last) this.ToggleTab(this.Last);
		this.ToggleTab(name);
		this.Last=name;
		try {
			$("tabcontent"+name).getElementsByTagName("a")[0].onclick();
		} catch (ex) {}		
	},
	ToggleTab:function (name) {
		$("tabcontent"+name).Toggle();
		$("tablink"+name).ToggleClassName("sel");
	}
};
$DL(
	function () {
		//TabSet.Show("Regions");
	}
);
function ShowCustomersByRegionId(id,name,queryName) {
	setTimeout(
		function () {
			$("regionFlash").setVariable("CodeNameJS",queryName);
			$("regionFlash").TPlay("_root.doFromJS");;
		},
		1
	);
	Mantis.GNS.Pages.Content.CustomersControl.GetCustomersByRegionId(
		id,
		Mantis.Ajax.AjaxPro.Output(
			function (customers) {
				PrintCustomers(customers,name,$("region"+id));
			}
		)
	);
}
function ShowCustomersByMarketId(id,name) {
	Mantis.GNS.Pages.Content.CustomersControl.GetCustomersByMarketId(
		id,
		Mantis.Ajax.AjaxPro.Output(
			function (customers) {
				PrintCustomers(customers,name,$("market"+id));
			}
		)
	);
}

var MAX_RELATED_LINK_LENGTH=30;

var lastItem=null;
function PrintCustomers(customers,name,li) {
	if (lastItem==li) return;
	else if (lastItem) ToggleCustomers(lastItem);
	ToggleCustomers(li);
	lastItem=li;

	$("item-name").innerHTML=name;
	var html=[];
	customers.Each(
		function (c,i) {
			html.push(
'\
<div class="customer '+(((i+1)%2)==0 ? "even" : "odd")+'">\
	<div class="name">'+c.Header+'</div>\
	<div>'+c.Description+'</div>\
'
			);

if (c.CaseStudies && c.CaseStudies.length) {
	html.push('<div>');
	c.CaseStudies.Each(
		function (cs) {
			var header=cs.Header;
			if (header.length>MAX_RELATED_LINK_LENGTH) header=header.substr(0,MAX_RELATED_LINK_LENGTH-3)+"...";
			html.push('<div><a href="?Page=CaseStudy&CaseStudyId='+cs.CaseStudyId+'" title="'+Mantis.Text.Escape(cs.Header)+'">'+header+'</a></div>');
		}
	);
	html.push('</div>');
}
if (c.PressReleases && c.PressReleases.length) {
	html.push('<div>');
	c.PressReleases.Each(
		function (pr) {
			var header=pr.Header;
			if (header.length>MAX_RELATED_LINK_LENGTH) header=header.substr(0,MAX_RELATED_LINK_LENGTH-3)+"...";
			html.push('<div><a href="?Page=CaseStudy&CaseStudyId='+pr.PressReleaseId+'" title="'+Mantis.Text.Escape(pr.Header)+'">'+header+'</a></div>');
		}
	);
	html.push('</div>');
}

			html.push(
'\
</div>\
'
			);
		}
	);
	$("customer-list").innerHTML=html.join("");
}
function ToggleCustomers(li) {
	li.ToggleClassName("sel");
}

function GoToRegion(id) {
	try {
		$("region"+id).getElementsByTagName("a")[0].onclick();
	} catch (ex) {}
}

// Logos
Mantis.Events.Add(window,"load",Logos.Bind());

function Logos() {
	var TIME_TO_SHOW=1000,
		FADE_JUMP=0.1,
		OFFSET_BETWEEN_FADES=8000,
		FADE_MS=20;

	var currentLogo=0,currentFrame=0;
	var frames=[],logos=[];

	function InitLogos() {
		if (CLIENT_LOGO_COUNT<LOGOS_PER_PAGE) LOGOS_PER_PAGE=CLIENT_LOGO_COUNT;
		for (var i=0;i<CLIENT_LOGO_COUNT;i++) logos.push($("customerLogo"+i));
		for (var i=0;i<LOGOS_PER_PAGE;i++) {
			var frame=_("div",$("customer-logos-list"));
			frame.AddClassName("group");
			frames.push(frame);
		}
		for (var i=0;i<LOGOS_PER_PAGE;i++) SetFrameNextLogo(frames[i]);
		StartFadeLogos();
	}
	function StartFadeLogos() {
		setInterval(
			function () {
				//if (currentFrame==LOGOS_PER_PAGE-1) return;
				/*
				setTimeout(
					function () {
					},
					4000
				);
				*/
				var frame=frames[currentFrame];
				FadeOut(
					frame,
					function () {
						SetFrameNextLogo(frame);
						currentFrame++;
						if (currentFrame>=LOGOS_PER_PAGE) currentFrame=0;
						FadeIn(frame);
					}
				);
			},
			OFFSET_BETWEEN_FADES
		);
	}
	function SetFrameNextLogo(frame) {
		logos[currentLogo].Show();
		if (frame.hasChildNodes()) frame.removeChild(frame.firstChild);
		frame.appendChild(logos[currentLogo]);
		currentLogo++;
		if (currentLogo>=CLIENT_LOGO_COUNT) currentLogo=0;
	}
	function FadeIn(el,callback) { FadeElement(el,0,1,FADE_JUMP,callback); }
	function FadeOut(el,callback) { FadeElement(el,1,0,-FADE_JUMP,callback); }
	function SetGroupNextItem(group) {
		var idx=group.IndexOf(group.current);
		if (idx>=group.length) idx=-1;
		if (!group[idx+1]) group.current=group[0];
		else group.current=group[idx+1];
	}
	function FadeElement(el,from,to,jump,callback) {
		if (el._fading) return;

		el._fading=true;
		el.SetOpacity(from);

		var iv=setInterval(
			function () {
				el.SetOpacity(from+=jump);
				if ((from>=to && el.GetOpacity()<=to) || (to>=from && el.GetOpacity()>=to)) {
					clearInterval(iv);
					el.SetOpacity(to);
					el._fading=false;
					if (callback) callback();
				}
			},
			FADE_MS
		);
	}
	
	InitLogos();
}
