$(document).ready(function(){
    var closed = 0;

	
	 $('a.get-rules').fancybox({
 'onStart': function(){$('div#rules').css('display', 'block'); $('div#forms').css('display', 'none');},
 'onClosed': function(){$('div#rules').css('display', 'none'); $('div#forms').css('display', 'none');},
 'autoDimensions': false,
 'width' : 600,
 'height': 'auto'

 });

 $('a.enter').fancybox({
 'onStart': function(){$('div#forms').css('display', 'block'); $('div#rules').css('display', 'none');},
 'onClosed': function(){
	$('div#forms').css('display', 'none');
	$('div#rules').css('display', 'none'); 
	if(closed=== 1){
		$('fieldset').html('');
		$('fieldset').html('<p><label for=first-name>First Name:</label><input name=first-name class=required id=name></p><p><label for=last-name>Last Name:</label><input name=last-name class=required id=name2></p><p><label for=address>Address:</label><input name=address class=required id=address></p><p><label for=city>City:</label><input name=city class=required id=city></p><p><label for=state>State:</label><select name=state id=state><option value=select selected>Select a State<option value=AL>Alabama<option value=AK>Alaska<option value=AZ>Arizona<option value=AR>Arkansas<option value=CA>California<option value=CO>Colorado<option value=CT>Connecticut<option value=DE>Delaware<option value=DC>District Of Columbia<option value=FL>Florida<option value=GA>Georgia<option value=HI>Hawaii<option value=ID>Idaho<option value=IL>Illinois<option value=IN>Indiana<option value=IA>Iowa<option value=KS>Kansas<option value=KY>Kentucky<option value=LA>Louisiana<option value=ME>Maine<option value=MD>Maryland<option value=MA>Massachusetts<option value=MI>Michigan<option value=MN>Minnesota<option value=MS>Mississippi<option value=MO>Missouri<option value=MT>Montana<option value=NE>Nebraska<option value=NV>Nevada<option value=NH>New Hampshire<option value=NJ>New Jersey<option value=NM>New Mexico<option value=NY>New York<option value=NC>North Carolina<option value=ND>North Dakota<option value=OH>Ohio<option value=OK>Oklahoma<option value=OR>Oregon<option value=PA>Pennsylvania<option value=RI>Rhode Island<option value=SC>South Carolina<option value=SD>South Dakota<option value=TN>Tennessee<option value=TX>Texas<option value=UT>Utah<option value=VT>Vermont<option value=VA>Virginia<option value=WA>Washington<option value=WV>West Virginia<option value=WI>Wisconsin<option value=WY>Wyoming</select></p><p><label for=zip>Zip:</label><input name=zip class=required id=zip></p><p><label for=email>Email:</label><input name=email class="required email" id=email></p><p><label for=email2>Confirm Your Email:</label><input name=email2 class="required email" id=email2></p><p class=checks><input checked type=checkbox name=agree id=agree class=required><label for=agree>YES! I\'d like to be informed of special contests and offers from Outdoor Gear Give Away and their partners. I know I can unsubscribe at any time by clicking the unsubscribe link in any email.<a class=get-rules id=get-rules href="#rules">Official Rules</a></label></p><p class=checks><input checked type=checkbox name=age id=age class=required><label for=age>YES! I am at least 21 years of age, as of March 31, 2011, with proof of residency in the 48 contiguous states of the United States and the District of Columbia, residents of Alaska, Hawaii and any U.S. territories are not eligible.<a class=get-rules id=get-rules href="#rules">Official Rules</a></label></p><p><a href="#" id=submit>Submit<span class=hover style="opacity: 0"></span></a></p>');
	}
 },
'autoDimensions' : false,
 'width': 750,
 'height': 615
 }); 
	$('#container').fadeIn(400);
	var the_class;
	
	$('.showprize').click(function(){
		the_class = $(this).attr('id');
		the_class = the_class[the_class.length - 1];
		
	
		$('.prizes').hide();
		$('.'+$(this).attr('id')).show("slide", { direction: "left" }, 1000);
		
		if($(this).attr('id') === 'grandprize4'){
			$('div#container').css('background', 'url(img/yamaha_background1.jpg) no-repeat');
			$('div#container').hide();
			$('#container').fadeIn(1000);
		}
		else if($(this).attr('id') === 'grandprize5'){
			$('div#container').css('background', 'url(img/yamaha_background1.jpg) no-repeat');
			$('div#container').hide();
			$('#container').fadeIn(1000);
		}
		else{
			$('div#container').css('background', 'url(img/yamaha_background1.jpg) no-repeat');
			$('div#container').hide();
			$('#container').fadeIn(1000);
		}
		
		
	});
	

   
       
      $('.hover').hover(function () {
      $(this).css('opacity', 0);
        $(this).stop().fadeTo(500, 1);
      }, function () {
        $(this).stop().fadeTo(500, 0);
      });
   
	

	$('input').select(function(){
		$(this).css('color', 'black');
	});
	
	$('a#submit').live('click',function(){
		$('label').css('color', 'white');
		$('input').css('color', 'black');
		
		//$('input[name=used_email]').val('')
		
		var required = check_required();
		if(required === ''){
		var contests = 'false';
		if($('input#agree').is(':checked') === true){
			contests = 'true';
		}
		
		var the_address = $('input[name=address]').val();
		the_address = the_address.replace('#','Number ');
		the_address = the_address.replace(/,/g, ' ');
		
		var geocode = '';
		var geo = new GClientGeocoder();
		geo.getLocations($('input[name=address]').val()+' '+$('input[name=city]').val()+', '+$('select[name=state] :selected').val()+' '+$('input[name=zip]').val(),function(result){
			 geocode = result.Placemark[0].Point.coordinates;
			 lat = geocode[1];
			 long = geocode[0];
			$.ajax({
				type: "POST",
				url:  "submit.php?first-name="+$('input[name=first-name]').val().replace(/,/g, ' ')+"&last-name="+$('input[name=last-name]').val().replace(/,/g, ' ')+"&address="+the_address+"&city="+$('input[name=city]').val().replace(/,/g, ' ')+"&state="+$('select[name=state]').val()+"&zip="+$('input[name=zip]').val()+"&email="+$('input[name=email]').val()+"&contests="+contests+'&lat='+lat+"&long="+long+"&referer="+$('#referer').val(),
				success: function(msg){
					closed = 1;
					
					$('fieldset').html('');
					$('fieldset').append('<img style="margin-left: 70px;" src="img/checkmark.png" alt="Thanks for Entering!"/>');
					$('fieldset').append('<h2 style="font-size:27px;">Thanks For Entering!</h2>');
					$('fieldset').append('<p style="font-size: 18px; font-weight: bold; margin-bottom: 15px;" class="thanks">We will email you updates as new prizes are added, and monthly winners are drawn.</p>');
					$('fieldset').append('<p  style="color:#dfae3e; font-size:16px;" class="thanks">Add us as a friend on Facebook & Twitter for more chances to win!</p>');
					$('fieldset').append('<a href="http://www.facebook.com/pages/Outdoor-Gear-Give-Away/108828559169667" target="_blank"><img alt="Facebook" src="img/facebook.png"></a> <a href="http://twitter.com/OutdoorGearGA" target="_blank"><img alt="Twitter" src="img/twitter.png"></a>');
					
				}			
			});
			
		}	 );
		}else{
		
			fixers = required.split('^');
			the_length = fixers.length -1;
					
					for(var i = 0; i < the_length; i++){
							if(fixers[i] === 'notzip'){
								$('input[name=zip]').val('Not Valid Zip').css('color','red');
							}else if(fixers[i] === 'emailinvalid'){
								$('input[name=email]').val('Not a valid email').css('color','red');		
							}else if(fixers[i] === 'email2'){
								$('input[name=email2]').val('Must match previous email').css('color','red');					
							}else if(fixers[i] === 'emailused'){
								$('input[name=email]').val('Email already signed up').css('color','red');					
							}else{
								$('input[name='+fixers[i]+']').val('Required Field').css('color','red');
								$('label[for='+fixers[i]+']').css('color', '#d1ab46');
							}			
					}
					
					$('input[name='+fixers[0]+']').focus();
					$('input[name='+fixers[0]+']').select();
					
		
		}
	});

function check_required(){
	
	var required = '';
	var filter = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
	
	
	//$('input[name=used_email]').val('')
	
	
	var first_name = $('input[name=first-name]').val();
	var last_name = $('input[name=last-name]').val();
	var address = $('input[name=address]').val();
	var city = $('input[name=city]').val();
	var state = $('select[name=state] :selected').val();
	var zip = $('input[name=zip]').val();
	var email = $('input[name=email]').val();
	var email2 = $('input[name=email]').val();
	
	if(first_name ==='' || first_name ==='Required Field' ){
		required += 'first-name^';
	}
	
	if(last_name ==='' || last_name ==='Required Field'){
		required += 'last-name^';
	}
	
	
	if(address ==='' || address ==='Required Field'){
		required += 'address^';
	}

	
	if(city ==='' || city ==='Required Field'){
		required += 'city^';
	}
	
	
	if(state === 'select'){
		required += 'state^';
	}
	
	
	if(zip ==='' || zip ==='Required Field'){
		required += 'zip^';
	}
	else if(isNumeric(zip) === false || zip.length < 5 ){
		required += 'notzip^';
	}
	
	if(email === ''){
		required += 'email^';
	}else{
	
		if(filter.test(email) === false){
			required += 'emailinvalid^';
		}
		
		$.ajax({
				type: "POST",
				url: "checkEmail.php?email="+email+" ",
				async: false,
				success: function(msg){
					//alert(msg)
						
					if(msg !== ''){
						required += 'emailused^';
					}
				}
		});
		
	}
	
	
	
if(!$('input[name=age]').is(':checked')){
		required += 'age^';
	}


	
	if(email2 !== email){

		filter.test(email);
		
		required += 'email2^';
				
	}
	
		
	
	
	return required;
}




function isNumeric(sText){
   var ValidChars = "0123456789.-";
   var IsNumber=true;
   var Char;

   for (i = 0; i < sText.length && IsNumber === true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) === -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
   }


if(document.location.pathname === '/stats.php'){
//Sort table for Stats
$("#submissions").tablesorter(); 


    var heatIcon = new GIcon();
      heatIcon.image = "http://www.outdoorgeargiveaway.com/google-heat.png";
      heatIcon.iconSize = new GSize(20, 20);
      heatIcon.shadow = "http://www.outdoorgeargiveaway.com/google-heat.png";
      heatIcon.shadowSize = new GSize(0, 0);
      heatIcon.iconAnchor = new GPoint(0, 0);
      heatIcon.infoWindowAnchor = new GPoint(5, 2);
      heatIcon.transparent = "http://www.outdoorgeargiveaway.com/google-heat.png";
  markerOptions = { icon:heatIcon };

var location ='';
var marker = '';

var map = new GMap2(document.getElementById("map"));
      map.addControl(new GLargeMapControl());
      map.addControl(new GMapTypeControl());
      map.setCenter(new GLatLng(37.704, -97.325), 4);

    
    
    
     var geo = new GClientGeocoder();
	var geocode = '';
    var splitLoc = '';
    var splitLat = '';
    //var mgr = new MarkerManager(map);
    var i = 0;
    var marker_list = [];
    $.ajax({
		type: "POST",
		url:  "../getListings.php",
		success: function(msg){
			splitLoc = msg.split('^');
			
			var splitLength = splitLoc.length;
			splitLength--;
			for(var j = 0; j < 200; j++){
			
				
			} 
			for(i = 0; i < splitLength; i++){
			
				splitLat = splitLoc[i].split('~');
			   	location = new GLatLng(splitLat[0], splitLat[1]);
			   	marker = new GMarker(location, markerOptions);
			   //	map.addOverlay(marker)	
			   	marker_list.push(marker);
			}
			var markerCluster = new MarkerClusterer(map,marker_list);
				
			
		}			
			}); 
    

    
   }
    


});

