/**
 * @author Sonic
 */

$(document).ready(function(){
	
		//GoogleMap.initialize();
	
		$('#banner_sub, #banner, #map').click(
				function(){
					document.location = 'winners.html';
				}
		).css({"cursor":"pointer"});
	
		$('.optionA').live("click",function(){
			$('#option_a').attr("checked", "checked");
		});
		$('.optionB').live("click",function(){
			$('#option_b').attr("checked", "checked");
		});
		$('.optionC').live("click",function(){
			$('#option_c').attr("checked", "checked");
		});
	
		$('#submit_vote').live("click",function(){
				
				error = 0;
				if($('#branch_drop').val()==""){
					alert('Please select a Branch');
					error = 1;
					return false;
				}
				if($('#option_a').attr('checked')==false && $('#option_b').attr('checked')==false && $('#option_c').attr('checked')==false){
					alert('Please select your Choosen Initiative');
					error = 1;
					return false;
				}
				if($('#email').val()==""){
					alert('Please enter your Email Address');
					error = 1;
					return false;
				}
				
				if(error == 0){
					//$('#vote_fns').html('<img src="img/loader.gif"/>');
					
					$.ajax({
					   type: "POST",
					   data: "action=addVote&email="+$('#email').val()+"&branch_id="+$('#branch_drop').val()+"&option="+$("input[type='radio']:checked").val()+"&key="+$('#key').val(),
					   url: "process.php",
					   success: function(msg, textStatus){
							
							if(msg.indexOf('success')>-1){
								//success
								$('#votenow').hide();
								$('#latestnews').hide();
								$('#thankyou').show();
							}else{
								//problem see msg.
								$('#vote_fns').html(msg);
							}
					   }
					});
					
				}
				
			}
		);
		
		
		$('#countysms2_drop').bind($.browser.msie? 'propertychange': 'change',
			function(){
				val = $(this).val();
				if(val!=""){
					
					$.ajax({
					   type: "POST",
					   data: "action=getBranchesByCounty&id="+val,
					   url: "process.php",
					   success: function(msg, textStatus){
							$('#branchsms2_drop').html(msg);
							$('#branchsms2_drop').focus();
					   }
					 });
					
				}else{
					$.ajax({
					   type: "POST",
					   data: "action=getBranchesByCounty",
					   url: "process.php",
					   success: function(msg, textStatus){
							$('#branchsms2_drop').html(msg);
							$('#branchsms2_drop').focus();
					   }
					 });
				}
			}
		);
		
		$('#branchsms2_drop').bind($.browser.msie? 'propertychange': 'change',
			function(){
				val = $(this).val();
				if(val!=""){
					
					$.ajax({
					   type: "POST",
					   data: "action=getCharityByBranch&id="+val,
					   url: "process.php",
					   success: function(msg, textStatus){
							
							output = msg.split('|');
							
							$('#votecontent2_sms').html(output[1]);
					   
					   }
					 });
					
				}
			}
		);
		
		
		$('#countysms_drop').bind($.browser.msie? 'propertychange': 'change',
			function(){
				val = $(this).val();
				if(val!=""){
					
					$.ajax({
					   type: "POST",
					   data: "action=getBranchesByCounty&id="+val,
					   url: "process.php",
					   success: function(msg, textStatus){
							$('#branchsms_drop').html(msg);
							$('#branchsms_drop').focus();
					   }
					 });
					
				}else{
					$.ajax({
					   type: "POST",
					   data: "action=getBranchesByCounty",
					   url: "process.php",
					   success: function(msg, textStatus){
							$('#branchsms_drop').html(msg);
							$('#branchsms_drop').focus();
					   }
					 });
				}
			}
		);
		
		$('#branchsms_drop').bind($.browser.msie? 'propertychange': 'change',
			function(){
				val = $(this).val();
				if(val!=""){
					
					$.ajax({
					   type: "POST",
					   data: "action=getCharityByBranch&id="+val,
					   url: "process.php",
					   success: function(msg, textStatus){
							
							output = msg.split('|');
							
							$('#votecontent_sms').html(output[1]);
					   
					   }
					 });
					
				}
			}
		);
		
		
		$('#county_drop').bind($.browser.msie? 'propertychange': 'change',
			function(){
				val = $(this).val();
				if(val!=""){
					
					$.ajax({
					   type: "POST",
					   data: "action=getBranchesByCounty&id="+val,
					   url: "process.php",
					   success: function(msg, textStatus){
							$('#branch_drop').html(msg);
							$('#branch_drop').focus();
					   }
					 });
					
				}else{
					$.ajax({
					   type: "POST",
					   data: "action=getBranchesByCounty",
					   url: "process.php",
					   success: function(msg, textStatus){
							$('#branch_drop').html(msg);
							$('#branch_drop').focus();
					   }
					 });
				}
			}
		);
		
		$('#branch_drop').bind($.browser.msie? 'propertychange': 'change',
			function(){
				val = $(this).val();
				if(val!=""){
					
					$.ajax({
					   type: "POST",
					   data: "action=getCharityByBranch&id="+val,
					   url: "process.php",
					   contentType: "application/x-www-form-urlencoded;charset=utf-8",
					   success: function(msg, textStatus){
							
							output = msg.split('|');
							
							$('#votecontent').html(output[0]);
							$('#votecontent_sms').html(output[1]);
					   
					   }
					 });
					
				}
			}
		);
		
		
		
		$('#searchfield').focus(function(){
			if($(this).val()=="Search Site"){
				$(this).val('');
			}
		});
		
		
		$('#searchfield').blur(function(){
			if($(this).val()==""){
				$(this).val('Search Site');
			}
		});
		
		$('.mainmenu_item').hover(
			function(){
				if ($(this).attr('id') != 'active') {
					//$(this).hide();
					//$(this).fadeIn('fast');
					$(this).css({
						"background-position": "0px -40px"
					});
				}
			},
			function(){
				
				if ($(this).attr('id') != 'active') {
					$(this).css({
						"background-position": "0px 0px"
					});
				}
			}			
		);
		
		//activate active item in menu
		$('.mainmenu_item').each(
			function(i){
				if($(this).attr('id')=='active'){
					$(this).css({"background-position":"0px -40px"});
				}
			}
		);
});

