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

$jQuery = jQuery.noConflict();

function toggleFullPreview(){
	$('preview').toggle();
	$('full_preview').toggle();
}

function show_editor_links(){
	$$('.editor_link').each(function(link) { link.show(); });
}

function show_member_links(){
	$$('.member_link').each(function(link) { link.show(); } );
}

function show_relative_dates(){
	$$('.relative_date').each(function(date) { new RelativeDate(date) });
	$$('.relative_date').each(function(date) { date.show(); });
}

function load_ajax_pagination(element){
  // the element in which we will observe all clicks and capture
  // ones originating from pagination links
  var container = element

  if (container) {
    var img = new Image
    img.src = '/images/ajax-loader.gif'

    function createSpinner() {
      return new Element('img', { src: img.src, 'class': 'spinner' })
    }

    container.observe('click', function(e) {
      var el = e.element()
      if (el.match('.pagination a')) {
        el.up('.pagination').insert(createSpinner())
        new Ajax.Request(el.href, { method: 'get' })
        e.stop()
      }
    })
  }	
}

// document.observe("dom:loaded", function() {
//   // the element in which we will observe all clicks and capture
//   // ones originating from pagination links
//   var container = $(document.body);
//   load_ajax_pagination(container);
// });


/* 
	Comment functions 
	TODO: move into own comments.js file and use only on comment pages
*/

function submit_comment(){
	var comment_submit = $('comment_submit');
	comment_submit.value = 'Loading...';
}

function submit_comment_failed(){
	$('comment_submit').value = 'Failed';
}

function init_comment_validations(){
	
	if($('comment_name') != undefined){
		var comment_name = new LiveValidation( "comment_name", { validMessage: "ok", wait: 500 } );
		comment_name.add( Validate.Presence, 
              				{ failureMessage: "Please enter your name." } );
	}
	if($('comment_name') != undefined){
		var comment_email = new LiveValidation( "comment_email", { validMessage: "ok", wait: 500 } );
		comment_email.add( Validate.Presence, 
              				{ failureMessage: "Please enter your email." } );
		comment_email.add( Validate.Email );
	}
	
	var comment_title = new LiveValidation( "comment_title", { validMessage: "ok", wait: 500 } );
	comment_title.add( Validate.Presence, 
			  			{ failureMessage: "Please enter a title for your comment." } );
	comment_title.add( Validate.Length, { minimum: 4 } );

	var comment_comment = new LiveValidation( "comment_comment", { validMessage: "ok", wait: 500 } );
	comment_comment.add( Validate.Presence, 
						{ failureMessage: "Didn't you want to enter a comment?" } );
}

$jQuery(function() {
  $jQuery("#comments .pagination a").live("click", function() {
    //$jQuery("#comments #comments_loading").html("loading...");
    $jQuery.get(this.href, null, null, "script");
    return false;
  });
});


/* dealmaker functions */

$jQuery(document).ready(function(){
	$jQuery(".deal_maker_link").colorbox({ height:"600px", width:"900px", onComplete: dm_init() });
});

var dm_current_index = 1;
var dm_image_count = 0;
var dm_current_image = null;

function dm_init(){
	dm_current_image = $('dm_image_' + dm_current_index);
	if(dm_current_image != undefined){
		
		dm_current_image.show();
		set_current_image_src();
	}
}

function next_image(){
	if(dm_current_index < dm_image_count){
		show_page_links();
		dm_current_image.hide();
		dm_current_image = $('dm_image_' + (++dm_current_index));
		dm_current_image.show();
		if(dm_current_index == dm_image_count){$('dm_next').hide();}
		set_current_image_src();
	} 
}

function previous_image(){
	if(dm_current_index > 1){
		show_page_links();
		dm_current_image.hide();
		dm_current_image = $('dm_image_' + (--dm_current_index));
		dm_current_image.show();
		if(dm_current_index == 1){$('dm_previous').hide();}
		set_current_image_src();
	} 	
}

function show_page_links(){
	$('dm_previous').show();
	$('dm_next').show();
}

function set_current_image_src(){
	var images = dm_current_image.select('img');
	if(images != undefined) {
		var image_src = dm_current_image.select('img')[0].src;
		var db_form_img = $('dm_form_image_src');
		if(db_form_img != undefined) {
		  db_form_img.setValue(image_src);	
		}
	}
}

function init_url_validation(){
	
	if($('url') != undefined){
		var url = new LiveValidation( "url", { validMessage: "ok", wait: 500 } );
		url.add( Validate.Presence, 
              				{ failureMessage: "Please enter a valid url." } );
		
		
		url.add( Validate.Format, { pattern: /^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(([0-9]{1,5})?\/.*)?$/i, failureMessage: "Please enter a valid url. ex: http://d27n.com" } );
	}
}

