function show(divid){
	document.getElementById(divid).style.display = "block";
}
function hide(divid){
	document.getElementById(divid).style.display = "none";
}


var hl = window.setInterval("chgHl('n','1');",5000);
var en = window.setInterval("chgEndorse('n','1');",2000);

// jQuery functios :: start
$(document).ready(function() {
	$(".hide").hide();
	
	$("#about").mouseover(function() {
		if($('.submenu').css('display') == 'none')
			$(".submenu").show();
	});
	$("#about .submenu").mouseover(function() {
		if($(this).css('display') == 'block')
			$(this).stop();
	});
	$("#about").mouseout(function() {
		if($(".submenu").css('display') == 'block')
			$(".submenu").hide();
		else
			$(".submenu").stop();
	});
	
	$("#involved").mouseover(function() {
		if($('.submenu2').css('display') == 'none')
			$(".submenu2").show();
	});
	$("#involved .submenu2").mouseover(function() {
		if($(this).css('display') == 'block')
			$(this).stop();
	});
	$("#involved").mouseout(function() {
		if($(".submenu2").css('display') == 'block')
			$(".submenu2").hide();
		else
			$(".submenu2").stop();
	});

	$("#facts").mouseover(function() {
		if($('.submenu3').css('display') == 'none')
			$(".submenu3").show();
	});
	$("#facts .submenu3").mouseover(function() {
		if($(this).css('display') == 'block')
			$(this).stop();
	});
	$("#facts").mouseout(function() {
		if($(".submenu3").css('display') == 'block')
			$(".submenu3").hide();
		else
			$(".submenu3").stop();
	});
	
	hl;
		
	$(".logo_facebook img, .logo_twitter img, .logo_youtube img, .logo_myspace img, .logo_flickr img, .logo_linkedin img, .logo_delicious img, .logo_picasa img, .logo_plaxo img, .logo_vimeo img, .logo_qik img, .logo_meetup img").mouseover(function(){
		var e = $(this).attr('src').replace('.gif','_over.gif');
		$(this).attr('src', e);
	});
	$(".logo_facebook img, .logo_twitter img, .logo_youtube img, .logo_myspace img, .logo_flickr img, .logo_linkedin img, .logo_delicious img, .logo_picasa img, .logo_plaxo img, .logo_vimeo img, .logo_qik img, .logo_meetup img").mouseout(function(){
		var e = $(this).attr('src').replace('_over.gif','.gif');
		$(this).attr('src', e);
	});
});



function toggleDiv(el,id)
{
	if($(id).css('display') == 'block'){
		$(el).html('more');
		$(id).css('display')='none;';
		//$(id).slideUp();
	}else{
		$(el).html('less');
		$(id).slideDown('slow');
	}
}



// funcao do destaque
function chgHl(d,b){

	if(b != '1')
		clearInterval(hl);

	var atual = parseInt($('#controller').val());
	var i =1;
	var t = $('#pictureBox .hide').each(function(){
		i++;
	});
	$('#destaque_'+atual).fadeOut();
	if(d == 'n'){
		if(atual == i){
			$('#destaque_1').fadeIn();
			$('#controller').val('1');
		}else{
			$('#destaque_'+(atual+1)).fadeIn();
			$('#controller').val(atual+1);
		}
	}else{
		if(atual == 1){
			$('#destaque_'+i).fadeIn();
			$('#controller').val(i);
		}else{
			$('#destaque_'+(atual-1)).fadeIn();
			$('#controller').val(atual-1);
		}
	}	
}

// funcao do destaque
function chgEndorse(d,b){
	if(b != '1'){
		clearInterval(en);
		$('#endorsement p').show();
	}else{
		var atual = parseInt($('#ctrl_end').val());
		var i =1;
		var e;
		var t = $('#endorsement p').each(function(){
			i++;
		});

		$('#end_s_1').html($('#end_s_2').html());
		$('#end_s_2').html($('#end_s_3').html());
		$('#end_s_3').html($('#end_s_4').html());
		$('#end_s_4').html($('#end_s_5').html());

		if(atual+1 == i){
			$('#end_s_5').html($('#end_1').html());
			$('#ctrl_end').val('1');
		}else{
			$('#end_s_5').html($('#end_'+(atual+1)).html());
			$('#ctrl_end').val(atual + 1);
		}
		
		/*if(atual+1 >= i){
			$('#end_s_1').html($('#end_1').html());
		}else{
			$('#end_s_1').html($('#end_'+(atual+1)).html());
		}
		
		if(atual+2 >= i){
			$('#end_s_2').html($('#end_1').html());
			e = 1;
		}else{
			$('#end_s_2').html($('#end_'+(atual+2)).html());
		}

		if(e > 0){
			e++;
			$('#end_s_3').html($('#end_'+e).html());
		}else if(atual+3 >= i){
			$('#end_s_3').html($('#end_1').html());
		}else{
			$('#end_s_3').html($('#end_'+(atual+3)).html());
		}
		
		if(e > 0){
			e++;
			$('#end_s_4').html($('#end_'+e).html());
		}else if(atual+4 >= i){
			$('#end_s_4').html($('#end_1').html());
		}else{
			$('#end_s_4').html($('#end_'+(atual+4)).html());
		}
		
		if(e > 0){
			e++;
			$('#end_s_5').html($('#end_'+e).html());
		}else if(atual+5 >= i){
			$('#end_s_5').html($('#end_1').html());
		}else{
			$('#end_s_5').html($('#end_'+(atual+5)).html());
		}
		
		if(e > 0){
			e++;
			$('#ctrl_end').val(atual+e);
		}else if(atual+5 >= i){
			$('#ctrl_end').val('1');
		}else{
			$('#ctrl_end').val(atual+5);
		}*/
	}
}

