// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

var vent = {

    // admin tour cost template selection
    assign_cost_template:function(tour_id, departure_id, cost_template_id) {
        new Ajax.Updater('cost_form',
            '/admin/tours/tour/' + encodeURIComponent(tour_id) +
            '/departure/' + encodeURIComponent(departure_id) +
            '/cost/new?template_id=' + encodeURIComponent(cost_template_id),
            {
                method: 'get',
                asynchronous:true, 
                evalScripts:true
            });
    },

    // admin tour reminder template selection
    assign_reminder_template:function(tour_id, departure_id, reminder_template_id) {
        new Ajax.Updater('reminder_form',
            '/admin/tours/tour/' + encodeURIComponent(tour_id) +
            '/departure/' + encodeURIComponent(departure_id) +
            '/reminders/new?template_id=' + encodeURIComponent(reminder_template_id),
            {
                method: 'get',
                asynchronous:true, 
                evalScripts:true
            });
    },
    
    // admin tour rule template selection
    assign_rule_template:function(tour_id, departure_id, rule_template_id) {
        new Ajax.Updater('rule_form',
            '/admin/tours/tour/' + encodeURIComponent(tour_id) +
            '/departure/' + encodeURIComponent(departure_id) +
            '/rules/new?template_id=' + encodeURIComponent(rule_template_id),
            {
                method: 'get',
                asynchronous:true, 
                evalScripts:true
            });
    },


  toggle_block:function(blockname) {
	  Effect.toggle(blockname, 'appear', { duration: 0.5 });
	  var block_head = document.getElementById(blockname + "_head")
	  if (block_head.hasClassName('open')) {
	    block_head.removeClassName('open');
	    block_head.addClassName('closed');
	  } else if (block_head.hasClassName('closed')) {
	    block_head.removeClassName('closed');
	    block_head.addClassName('open');
	  }
  },
  
	// toggle file upload field in webadmin view
	toggle_upload_field:function(model, op, path) {
        if(model=='photo') {
	        record = 'photo_image';
	        record_name = 'photo[image]';
        }
        else if(model=='person') {
            record = 'person_photo';
            record_name = 'person[photo]';
        }

		if(op=='upload') {
			var tmp = $('fileUploadLink').childNodes;
			for(var i=0; i<tmp.length; i++) {
				if(tmp[i].nodeName=='A')
					tmp[i].setAttribute('onclick', 
					  'vent.toggle_upload_field(\''+model+'\',\'image\',\''+path+'\');return false;');
					tmp[i].innerHTML = 'Cancel';
			}
			
			var hidden_field = document.createElement('input');
			hidden_field.setAttribute('type', 'hidden');
			hidden_field.setAttribute('id', record+'_temp');
			hidden_field.setAttribute('name', record+'_temp');
			$('fileUploadImage').remove();
			$('fileUpload').appendChild(hidden_field);

			var file_field = document.createElement('input');
			file_field.setAttribute('type', 'file');
			file_field.setAttribute('id', record);
			file_field.setAttribute('name', record_name);
			file_field.setAttribute('size', 30);
			var br = document.createElement('br');
			file_field.appendChild(br);
			$('fileUpload').insertBefore(file_field, $('fileUploadLink'));

		}
		else if(op=='image') {
			var tmp = $('fileUploadLink').childNodes;
			for(var i=0; i<tmp.length; i++) {
				if(tmp[i].nodeName=='A')
					tmp[i].setAttribute('onclick', 
					  'vent.toggle_upload_field(\''+model+'\',\'upload\',\''+path+'\');return false;');
					tmp[i].innerHTML = 'Change Image';
			}
			
			var image = document.createElement('img');
			image.setAttribute('alt', ' ');
			image.setAttribute('src', path);
			var file_image = document.createElement('span');
			file_image.setAttribute('id', 'fileUploadImage');
			file_image.appendChild(image);
			var br = document.createElement('br');
			file_image.appendChild(br);
			
			$(record+'_temp').remove();
			$(record).remove();
			$('fileUpload').insertBefore(file_image, $('fileUploadLink'));
		}
	}
}

show_offer_details = function(offer_id) {
    $('offer_details_' + offer_id).show();
    $('show_offer_details_' + offer_id).remove();
}

show_all_birdlists = function() {
    var hidden_links = $$('div.block ul.select li.hidden_bl');
    for (var i=0; i<hidden_links.length; i++) {
        hidden_links[i].className= "";
    }
    $('show_birdlists').remove();
}
show_all_tour_reports = function() {
    var hidden_links = $$('div.block ul.select li.hidden_tr');
    for (var i=0; i<hidden_links.length; i++) {
        hidden_links[i].className= "";
    }
    $('show_tour_reports').remove();
}

sfHover = function() {
    var sfEls = document.getElementById("menu").getElementsByTagName("LI");
    for (var i=0; i<sfEls.length; i++) {
        sfEls[i].onmouseover=function() {
            this.className+=" sfhover";
        }
        sfEls[i].onmouseout=function() {
            this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
        }
    }
}
if (window.attachEvent) window.attachEvent("onload", sfHover);