jQuery(function($) {
  $('#changeLocationBtn').click(function(e) {
    e.preventDefault();
    $('#change-location-dialog').fadeIn('fast');
  });

  $('#change-location-dialog a.btn-close').click(function(e) {
    e.preventDefault();

    $('#change-location-dialog').fadeOut('fast');
  });

  $('#change-location-dialog a.btn-save').click(function(e) {
    e.preventDefault();
	
    var locationStr = $('#change-location-dialog input').val();
    var locationObj = null;

    var loc = {city: locationStr.split(',')[0]};
    var rest = $.trim(locationStr.split(",")[1]);
    if (rest.search(" ") === -1) {
      loc.region = '';
      loc.country = rest;
    }
    else {
      loc.region = rest.split(" ")[0];
      loc.country = rest.split(" ")[1];
    }

    var data = {
      action: "ou_theme_set_location",
      loc: loc
    };
	
	function formatTimestamp(timestamp) {
		var date = new Date(timestamp * 1000),
			formatted = '';
		
		console.log(date.toString());
		formatted = date.getHours() + ':' + date.getMinutes();
		return formatted;
	}
	
	$('<div class="saving" style="display:none">Saving....</div>').appendTo('#change-location-dialog').slideDown();
    $.post("/life/wp-admin/admin-ajax.php", data, function(result) {
	
	  if (result == 0) {
        alert("Error changing location");
      }
      else {
		if (result.status == 'ok') {
			var zmanim = result.zmanim;
			$('.candles .time').html(zmanim.candleLightingShabbos);
			$('.sunrise').html(zmanim.sunrise);
			$('.sunset').html(zmanim.sunset);
			$('#myLocation .city').html(zmanim.cn);
			$('#myLocation .region').html(zmanim.rc);
			$('#myLocation .country').html(zmanim.cc);
		}
      }
	  
	  $('#change-location-dialog a.btn-close').click();
	  $('#change-location-dialog .saving').remove();
	  $('#change-location-dialog input').val('');
    });
  });
  
  
var formInfoGenerator = {
	getFormAttributes: function(dataElement) {
		return $(dataElement).data('form-attr');
	},
	
	getFormFields: function(dataElement) {
		return $(dataElement).data('form-fields');
	},
	
	getKeywordFieldName: function(dataElement) {
		return $(dataElement).data('keyword-field-name');
	},
	
	generate: function(dataElement, form, fieldCont, keywordField) {
		var formAttributes = this.getFormAttributes(dataElement),
			formFields = this.getFormFields(dataElement),
			keywordFieldName = this.getKeywordFieldName(dataElement),
			hiddenFieldsHtml = '',
			hiddenFieldsTpl = '<input type="hidden" name="{name}" value="{value}" />';
		
		if (formFields) {
			// Adding fields
			$.each(formFields, function(index, val) {
				hiddenFieldsHtml += hiddenFieldsTpl + "\n";
				hiddenFieldsHtml = hiddenFieldsHtml.replace('{name}', index).replace('{value}', val);
			});
			
			if (fieldCont) {
				$(fieldCont).html(hiddenFieldsHtml);
			}
			else {
				form.prepend(hiddenFieldsHtml);
			}
		}
		
		if (formAttributes) {
			// Adding attributes
			$.each(formAttributes, function(index, val) {
				$(form).attr(index, val);
			});
		}
		
		// Adding custom placeholder
		var placeholder = $(dataElement).data('placeholder') || 'Search ' + $(dataElement).html();
		$(dataElement).parents('form').find('input[type=search]').attr('placeholder', placeholder);
		
		// Adding keyword field
		$(keywordField).attr('name', keywordFieldName);
	}
};

$('#multiSearchForm a').click(function(e){
	e.preventDefault();
	formInfoGenerator.generate(this, '#multiSearchForm', '#multiSearchForm .hidden-fields', '#multiSearchForm #s');
	
	$('#multiSearchForm a').removeClass('active');
	$(this).addClass('active');
});

$('#multiSearchForm').submit(function(){
	if ($('#s').attr('name') == 'hash') {
		var url = $(this).attr('action'),
			hash = $('#s').val();
		
		location.href = url + '#' + hash;
		
		return false;
	}
});
  
});

