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

function initialize_page() {
	if($('scroll_target') && $('scroll_thumb')) {
		if(init_scroll_thumb('scroll_target','scroll_thumb')) {
			Element.show('scrollbar_container');
		}
	}
};

var swfu;

function init_flash_uploader(url_to_upload, auth, session) {
	try {
		var swfu = new SWFUpload({
			// Create the custom swfupload_photos_path in the routes.rb file
			upload_url : url_to_upload, //'<%= formatted_shoot_file_images_path(@shoot_file, :js) %>',
			post_params : {
				'authenticity_token' : auth, //'<%= form_authenticity_token %>',
				'_contentlocations_session' : session //'<%= session.session_id %>'
			},
			flash_url : '/assets/swfupload_fp10.swf',
			flash9_url : '/assets/swfupload_fp9.swf',
			
			button_placeholder_id : 'spanSWFUploadButton',
			button_width : "160",
			button_height : "26",
			button_text  : '<img src="/images/page_white_add.png" style="padding-right: 3px; vertical-align: bottom;">Select Images <span style="font-size: 7pt;">(2 MB Max)</span>',
			button_text_left_padding : 5,
			button_text_top_padding  : 5,

			file_size_limit : '<%= 5.megabytes %>',
			file_types : '*.jpg;*.gif;*.png;',
			file_types_description : 'JPG, GIF, or PNG Images',
			file_upload_limit : '0',

			file_queue_error_handler : fileQueueError,
			file_dialog_complete_handler : fileDialogComplete,
			upload_progress_handler : uploadProgress,
			upload_error_handler : uploadError,
			upload_success_handler : uploadSuccess,
			upload_complete_handler : uploadComplete,

			custom_settings : { 
				upload_target : 'divFileProgressContainer'
			},

			debug: false

		});
		return swfu;
	} catch (ex) { this.debug(ex); }
};

	
function getElementsByClass(searchClass,node,tag) {
  var classElements = new Array();
  if (node == null)
    node = document;
  if (tag == null)
    tag = '*';
  var els = node.getElementsByTagName(tag);
  var elsLen = els.length;
  var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
  for (i = 0, j = 0; i < elsLen; i++) {
    if (pattern.test(els[i].className) ) {
      classElements[j] = els[i];
      j++;
    }
  }
  return classElements;
};

function init_draggables(c) {
	elements = getElementsByClass(c);
	for (i = 0; i < elements.length; i++) {
	  new Draggable(elements[i].id, {
		ghosting:true, 
		revert:true
		}
	  );
	}
};

function show_objects(c) {
	elements = getElementsByClass(c);
	for (i = 0; i < elements.length; i++) {
	  Element.appear(elements[i].id, {duration: 0.3, delay: i/10});
	}
};

var thumbMessage = "These 7 images will be shown on the live site. >>";
function init_sortables(l1, l2, message_id, button_id, finished_id) {
   Sortable.create(l1, {
	containment: [l1,l2], 
 	dropOnEmpty: true,
	onUpdate:function() {
/*		new Ajax.Request(sort_url,{
			asynchronous:true,
            evalScripts:true,
            onComplete:function(request) { new Effect.Highlight(list_id,{}); },
            parameters:Sortable.serialize(list_id)})
		    }
*/
	  var numThumbs = Sortable.sequence(l1).length;
      (numThumbs == 7) ? Element.show(button_id) : Element.hide(button_id);
	  Element.hide(finished_id); //hide the finished button until they are saved.
	  if (numThumbs > 7) {
		thumbMessage = "Only the top 7 will be shown live on the site.  Please remove " + (numThumbs - 7) + " images.";
	  } else if (numThumbs < 7) {
	    thumbMessage = "You have " + numThumbs + " images chosen. Please select " + (7 - numThumbs) + " more."; 
	  } else {
	    thumbMessage = "These 7 images will be shown on the live site.";
	  }
	  Element.update(message_id, thumbMessage + " >>");
	
	}
   });
   Sortable.create(l2, {containment: [l1,l2], dropOnEmpty: true});
};

