var slideshow;

$(document).ready(function(){

	$("ol.numbers")
		.each(function() {
			$('li',this).each(function(i) { 
				var i = i+1;
				var i = i.toString();
				var a = i.charAt(0);
				var b = i.charAt(1);
				if (i.length == 1) {
					$(this).prepend('<div class="number"><img class="digit1" src="' + baseURL + 'img/numbers/li' + a + '.gif" alt="' + a + '" /></div>');
				}
				else {
					$(this).prepend('<div class="number"><img class="digit1" src="' + baseURL + 'img/numbers/li' + a + '.gif" alt="' + a + '" /><img class="digit2" src="' + baseURL + 'img/numbers/li' + b + '.gif" alt="' + b + '" /></div>');
				}
			});
			var z =	$(this).children('li').size();
			var z = z.toString();
			if ( z.length == 1) {
				$(this).addClass('single');
			} else {
				$(this).addClass('double');
			}
		});
	
	$('#homeHead ul li a').livequery(function(){
		$(this).click(function(){
			clearTimeout(slideshow);
			slideshow = setInterval( "slideSwitch()", 7000 );
			var which = "#"+$(this).attr('title');
			$('#homeHead dl').removeClass('current');
			$(which).addClass('current');
			return false;
		});
	});

	/*---Physicians Home Head---*/
	$('#physiciansHomeBox1').click(function(){
		$('#physiciansHomeHead1').addClass('current');
		$('#physiciansHomeHead2').removeClass('current'); $('#physiciansHomeHead3').removeClass('current');
	});
	$('#physiciansHomeBox2').click(function(){
		$('#physiciansHomeHead2').addClass('current');
		$('#physiciansHomeHead1').removeClass('current'); $('#physiciansHomeHead3').removeClass('current');
	});
	$('#physiciansHomeBox3').click(function(){
		$('#physiciansHomeHead3').addClass('current');
		$('#physiciansHomeHead1').removeClass('current'); $('#physiciansHomeHead2').removeClass('current');
	});
	
	
	/* Member site home page */
	$('#membersHomeBox1').click(function(){
		$('#membersHomeHead1').addClass('current');
		$('#membersHomeHead2').removeClass('current'); $('#membersHomeHead3').removeClass('current');
	});
	$('#membersHomeBox2').click(function(){
		$('#membersHomeHead2').addClass('current');
		$('#membersHomeHead1').removeClass('current'); $('#membersHomeHead3').removeClass('current');
	});
	$('#membersHomeBox3').click(function(){
		$('#membersHomeHead3').addClass('current');
		$('#membersHomeHead1').removeClass('current'); $('#membersHomeHead2').removeClass('current');
	});
	

	/*---Lightboxes---*/
	var backPatient = null;  // variable to keep track of which patient story you just came from

	$('#main .stories.lightbox>li').click(function(){
		openLB($(this).children('div.lb-content'));
		//$('body').bind('scroll', function(){return false;});
		return false;
	});
	
	$('li#contact').click(function(){
		openLBContact($(this).children('div.lb-content'));
		//$('body').bind('scroll', function(){return false;});
		return false;
	});
	
	$('div.lb-content a.close').click(function(){
		closeLB();
		return false;
	});
	
	$('#main .lb-content ul li .black.docK').click(function(){
		backPatient = $(this).parent().parent().parent();
		closeLB(backPatient);
		openLB($('div#drk-content'));
		return false;
	});
	
	$('#main .lb-content ul li .black.docG').click(function(){
		backPatient = $(this).parent().parent().parent();
		closeLB(backPatient);
		openLB($('div#drg-content'));
		return false;
	});
	
	$('div.doc-lb a.black').click(function(){
		if (backPatient) {
			closeLB($(this).parent().parent().parent());
			openLB(backPatient);
			backPatient = null;
		} else {closeLB();}
		return false;
	});
	
	
	$("#homeZip, #findCenter, #searchText, #FinderZip").focus(function() {
		if( this.value == this.defaultValue ) {
			this.value = "";
			$(this).css('color','black');
		}
	}).blur(function() {
		if( !this.value.length ) {
			this.value = this.defaultValue;
			$(this).css('color','')
		}
	});
	$('#homeHead dl dd').append($(
		'<ul>'+
			'<li id="homeLink4"><a href="#homeHead4" title="homeHead4">Home Feature 4</a></li>'+
			'<li id="homeLink3"><a href="#homeHead3" title="homeHead3">Home Feature 3</a></li>'+
			'<li id="homeLink2"><a href="#homeHead2" title="homeHead2">Home Feature 2</a></li>'+
			'<li id="homeLink1"><a href="#homeHead1" title="homeHead1">Home Feature 1</a></li>'+
			'<li id="homeLink5"><a href="#homeHead5" title="homeHead5">Home Feature 5</a></li>'+
		'</ul>'
	));
    $('#homeHead4').css({opacity: 0.0})
        .addClass('current')
        .animate({opacity: 1.0}, 1000, function() {
            slideshow = setInterval( "slideSwitch()", 7000 );
    });
	$('#ContactHeardAbout').change(function(){
		var otherVal = $(this).val();
		if ( otherVal == 'Other' ) { 
			$(this).parent('div.input')
				.append('<input id="ContactHeardAboutOther" type="text" value="" name="data[Contact][other]" />');
		} else {
			$('#ContactHeardAboutOther').remove();
		}
	});
});

