$(function() {

// JavaScript Document//stupid IE7 bugfix - fixes disappearing inputs
	setTimeout('$("#FormLocatorWrapper").css("font-size","15px")',1);
	
	$('#MainEvent').IllyNews();
	$('#gallery').IllyNews();
	
	
    if ($('#LocationIndexForm').length > 0) {
        $("select, input:checkbox, input:radio, input:file").uniform();
    }

    $("#LocationZip").click(function(e) {
        if ($(this).val() == "Zip") {
            $(this).val("");
        }
    });
    $("#LocationCity").click(function(e) {
        if ($(this).val() == "City") {
            $(this).val("");
        }
    });

	// check for div containing location results, and scroll to it if it exists
	if ($("#LocationResults").length > 0) {
		$(window).load(function(){
			$('html, body').animate({
				scrollTop: $("#LocationResults").offset().top - 100
			}, 300);
		});      
	}

	if ($("#hideMap").length > 0) {
		$('#hideMap').click(function(){
			$('#location_map').slideToggle('slow');
			var text = $(this).html();
			if (text == "HIDE MAP") 
				$(this).html("SHOW MAP");
			else 
				$(this).html("HIDE MAP");
			return false;
		});
	}
    // END On doc Ready	
});


// jQuery Plugin Boilerplate, revisited
// A boilerplate for jumpstarting jQuery plugins development
// version 1.0, May 07th, 2011
// by Stefan Gabos
// inspired by http://starter.pixelgraphics.us/