function show_large_photo(show_element_id, hide_class) {
	elements = getElementsByClass(hide_class);
	cancel_effects();
	Element.appear(show_element_id, {duration:0.2, beforeStart:hide_others(show_element_id, elements), afterFinish:hide_others(show_element_id, elements), queue: "end"});
}

function hide_others(e, elements) {
	for (i = 0; i < elements.length; i++) {
		if(elements[i].id == e.id) {
			continue;
		}
		Element.hide(elements[i].id);
	};
}

//Scroll Bar Functions

var scroll_thumbheight;
var target_scrollable; //the distance the scroll window can travel
var scroll_midheight;
var scroll_xcon;
var scroll_yres;
var scroll_ymin;
var scroll_ymax;
var scroll_ydiff; //the distance the thumb can travel 
var scroll_ydiv;  //one unit of travel for thumb
var scroll_click_divisions;
var scroll_click_thumbheight_percent;

function init_scroll_thumb(target_id, thumb_id) {
	var e = $(target_id);
	//	alert("scroll height = " + e.scrollHeight + " and client height = " + e.clientHeight);
	if (e.scrollHeight > e.clientHeight) {

		target_scrollable = e.scrollHeight - e.clientHeight;
		var h = e.clientHeight / e.scrollHeight * scroll_midheight;
		scroll_thumbheight = (h < scroll_minthumbheight) ? scroll_minthumbheight : h;
		scroll_ymax = scroll_midheight - scroll_thumbheight + scroll_ymin;
		scroll_ydiff = scroll_ymax - scroll_ymin;
		scroll_ydiv = scroll_ydiff / scroll_yres;
		scroll_click_divisions = Math.round(scroll_click_thumbheight_percent * scroll_thumbheight / scroll_ydiv);

		$(thumb_id).style.height = Math.round(scroll_thumbheight) + 'px';	
		//alert("h = " + h + " scroll_thumbheight = " + Math.round(scroll_thumbheight));
		return true;
	} else {
		return false;
	}
}
function thumb_change(target_id, draggable) {
	var ypos = draggable.element.offsetTop;
	var ypos_absolute = ypos - scroll_ymin; 
	var percent = ypos_absolute / scroll_ydiff;
	scroll_window(target_id,percent);
}
function scroll_window(target_id, percent) {
	var e = $(target_id);	
	target_scrollable = e.scrollHeight - e.clientHeight; //recompute because the value changes after initialization???
	percent = (percent < 0) ? 0 : ( (percent > 1) ? 1 : percent);
	e.scrollTop = Math.round(target_scrollable * percent);
}
function scroll_button(direction, thumb_id, target_id) {
	var thumb_distance = scroll_ydiv * scroll_click_divisions;
	var sign = (direction == "up") ? -1 : 1;
	
	var y = $(thumb_id).offsetTop;
	thumb_top = y + (Math.round(thumb_distance) * sign);
	//alert('thumb_top=' + thumb_top + ' y=' + y);
	//check for max, min, and set both windows
	if (thumb_top > scroll_ymax) {
		$(thumb_id).style.top = scroll_ymax + 'px';
		scroll_window(target_id, 1);
	} else if (thumb_top < scroll_ymin) {
		$(thumb_id).style.top = scroll_ymin + 'px';
		scroll_window(target_id, 0);
	} else {
		$(thumb_id).style.top = thumb_top + 'px';
		scroll_window(target_id, ((thumb_top - scroll_ymin) / scroll_ydiff) );
	}
}
 


//LIBRARY
function cancel_effects() {
	var queue = Effect.Queues.get('global');
	queue.each(function(effect) { effect.cancel(); });
}

function click_on_property(element) {
	elements = getElementsByClass('live');
	for (i = 0; i < elements.length; i++) {
		elements[i].className = 'dead'
	}
	element.className = 'live'
};

//Sortable Property List
function createSortableProperties(id, submit) {
	Sortable.create(id, {
	  	onUpdate:function(){ new Ajax.Request(submit, {
			method:'post',
			asynchronous:true,
          	evalScripts:true,
          	onComplete:function(request){ new Effect.Highlight(id,{}); },
          	parameters:Sortable.serialize(id)})
		},
		handle:'handle'
	});
};
