// itinerary related
function i_add(sid) {
	// perform ajax request and update i_status
	var url = '?ajax=i_add&id='+sid;
	new Ajax(url, {
			 method: 'get',
			 update: $('i_status')
	}).request();
	return false;
}

function i_list_remove(sid) {
	// perform ajax request and update i_status
	var url = '?ajax=i_list_remove&id='+sid;
	new Ajax(url, {
			 method: 'get',
			 update: $('i_listing')
	}).request();
	return false;
}

function i_remove(sid) {
	// perform ajax request and update i_status
	var url = '?ajax=i_remove&id='+sid;
	new Ajax(url, {
			 method: 'get',
			 update: $('i_status')
	}).request();
	return false;
}

function i_view() {
	// redirect the user to the view page
	document.location = '/page/itinerary_view';
	return false;
	
	// display a listing of stores to the user
	var url = '?ajax=i_view';
	new Ajax(url, {
			 method: 'get',
			 update: $('i_status')
	}).request();
	return false;
}

function i_list_clear() {
	// perform ajax request and update i_status
	var url = '?ajax=i_list_clear';
	new Ajax(url, {
			 method: 'get',
			 update: $('i_listing'),
			 onComplete: document.location = '/page/wine_discovery'
	}).request();
	return false;
}

function i_print() {
	if ( confirm('Press OK to print this page') )
		window.print();
	return false;
}

// map popup
function map_popup(URL) {
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=1,width=800,height=600,left = 400,top = 192');");
}

// store image changes
function storeimage(id, src) {
	var target = $(id);
	if ( target ) target.setStyle('background-image','url('+src+')');
	return false;
}

// media pagination
function media_page(id, page) {
	var url = '?ajax=media_page&id='+id+'&page='+page;
	new Ajax(url, {
			 method: 'get',
			 update: $('media'+id)
	}).request();
}

// media pagination
function subsite_gallery(id, page) {
	var url = '?ajax=subsite_gallery&id='+id+'&pagenum='+page;
	new Ajax(url, {
			 method: 'get',
			 update: $('subsite_gallery'+id)
	}).request();
}

// css skinable scrollbar
function makeScrollbar(content,scrollbar,handle,horizontal,ignoreMouse){
	var steps = (horizontal?(content.getSize().scrollSize.x - content.getSize().size.x):(content.getSize().scrollSize.y - content.getSize().size.y))
	var slider = new Slider(scrollbar, handle, {	
		steps: steps,
		mode: (horizontal?'horizontal':'vertical'),
		onChange: function(step){
			// Scrolls the content element in x or y direction.
			var x = (horizontal?step:0);
			var y = (horizontal?0:step);
			content.scrollTo(x,y);
		}
	}).set(0);
	if( !(ignoreMouse) ){
		// Scroll the content element when the mousewheel is used within the 
		// content or the scrollbar element.
		$$(content, scrollbar).addEvent('mousewheel', function(e){	
			e = new Event(e).stop();
			var step = slider.step - e.wheel * 30;	
			slider.set(step);					
		});
	}
	// Stops the handle dragging process when the mouse leaves the document body.
	$(document.body).addEvent('mouseleave',function(){slider.drag.stop()});
}


// standard events
window.addEvent('domready', function() {

try {
	makeScrollbar( $('content_scroll'), $('content_scrollbar'), $('content_handle') );
} catch (e) { }


// slide a left pane in and out
try {
	var leftpane = new Fx.Slide('cms_leftpane', { mode: 'horizontal' });
	
	$('toggleleftpane').addEvent('click', function(e) {
		e = new Event(e);
		
		var elements = $$('.arrow');

		if ( $('toggleleftpane').title == 'Open' ) {

			$('cms_rightpane').setStyle('left',215);
			$('toggleleftpane').setStyle('left',215);
			$('toggleleftpane').setStyle('background-image','url(images/cms/close.gif)');
			$('toggleleftpane').title = 'Close';
			leftpane.show().chain(function() {  });
			auto_height_adjust();
			
			// handle arrows
			elements.each(function(element, idx) {
				element.setStyle('display', '');
			});

		} else if ( $('toggleleftpane').title == 'Close' ) {
			$('cms_rightpane').setStyle('left',0);
			$('toggleleftpane').setStyle('left',0);
			$('toggleleftpane').setStyle('background-image','url(images/cms/open.gif)');
			$('toggleleftpane').title = 'Open';

			// handle arrows
			elements.each(function(element, idx) {
				element.setStyle('display', 'none');
			});

			leftpane.hide().chain(function() {  });
			auto_height_adjust();
		}

		e.stop();
	});
} catch (e) { }	


// handle country/provstate selector
 /*try {
	$('countryid').addEvent('change', function(e) {
		e = new Event(e).stop();
	 
		var country_id = document.getElementById('countryid').value;
		var url = "/?ajax=provstate_selector&country_id="+country_id;
		var dest = $('provstate_id_container');
		dest.innerHTML='Loading...';
	 
		new Ajax(url, {
			method: 'get',
			update: dest
		}).request();
	});
 } catch (e) { }*/

 // make anything draggable
 try {
 var elements = $$('.draggable');
 elements.each(function(element, idx) {
	new Drag.Move(element.id);
 });
 } catch (e) {
	 
 }
 
 // animation effect on store listing
 try {
	var default_opacity = '0.7';
	var elements = $$('.store_row-disabled');
	elements.each(function(element, idx) {
		element.setStyle('opacity',default_opacity);
		element.addEvent('mouseenter', function(e) {
			element.setStyle('opacity','1.0');
		});
		element.addEvent('mouseleave', function(e) {
			element.setStyle('opacity',default_opacity);
		});
	});
	var elements = $$('.store_row_alt-disabled');
	elements.each(function(element, idx) {
		element.setStyle('opacity',default_opacity);
		element.addEvent('mouseenter', function(e) {
			element.setStyle('opacity','1.0');
		});
		element.addEvent('mouseleave', function(e) {
			element.setStyle('opacity',default_opacity);
		});
	});
} catch (e) {
	 
 }


  // backend calcal
  try {
  var elements = $$('.calcal tr td');
  elements.each(function(element, idx) {
  	

	if ( element.innerHTML == '&nbsp;' || element.innerHTML.charCodeAt(0) == 160 ) {
		element.style.backgroundColor = '#cccccc';
	} else {
		if ( element.className != 'hiday' ) {
			element.addEvents({
				'mouseenter': function() {
					element.style.cursor = 'pointer';
					element.style.backgroundColor = '#BDD5D5';
				},
				'mouseleave': function() {
					element.style.backgroundColor = '#ffffff';
				},
				'click': function() {
					// grab the text from inside and open it
					
				}
			});
		}
  }
  });
  } catch (e) {
	  
  }  

});


