jQuery.noConflict();

var Dateform = {
   DELETE_MSG: "Deleting this date is permanent.\n\nClick 'OK' to continue with delete.",
   do_submit: function(form) {
      // calling form.submit seems to override the onsubmit handler...
      $(form).onsubmit();
   },
   toggle_complete: function( cb ) {
      Element.add_class( cb.form.id, 'loading' );
      this.do_submit( cb.form.id );
   },
   delete_date: function( uri, elem ) {
      if( confirm( this.DELETE_MSG ) ) {
         var dom = $(elem);
         var opts = {
            onComplete: function(data) {
               if( data.responseText == "SUCCEED" ) {
                  new Effect.Fade(dom);
               } else {
                  alert('An error occured trying to delete a date.\n\n'+ data.responseText );
               }
            }
         }
         // Send request to server...
         var req = new Ajax.Request(uri, opts);
      }
   }
};

function GetPage(intPage) {
	document.pageFrm.PageNumber.value = intPage;
	document.pageFrm.submit();
}	

function changeColoredTab (tabname) {
	document.getElementById('tab_main').style.display = 'none';
    document.getElementById('tab_categories').style.display = 'none';  
	document.getElementById('tab_discounts').style.display = 'none';
	document.getElementById('tab_schedule').style.display = 'none';	
	document.getElementById('tab_info').style.display = 'none';		
	document.getElementById('tab_image').style.display = 'none';		
	document.getElementById('tab_'+tabname).style.display = 'block';
}

Ajax.Responders.register({
  onCreate: function() {
    if($('busy') && Ajax.activeRequestCount>0)
      Effect.Appear('busy',{duration:0.5,queue:'end'});
  },
  onComplete: function() {
    if($('busy') && Ajax.activeRequestCount==0)
      Effect.Fade('busy',{duration:0.5,queue:'end'});
  }
});
							
function change(id, newClass) {
	identity=document.getElementById(id);
	identity.className=newClass;
}		

function checkedAll (id, checked) {
	var el = document.getElementById(id);
	for (var i = 0; i < el.elements.length; i++) {
  		el.elements[i].checked = checked;
	}
}

Event.observe(window, 'load', init, false);
function init() {
	if ($('merge_with_venue_id')) {
		Event.observe('merge_with_venue_id', 'change', getVenue);
	}
}

function getVenue(venue) {
	new Ajax.Request('/venue/for_merge/' + $F($('merge_with_venue_id')), {
		method: 'get',
		onSuccess: function(value) {
			$('venue_info').update(value.responseText);
		}
	});	
}
function getSelectionId(text, li) {
	new Ajax.Request('/event/venue_info/' + li.id, {
		method: 'get',
		onSuccess: function(value) {
			$('auto_info').update(value.responseText);
		}
	});
}

var coords = {
	x: null,
	y: null,
	x2: null,
	y2: null,
	w: null,
	h: null
}

function setCoords(c) {
	coords.x = c.x;
	coords.y = c.y;
	coords.x2 = c.x2;
	coords.y2 = c.y2;
	coords.w = c.w;
	coords.h = c.h;
}

function makeTheCrop() {
	var controller = location.href.split("/")[3];
	var id = location.href.split("/")[5];
	var url = '';
	if (controller == 'event') {
		url = '/event/crop_square_image/' + id;
	} else {
		url = '/organization/crop_square_image/' + id;
	}

	new Ajax.Request(url, {
		parameters: {x : coords.x, y : coords.y, w : coords.w, h : coords.h },
		onSuccess: function(transport) {
			src = jQuery("#square_img").attr("src");
			jQuery("#square_img").attr("src", src + '?' + Math.random());
		}
	})
}

jQuery(document).ready(function() {
	jQuery('#full_size_image').Jcrop({
		setSelect: [ 20, 120, 120, 120 ],
		onChange: setCoords,
		// onChange: showPreview,
		// onSelect: showPreview,
		aspectRatio: 1
	});
});

function showPreview(coords)
{
	var rx = 100 / coords.w;
	var ry = 100 / coords.h;

	$('#preview').css({
		width: Math.round(rx * 500) + 'px',
		height: Math.round(ry * 370) + 'px',
		marginLeft: '-' + Math.round(rx * coords.x) + 'px',
		marginTop: '-' + Math.round(ry * coords.y) + 'px'
	});
};

