var $j = jQuery.noConflict();

$j(document).ready(function() {
  var w = 2;
  $j('#links .link').each(function() {
    w += $j(this).outerWidth(true);
  })
  $j('#links').css({
    'width' : w,
    'margin' : 'auto',
    'visibility' : 'visible'
  });
  $j('#links .link').mouseover(function() {
    $j(this).addClass('hover');
  })
  $j('#links .link').mouseout(function() {
    $j(this).removeClass('hover');
  })
});

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

// Players Registration
function check_add_to_terms() {
    if (!(b = $("add_to_terms").checked)) {
        alert("Your no accept the terms in the license Agreements.");
    }
    return b;
}


// Teams
function remove_roster(id) {
    if (confirm("Are you sure?")) {
        $("div_"+ id).remove();
    }
}

// Forgot Password
function forgot_password(url) {
    window.location = url + "/?email=" + $("email").value;
}

// Report Validations
function validate_report(home_id, visitor_id) {
  var hs = parseInt($F("report_home_score"));
  var vs = parseInt($F("report_visitor_score"));
  if (isNaN(hs) || isNaN(vs)) {
    alert("You must enter home score and visitor score.");
    return false;
  }
  var h = 0;
  var v = 0;
  $$("#scorelogs select").each(
    function(s) {
      if (/team_id/.test(s.name) && parseInt($F(s))) {
        if ($F(s) == home_id) {
          h++;
        }
        if ($F(s) == visitor_id) {
          v++;
        }
      }
    }
  );
  if (h == hs || v == vs) {
    $("report_form").submit();
  } else {
    alert("you should record the correct score.\nHome: "+ h +"\nVisitor: "+ v);
    return false;
  }
}

$j(document).ready(function() {
  $j('a[rel*=facebox]').facebox();
});

/* Close Facebox pop-up. */
function closeFacebox() {
  $j.facebox.close();
}


/* Media Uploads */
function uploadFile(el_form, upload_progress) {
    var f = $j(el_form);
    var iframe_name = 'iframe' + parseInt(new Date().getTime().toString().substring(0, 10));
    //Generate id for upload progress tracking
    var uuid = "";
    for (i = 0; i < 32; i++) {
        uuid += Math.floor(Math.random() * 16).toString(16);
    }
    //Attach the id in the form
    f.attr("action", f.attr("action") + "?X-Progress-ID=" + uuid);
    $j('#container').append('<iframe id="' + iframe_name + '" name="' + iframe_name + '" style="height: 0; width: 0; display: none;" />');
    f[0].target = iframe_name;
    f[0].submit();
    if(upload_progress) {
        $j('#media_uploader .title:first').html('Uploading');
        $j('#reply_image_form').hide();
        $j('#progress_bar_pane').show();
        timer = window.setInterval(function(){update_progres_bar(uuid)}, 2000);
    } else {
        $j('#facebox .content:first').load('/shared/display_upload_progress');
    }
}

function update_progres_bar(uuid) {
	//	uncomment out following lines for dev
	window.clearTimeout(timer);
	$j('#facebox .content:first').load('/shared/display_upload_progress');
	return;
	$j.ajax( {
		type: 'GET',
		url: '/progress',
		dataType: 'json',
		beforeSend: function(xhr) {
			xhr.setRequestHeader("X-Progress-ID", uuid);
		},
		success: function(upload) {
			// change the width in the inner progressbar
			if (upload.state == 'uploading') {
				bar = $j('#upload_bar');
				w = Math.floor((upload.received / upload.size)*100);
				bar.width(w + '%');
			}
			//done, stop the interval
			if (upload.state == 'done') {
				window.clearTimeout(timer);
                $j('#facebox .content:first').load('/shared/display_upload_progress');
			}
		}
	});
}

//wraper method to create a confirm dialog for media uploader
(function($j){
  $j.confirmDialogMedia = {}
  $j.fn.confirmDialogMedia = function(el){
    $j.confirmDialogMedia.container = el || '#confirm_media_dialog_modal_container';

    this.each(function(){
      var el = $j(this);
      el.click(function(ev){
        ev.preventDefault();
        $j.confirmDialogMedia.show_modal();
        $j(document).bind('close.facebox', $j.confirmDialogMedia.attach_to_facebox);

        $j.confirmDialogMedia.attach_modal_events(this.href);
        //console.log(this.href);
      });
    });

    return this;
  };

  $j.confirmDialogMedia.show_modal = function(){
    jQuery.facebox($j($j.confirmDialogMedia.container).html());
  };

  $j.confirmDialogMedia.attach_modal_events = function(href){
    //connect where facebox listen
    $j('#facebox').bind('click', href, $j.confirmDialogMedia.click_handler);
  };

  $j.confirmDialogMedia.click_handler = function(ev){
    ev.preventDefault();
    var str = ev.target.id;
    if(str == 'confirm_modal_cancel'){$j(document).trigger('close.facebox')}
    if(str == 'confirm_modal_submit'){
      $j.ajax({dataType:'script', url:ev.data});
      $j(document).trigger('close.facebox')
      //console.log('submit ' + ev.data);
    }
  };

  $j.confirmDialogMedia.attach_to_facebox = function(){
    $j('#facebox').unbind('click', $j.confirmDialogMedia.click_handler);
    $j(document).unbind('close.facebox', $j.confirmDialogMedia.attach_to_facebox);
  };

})(jQuery);