// jQuery functios :: end


getTwitters('tweet', { 
	id: 'davidweprin', 
	count: 3, 
	enableLinks: true, 
	ignoreReplies: true, 
	clearContents: true,
	template: '%text% <span class="time">%time%</span>'
});

function selectRadio(field){
	document.getElementById(field).checked = true;
}
function clearField(field){
	if (field.value == field.defaultValue) field.value = "";
	field.style.backgroundColor = "#FFF";
}
function popField(field){
	if (field.value == "") field.value = field.defaultValue;
}
function enableTextField(field,status){
	if(status=='off'){
		//document.getElementById(field).value = document.getElementById(field).defaultValue;
		document.getElementById(field).disabled = true;
	}else{
		document.getElementById(field).disabled = false;
	}
}
function openImage(image){
	var id = 'imageField_'+image;
	document.getElementById(id).style.display = "block";
	document.getElementById('blackBg').style.display = "block";
	document.getElementById('video').style.display = "none";
}
function closeImage(image){
	var id = 'imageField_'+image;
	document.getElementById(id).style.display = "none";
}
function closeAll(){
	document.getElementById('blackBg').style.display = "none";
	document.getElementById('imageField_ad1').style.display = "none";
	document.getElementById('imageField_ad2').style.display = "none";
	document.getElementById('imageField_ad3').style.display = "none";
	document.getElementById('imageField_ad4').style.display = "none";
	document.getElementById('video').style.display = "block";
}
function showErrField(field){
	field.style.backgroundColor = "#ECC";
	//alert(field.style.backgroundColor);
}

function processContribution(){
	var t=0;
	var form = document.getElementById('form');
	var fields = form.getElementsByTagName('input');
	var errFields = new Array();
	for(i=0;i<fields.length;i++){
		if(fields[i].type=="text"){
			if(fields[i].value==fields[i].defaultValue && fields[i].disabled==false){
				errFields[t] = fields[i];
				t++;
				//alert(fields[i].type+"\n"+fields[i].value+"\n"+errFields.length);
				showErrField(fields[i]);
			}else{
				//alert('preenchido');
			}
		}
		/*else if(fields[i].type=="radio"){
			
		}*/
	}
	if(errFields.length==0){
		form.submit();
	}else{
		alert("Please, complete all fields shown in red.")
	}
}
function nextStep(){
	var t=0;
	var form = document.getElementById('form');
	var fields = form.getElementsByTagName('input');
	var errFields = new Array();
	for(i=0;i<fields.length;i++){
		if(fields[i].type=="text"){
			if(fields[i].value==fields[i].defaultValue && fields[i].disabled==false){
				errFields[t] = fields[i];
				t++;
				//alert(fields[i].type+"\n"+fields[i].value+"\n"+errFields.length);
				showErrField(fields[i]);
			}else{
				//alert('preenchido');
			}
		}
		/*else if(fields[i].type=="radio"){
			
		}*/
	}
	if(errFields.length==0){
		form.submit();
	}else{
		alert("Please, answer all questions shown in red.")
	}
	/*
	if (){
		form.submit();
	}else{
		for(i=0; i<errFields.length; i++){
			showErrField(field[i]);
		}
	}
	*/
}


