var People = {
	
	People : new Array(),
	Active : 0,
	
	Init : function() {
		if( $('#people').is('div') ) {
			
			var allPeople = $('#people .list li a');
			for( i = 0 ; i < allPeople.length; ++i ) {
				People.People[i] = new People.PeopleObject(i, allPeople[i].title, allPeople[i].href, allPeople[i].parentNode.getElementsByTagName('span')[0].innerHTML, allPeople[i].parentNode.className, allPeople[i].id, '');
				allPeople[i].id = 'People_' + i;
				allPeople[i].parentNode.id = 'People_Li_' + i;
				allPeople[i].onmouseover = People.DisplayInfo;
				allPeople[i].onmouseout = People.RemoveInfo;
				allPeople[i].removeAttribute('href');
				allPeople[i].onclick = People.DisplayPopup;
				allPeople[i].title = '';
				People.People[i].HTML = allPeople[i].parentNode.innerHTML;
			}
			$('#people .list li a').append('<span class="p"></span><span class="mo"></span>');
		}
	},
	
	DisplayPopup : function() {
		var id = this.id.split('_');
				id = id[1] * 1;
		People.Active = id;
		
		for( i = 0 ; i < People.People.length ; ++i ) {
			if( People.People[i].id == id ) { id = i; break; }
		}
		PeoplePopup.Show( People.People[id].DBID, People.People[id].party );
		return false;
	},
	
	DisplayInfo : function() {
		var id = this.id.split('_');
				id = id[1] * 1;
		
		for( i = 0 ; i < People.People.length ; ++i ) {
			if( People.People[i].id == id ) { id = i; break; }
		}
	
		if( $('#people_box').is('div') ) { $('#people_box').remove(); }

		var BoxText = '<div id="people_box">';
				BoxText+= 	'<h3 class="' + People.People[id].party +'">' + People.People[id].firstName + ' ' + People.People[id].lastName + '</h3>';
				BoxText+= 	'<h4>Antal poäng denna vecka:</h4>';
				BoxText+= 	'<p class="votes">';
				BoxText+= 		'<span class="total">' + People.People[id].voteTotal + '</span>';
				BoxText+= 		'<span class="posneg">';
				BoxText+= 			'<span class="pos">' + People.People[id].votePos + '</span>';
				BoxText+= 			'<span class="neg">' + People.People[id].voteNeg + '</span>';
				BoxText+= 		'</span>';
				BoxText+= 	'</p>';
				BoxText+= 	'<p class="more">Klicka på bilden för att läsa mer</p>';																																
				BoxText+= '</div>';
		
		$('#wrapper').append(BoxText);
		
		var ol = $('#' + this.id).offset();
		
		if( ol.left > 650 ) {
			$('#people_box').addClass('flip');
			$('#people_box').css('left', (ol.left - 290) + 'px');
		} else {
			$('#people_box').css('left', (ol.left + 81) + 'px');		
		}
		$('#people_box').css('top', (ol.top - 15) + 'px');		
				
	},
	
	RemoveInfo : function() {
		if( $('#people_box').is('div') ) { $('#people_box').remove(); }
	},
	
	PeopleObject : function(id, name, URL, votes, party, DBID, HTML) {
		votes = votes.split(',');
		name = name.split(',');
		DBID = DBID.split('_');
		this.id = id;
		this.DBID = DBID[1] * 1;
		this.firstName = name[0];
		this.lastName = name[1];		
		this.URL = URL;
		this.votePos = votes[0]*1; 
		this.voteNeg = votes[1]*1;
		this.voteTotal = parseInt(parseFloat( this.votePos + this.voteNeg ));
		this.party = party;
		this.HTML = HTML;
	}
}


var PeoplePopup = {

	WeeklyProfile : 89,
	
	Init : function() {
		if( $('#people').is('div') ) { 
			//PeoplePopup.Show(PeoplePopup.WeeklyProfile);
		}
	},
	
	Show : function( id, party ) {
		$('#content').append('<div id="people_popup_cover"><p class="PopupLoader">Laddar...</p></div>');
		$('#content').append('<div id="people_popup" class="' + party + '">Laddar…</div>');
		$('#people_popup_cover').click(function() {PeoplePopup.Remove();});
		$('#people_popup_cover').hide();
		$('#people_popup').hide();	
		$('#people_popup_cover').fadeIn(100);
		
		URL = '/templates/People_Popup.php?id=' + id + '&noCache=' + Math.random();
		$('#people_popup').load(URL, function() {
			if( id == PeoplePopup.WeeklyProfile ) { $('#people_popup .weekly_profile').css('visibility','visible'); }
			Global.SetPersonVars();
			Vote.Init();
			Feeds.Load();
			Feeds.Twingly.Search();
			PeoplePopup.LoadHistory(id);
			$('#people_popup .close').click(function() {
				PeoplePopup.Remove();
			});
			$('#people_popup').slideDown(250, function() {
				$('#people_popup_cover p.PopupLoader').remove();
			});
		});	
	},
	
	Remove : function() {
		$('#people_popup').slideUp(250, function() {
			$('#people_popup_cover').fadeOut(100, function() {
				$('#people_popup').remove();
				$('#people_popup_cover').remove();						
			});
		});
	},
	
	LoadHistory : function( id ) {
		var flashvars = {PeopleID:id};
		var params = {PeopleID:id};
		var attributes = {}	
		swfobject.embedSWF("/swf/Graph.swf", "history_swf", "290", "140", "9.0.0", flashvars, params, attributes);
	}
	
}