function openLB(lb)  //expects to be passed a div that is the container of the lightbox to be opened
{	
	window.scrollTo(0,150);
	var cw = $('body').width();
	var ch = $('body').height();
	var lm = 0 - ((cw-985)/2);
	lb.css('display','block');

	//$('.lb-overlay').css('display','block').css('width',cw).css('height',ch).css('left',lm);
}

function openLBContact(lb)  //expects to be passed a div that is the container of the lightbox to be opened
{	
	var cw = $('body').width();
	var ch = $('body').height();
	var lm = 0 - ((cw-985)/2);
	lb.css('display','block');

	//$('.lb-overlay').css('display','block').css('width',cw).css('height',ch).css('left',lm);
}

function closeLB(lb)  //will close a specific lightbox, or make sure that all lightbox stuff is closed
{
	if (lb) {lb.css('display', 'none');}
	else {
		$('.lb-overlay').css('display','none');
		$('.lb-content').css('display', 'none');
	}
}


/* Thanks to Jon Raasch and his blog for the following slideshow script idea
 * http://jonraasch.com/blog/a-simple-jquery-slideshow */

function slideSwitch() {
    var $active = $('#homeHead dl.current');

    if ( $active.length == 0 ) $active = $('#homeHead dl:last');

    var $next = $active.next().length ? $active.next() : $('#homeHead dl:first');

    $active.addClass('last-active');

    $next.css({opacity: 0.0})
        .addClass('current')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('current last-active');
        });
}
/*********************************************************
Crteated By          Raju Kumar
Date                 04-June-2009
Purpose              To remove the white space
Company              Intelegencia 
**********************************************************/
function trim(str)
{
   return str.replace(/^\s+|\s+$/g,'');
}
/*********************************************************
Crteated By          Raju Kumar
Date                 04-June-2009
Purpose              To validate the zip code
Company              Intelegencia 
**********************************************************/
function validateZip()
{
	
	var zipCode;
	//to hide the error div
	 $("#zipError").hide();
	var error="";
	if(document.getElementById("FinderZip")!=null)
	{
			var zipCode=document.getElementById("FinderZip");
	}
	else
	{
		if(document.getElementById("homeZip")!=null)
		{
			var zipCode=document.getElementById("homeZip");
		}
		if(document.getElementById("findCenter")!=null)
		{
			var zipCode=document.getElementById("findCenter");
		}
	}
	//var zipCode=document.getElementById("FinderZip");	
	//remove the white space and check if it is numeric
	if(trim(zipCode.value)=="")	{
	 //alert("Zip code can not be blank");
	 document.getElementById("zipError").innerHTML="Zip Code can not be blank";	
	 zipCode.focus();		
	 $("#zipError").show();		 
	 return false;
	}
	else{
			
			if(/[^0-9]/.test(zipCode.value)){	
			   //alert("Zip code should be numeric value");
				document.getElementById("zipError").innerHTML="Zip Code should be a numeric value";	
				$("#zipError").show();
				zipCode.focus();
				return false;
			}
			if(zipCode.value.length!=5){
				//alert("Zip code should be less than five characters");
				document.getElementById("zipError").innerHTML="Zip Code should have exactly five digits";	
				$("#zipError").show();
				zipCode.focus();
				//$("#zipError").hide(5000);
				return false;
			}
		
	}
	
	//check if submited from center_locator.ctp
	if(document.forms['FindAddForm']!=null){
		document.forms['FindAddForm'].submit();		
	}
	//check if submited from find center.ctp
	if(document.forms['ContactAddForm']!=null)
	{
	 document.forms['ContactAddForm'].submit();
	}
}

function submitForm()
{
	var required = {
	"ContactDoctorName": "Doctor name cannot be left blank",
	"ContactAddress1": "Address cannot be left blank",
	"ContactCity": "City cannot be left blank",
	"ContactCounty": "County name cannot be left blank",
	"ContactState": "State name cannot be left blank",
	"ContactZip": "Zip cannot be left blank",
	"ContactOfficePhone": "Office Phone cannot be left blank",
	"ContactEmail": "Email cannot be left blank",
	"ContactSpecialty": "Speciality cannot be left blank",
	"ContactHeardAbout": "Where did you hear about us?"
	};
	for (var i in required) 
	{
		var el = document.getElementById(i);
		if (!el.value) 
		{
			alert(required[i]);
			el.focus();	
			return false;
		}
	}
	var email=document.getElementById ('ContactEmail').value;
	if(email.length &&(email.indexOf("@")==0||email.indexOf("@")==-1||email.indexOf(".")==-1||email.indexOf(".")==0))
    {
        alert("Invalid E-Mail address.\n"+"Valid Format: email@domainname.com");
        document.getElementById('ContactEmail').value='';
		document.getElementById('ContactEmail').focus();
        return false;
    }
	document.forms['ContactAddForm'].submit();
	return false;
}