var text = new Array(
'Former Bronx Borough President Herman Badillo',
'Assemblyman Dov Hikind',
'The Richmond County Democratic Organization',
'Richmond County Democratic Chair John Gullino',
'Assemblymember Steven Cymbrowitz',
'Assemblymember Peter Abate',
'Assemblymember Janelle Heyer-Spencer',
'Councilmember Vincent Gentile',
'District Leader Michael Geller',
'District Leader Jacob Gold',
'District Leader Pearl Siegelman',
'District Leader Joseph Bova',
'District Leader Mary Rose Sati',
'District Leader James Wrynn',
'District Leader Delia Shack',
'New York City Patrolmen`s Benevolent Association',
'DC37 Retirees',
'Congressman Gary Ackerman',
'District Leader Leo Barille',
'Assemblyman James Brennan',
'Assemblywoman Ann Margaret Carrozza',
'Congressman Steve Israel',
'Former Assemblymember and Councilmember Steve Kaufman',
'Assemblyman Charles D. Lavine',
'District Leader Honey Miller',
'Hon. Steve Sanders',
'Former District Leader William Saunders',
'District Leader Martha Taylor',
'Assemblyman Mark Weprin'); 

var links = new Array('http://en.wikipedia.org/wiki/Herman_Badillo',
'http://assembly.state.ny.us/mem/?ad=048',
null,
null,
'http://en.wikipedia.org/wiki/Steven_Cymbrowitz',
null,
null,
'http://council.nyc.gov/d43/html/members/home.shtml',
null,
null,
null,
null,
null,
null,
null,
'http://nycpba.org/',
null,
'http://www.house.gov/ackerman/',
null,
'http://assembly.state.ny.us/mem/?ad=044',
'http://assembly.state.ny.us/mem/?ad=026',
'http://israel.house.gov/',
null,
'http://assembly.state.ny.us/mem/?ad=013',
null,
null,
null,
null,
'http://assembly.state.ny.us/mem/?ad=024');


var index = 0;
var i;
var text_to_frame = '';
var back = 1;
setInterval("changeText()", 3000); 

function changeText() { 
		if (index == text.length - 1) 
		{
			index = 0;
			back = 1;
		}
		else
		{
			index++;
			text_to_frame = '';
		} 
		for ( i = index; i < index + 6; i++)
		{
			if ( text[i] == undefined )
			{
				text[i] = text[back];
				links[i] = links[back];
				back++;
			}
			if ( links[i] == null)
			{
				text_to_frame = text_to_frame + "<p id='end_"+i+"'>"+text[i] + "</p>";
			}
			else
				{
					text_to_frame = text_to_frame +"<p id='end_"+i+"'><a href='"+links[i]+"'>"+text[i] + "</a></p>";
				}
			}
		document.getElementById("endorsements").innerHTML= text_to_frame; 
}

// ----------------------------------------------------------------------
// Javascript form validation routines.
// Author: Stephen Poley
//
// Simple routines to quickly pick up obvious typos.
// All validation routines return true if executed by an older browser:
// in this case validation must be left to the server.
//
// Update Jun 2005: discovered that reason IE wasn't setting focus was
// due to an IE timing bug. Added 0.1 sec delay to fix.
//
// Update Oct 2005: minor tidy-up: unused parameter removed
//
// Update Jun 2006: minor improvements to variable names and layout
// ----------------------------------------------------------------------

var nbsp = 160;		// non-breaking space char
var node_text = 3;	// DOM text node-type
var emptyString = /^\s*$/ ;
var global_valfield;	// retain valfield for timer thread

// --------------------------------------------
//                  trim
// Trim leading/trailing whitespace off string
// --------------------------------------------

function trim(str)
{
  return str.replace(/^\s+|\s+$/g, '');
}


// --------------------------------------------
//                  setfocus
// Delayed focus setting to get around IE bug
// --------------------------------------------

function setFocusDelayed()
{
  global_valfield.focus();
}

function setfocus(valfield)
{
  // save valfield in global variable so value retained when routine exits
  global_valfield = valfield;
  setTimeout( 'setFocusDelayed()', 100 );
}


// --------------------------------------------
//                  msg
// Display warn/error message in HTML element.
// commonCheck routine must have previously been called
// --------------------------------------------

function msg(fld,     // id of element to display message in
             msgtype, // class to give element ("warn" or "error")
             message) // string to display
{
  // setting an empty string can give problems if later set to a 
  // non-empty string, so ensure a space present. (For Mozilla and Opera one could 
  // simply use a space, but IE demands something more, like a non-breaking space.)
  var dispmessage;
  if (emptyString.test(message)) 
    dispmessage = String.fromCharCode(nbsp);    
  else  
    dispmessage = message;

  var elem = document.getElementById(fld);
  elem.firstChild.nodeValue = dispmessage;  
  
  elem.className = msgtype;   // set the CSS class to adjust appearance of message
}