(function($) {

    $.IllyNews = function(element, options) {

        var plugin = this;

        var defaults = {
            debug: true,
            onstart: function() {}
        }

        plugin.settings = {}

        var $element = $(element),
             element = element;

        plugin.init = function() {
            plugin.settings = $.extend({}, defaults, options)
            // code goes here
			
			
			
			//get the facebook Json Data
			switch(element.id){
				
				case "MainEvent":
					/*
					// Load first Facebook Event into the Main Event item
					$.when( getFBEvents() )
						.then(function(data){
							$.each(data, function(i, fb_event){
								$('#MainEvent').html('<h3>'+fb_event.name+'</h3><p>'+fb_event.desc+' - <strong>'+fb_event.date+'</strong> at '+
									fb_event.location+'</p><a href="http://www.facebook.com/event.php?eid='+fb_event.id+'" class="fb_rsvp" target="fb">RSVP</a>');	
								if ( i == 0 ) return false;
							});	
						});
					*/
					
				break;
				
				case "gallery":
					// Load Facebook Events into footer and give events button active state.
					$.when( getFBEvents() )
						.then(function(data){
							$('.AjaxLoader').hide();
							if(data.success == "true"){
								
								
								
								var items = [];
								$.each(data.data, function(i, fb_event){
									var lilast = "";
									if ( i == 2 ) lilast = " class='last'";
									items.push('<li'+lilast+'><h3>'+fb_event.name+'</h3><p>'+fb_event.desc+'<br /><br /><strong>'+fb_event.date+'</strong><br />'+
									fb_event.location+' - '+fb_event.city+', '+fb_event.state+'</p><a href="http://www.facebook.com/event.php?eid='+fb_event.id+'" class="fb_rsvp" target="fb">RSVP</a></li>');								
									if ( i == 2 ) return false;
								});
								$('.AjaxLoader').hide();
								
								$('<ul/>', {
									'class': 'fbevent-list',
									html: items.join('')
								  }).appendTo('#gallery');
							}else{
								
								$("<div/>",{
									'class': 'fbevent-list',
									html: '<h3>No events available. Check <a href="http://www.facebook.com/illyissimousa" target_blank>Facebook</a> for the latest activity.</h3>'
								}).appendTo('#gallery');
								
							}

						}).fail(function(){
						 	$("<div/>",{
								'class': 'fbevent-list',
								html: '<h3>No events available. Check <a href="http://www.facebook.com/illyissimousa" target_blank>Facebook</a> for the latest activity.</h3>'
							}).appendTo('#gallery');
					   });
					
					// Load Flickr Images into footer	
					$.when( getFlickr() )
						.then(function(fdata){
							
							$('.AjaxLoader').hide();
							var fitems = [];
							var counter2 = 0;
							$.each(fdata, function(x, flickr_img){
							
								
								//2 images
								
								//1 image
								
								//4 images
								
								
								/*if (  counter2 == 0){
									fitems.push('<div class="twoImg">');
								}else if (  counter2 == 2 ||  counter2 ==  7){
									fitems.push('<div class="singleImg">');
								}else if (  counter2 == 3){
									fitems.push('<div class="fourImg">');
								}
								
								
								
								fitems.push('<a href="'+flickr_img.link+'" target="flickr-img"><img src="'+flickr_img.imgsrc+'" /></a>');
								
								
								if (  counter2 == 1 || counter2 == 2  || counter2 ==6 || counter2 == 7){
									fitems.push('</div>');
								}
								
								
								
								if (  counter2 == 7){
									counter2=0;
								}
								else{counter2++;}*/
								
							
								if (  counter2 == 0 || counter2 == 3  || counter2 == 5 || counter2 == 7 || counter2 == 10){
									fitems.push('<div class="twoImg">');
								}else if (  counter2 == 2 ||  counter2 ==  9){
									fitems.push('<div class="singleImg">');
								}
								
								
								fitems.push('<a href="'+flickr_img.link+'" target="flickr-img"><img src="'+flickr_img.imgsrc+'" /></a>');
								
								
								if (  counter2 == 1 || counter2 == 2 || counter2 == 4 || counter2 == 6  || counter2 ==8 || counter2 == 9 || counter2 == 11){
									fitems.push('</div>');
								}
								
								
								
								if (  counter2 == 11){
									counter2=0;
								}
								else{counter2++;}
							
								
								
								
								
								
								
								
							});
							
							$('<div/>', {
								'class': 'flickr-list',
								html: fitems.join('')
							  }).appendTo('#gallery');
							  
							 
							  
						}).done(function(){
						 	consoleLog('DONE');
							$('.fourImg a:nth-child(3n+1)').addClass('first');
							 $('.twoImg a:nth-child(2n+2),.fourImg a:nth-child(3n+3)').addClass('last');
						 
					   }).fail(function(){
						 	
							$("<div/>",{
								'class': 'fbevent-list',
								html: '<h3>No photos available. Check <a href="http://www.flickr.com/groups/illyissimo" target_blank>flickr</a> for the latest activity.</h3>'
							}).appendTo('#gallery');
						 
					   });
				break;
				
			}
			
        }
		
		
		// Toggle the Facebook and Flickr Buttons in footer
		$('.fbevents').click(function(){
			$('.flickr-list').hide();
			$('.fbevent-list').fadeIn();
			$('.fbevents').addClass('active');
			$('.flickr').removeClass('active');
			return false;
			});
		$('.flickr').click(function(){
			$('.fbevent-list').hide();
			$('.flickr-list').fadeIn();
			$('.fbevents').removeClass('active');
			$('.flickr').addClass('active');
			return false;
			});
			

		var getFBEvents = function() {
			return $.ajax({
			  url: '/site/getFacebookEvents',
			  timeout:10000,
			  dataType:'json'
			});
		}
		
		var getFlickr = function() {
			return $.ajax({
			  url: '/site/getFlickr',
			  timeout:10000,
			  dataType:'json'
			});
		}

        plugin.foo_public_method = function() {
            // code goes here
        }


		var consoleLog =  function (content) {
            if ((window.console && window.console.log) && defaults.debug == true)
                console.log(content);
            //end log()
        }

        plugin.init();

    }

    $.fn.IllyNews = function(options) {

        return this.each(function() {
            var plugin = new $.IllyNews(this, options);
            $(this).data('IllyNews', plugin);
        });

    }

})(jQuery);


// Changes XML to JSON
function xmlToJson(xml) {
  
  // Create the return object
  var obj = {};

  if (xml.nodeType == 1) { // element
    // do attributes
    if (xml.attributes.length > 0) {
    obj["@attributes"] = {};
      for (var j = 0; j < xml.attributes.length; j++) {
        var attribute = xml.attributes.item(j);
        obj["@attributes"][attribute.nodeName] = attribute.nodeValue;
      }
    }
  } else if (xml.nodeType == 3) { // text
    obj = xml.nodeValue;
  }

  // do children
  if (xml.hasChildNodes()) {
    for(var i = 0; i < xml.childNodes.length; i++) {
      var item = xml.childNodes.item(i);
      var nodeName = item.nodeName;
      if (typeof(obj[nodeName]) == "undefined") {
        obj[nodeName] = xmlToJson(item);
      } else {
        if (typeof(obj[nodeName].length) == "undefined") {
          var old = obj[nodeName];
          obj[nodeName] = [];
          obj[nodeName].push(old);
        }
        obj[nodeName].push(xmlToJson(item));
      }
    }
  }
  return obj;
};