// --------------------------------------------
//            commonCheck
// Common code for all validation routines to:
// (a) check for older / less-equipped browsers
// (b) check if empty fields are required
// Returns true (validation passed), 
//         false (validation failed) or 
//         proceed (don't know yet)
// --------------------------------------------

var proceed = 2;  

function commonCheck    (valfield,   // element to be validated
                         infofield,  // id of element to receive info/error msg
                         required)   // true if required
{
  if (!document.getElementById) 
    return true;  // not available on this browser - leave validation to the server
  var elem = document.getElementById(infofield);
  if (!elem.firstChild) return true;  // not available on this browser 
  if (elem.firstChild.nodeType != node_text) return true;  // infofield is wrong type of node  

  if (emptyString.test(valfield.value)) {
    if (required) {
      msg (infofield, "error", "ERROR: required");  
      setfocus(valfield);
      return false;
    }
    else {
      msg (infofield, "warn", "");   // OK
      return true;  
    }
  }
  return proceed;
}

// --------------------------------------------
//            validatePresent
// Validate if something has been entered
// Returns true if so 
// --------------------------------------------

function validatePresent(valfield,   // element to be validated
                         infofield ) // id of element to receive info/error msg
{
  var stat = commonCheck (valfield, infofield, true);
  if (stat != proceed) return stat;

  msg (infofield, "warn", "");  
  return true;
}

// --------------------------------------------
//               validateEmail
// Validate if e-mail address
// Returns true if so (and also if could not be executed because of old browser)
// --------------------------------------------

function validateEmail  (valfield,   // element to be validated
                         infofield,  // id of element to receive info/error msg
                         required)   // true if required
{
  var stat = commonCheck (valfield, infofield, required);
  if (stat != proceed) return stat;

  var tfld = trim(valfield.value);  // value of field with whitespace trimmed off
  var email = /^[^@]+@[^@.]+\.[^@]*\w\w$/  ;
  if (!email.test(tfld)) {
    msg (infofield, "error", "ERROR: not a valid e-mail address");
    setfocus(valfield);
    return false;
  }

  var email2 = /^[A-Za-z][\w.-]+@\w[\w.-]+\.[\w.-]*[A-Za-z][A-Za-z]$/  ;
  if (!email2.test(tfld)) 
    msg (infofield, "warn", "Unusual e-mail address - check if correct");
  else
    msg (infofield, "warn", "");
  return true;
}


// --------------------------------------------
//            validateTelnr
// Validate telephone number
// Returns true if so (and also if could not be executed because of old browser)
// Permits spaces, hyphens, brackets and leading +
// --------------------------------------------

function validateTelnr  (valfield,   // element to be validated
                         infofield,  // id of element to receive info/error msg
                         required)   // true if required
{
  var stat = commonCheck (valfield, infofield, required);
  if (stat != proceed) return stat;

  var tfld = trim(valfield.value);  // value of field with whitespace trimmed off
  var telnr = /^\+?[0-9 ()-]+[0-9]$/  ;
  if (!telnr.test(tfld)) {
    msg (infofield, "error", "ERROR: not a valid telephone number. Characters permitted are digits, space ()- and leading +");
    setfocus(valfield);
    return false;
  }

  var numdigits = 0;
  for (var j=0; j<tfld.length; j++)
    if (tfld.charAt(j)>='0' && tfld.charAt(j)<='9') numdigits++;

  if (numdigits<6) {
    msg (infofield, "error", "ERROR: " + numdigits + " digits - too short");
    setfocus(valfield);
    return false;
  }

  if (numdigits>14)
    msg (infofield, "warn", numdigits + " digits - check if correct");
  else { 
    if (numdigits<10)
      msg (infofield, "warn", "Only " + numdigits + " digits - check if correct");
    else
      msg (infofield, "warn", "");
  }
  return true;
}

// --------------------------------------------
//             validateAge
// Validate person's age
// Returns true if OK 
// --------------------------------------------

function validateAge    (valfield,   // element to be validated
                         infofield,  // id of element to receive info/error msg
                         required)   // true if required
{
  var stat = commonCheck (valfield, infofield, required);
  if (stat != proceed) return stat;

  var tfld = trim(valfield.value);
  var ageRE = /^[0-9]{1,3}$/
  if (!ageRE.test(tfld)) {
    msg (infofield, "error", "ERROR: not a valid age");
    setfocus(valfield);
    return false;
  }

  if (tfld>=200) {
    msg (infofield, "error", "ERROR: not a valid age");
    setfocus(valfield);
    return false;
  }

  if (tfld>110) msg (infofield, "warn", "Older than 110: check correct");
  else {
    if (tfld<7) msg (infofield, "warn", "Bit young for this, aren't you?");
    else        msg (infofield, "warn", "");
  }
  return true;
}
