// document onload function to hook up all JQuery event hijackers
$(document).ready(function() {
   if (typeof document.body.style.maxHeight == "undefined") {
     // this is IE 6 or below, we don't have to do anything
   } else {
     // for every other browser, remove the ie6 notice div altogether so it hopefully doesn't get indexed by Google
     $('#ie_notice').remove();
   }
   
   // set up links with the 'remote' class to be submitted via ajax
   $("a.remote").click(function(){
     if(this.rel) {
       // if a rel is specified, the contents of the href are loaded into the div with that id
       $(this.rel).load(this.href);
     } else {
       $.get(this.href, null, null, "script");
     }
     return false;
   })
   
   // hoverpulse effect for bottles
   $('img.bottle').hoverpulse({
       size: 30,  // number of pixels to pulse element (in each direction)
       speed: 400 // speed of the animation 
   });
   
   // show the age verification popup
   if ($('#notice21')) {
     var height = 0;
     var width = 0;
     // get the browser window dimensions (for positioning the popup)
     var browser_height = $(window).height();
     var browser_width = $(window).width();
     
     // get the page dimensions (for sizing the overlay to cover everything)
     var document_height = $(document).height();
     var document_width = $(document).width();
     
     // we want the popup in the middle of the screen
     if(browser_width > 962) {
       // position the popup in the center of the browser width
       width = (browser_width - 962)/2;
     }
     if(browser_height > 536) {
       // position the popup in the center of the browser height
       height = (browser_height - 536)/2;
     }
     
     // show and resize overlay
     $('#verification_overlay').fadeIn();
     $('#verification_overlay').css({'height' : document_height+'px', 'width' : document_width+'px'})
     
     // show and position the popup
     $('#notice21').fadeIn();
     $('#notice21').css({'top' : height+'px', 'left' : width+'px'})
   }
   
});

function dynamic_age_verification(){
  // new Ajax.Request('/default/dynamic_age_verfication', {asynchronous:true, evalScripts:true}); return false;
	$.ajax({
			url : "/default/dynamic_age_verification", 
			type: "POST", // get  or post
			async: true,
			dataType: 'script'
			});

}




// methods to show/hide "AJAX" form divs. assumes you have _form, _busy, and _link divs
function show_busy_div(name) {
	if($("#" + name + "_form")) { $("#" + name + "_form").hide(); };
	if($("#" + name + "_link")) { $("#" + name + "_link").hide(); };
	if($("#" + name + "_busy")) { $("#" + name + "_busy").show(); };
}
function show_form_div(name) {
	if($("#" + name + "_link")) { $("#" + name + "_link").hide(); };
	if($("#" + name + "_busy")) { $("#" + name + "_busy").hide(); };
	if($("#" + name + "_form")) { $("#" + name + "_form").show(); };
}
function show_link_div(name) {
	if($("#" + name + "_busy")) { $("#" + name + "_busy").hide(); };
	if($("#" + name + "_form")) { $("#" + name + "_form").hide(); };
	if($("#" + name + "_link")) { $("#" + name + "_link").show(); };
}
function clear_form_div(name) {
	if($("#" + name + "_form")) { $("#" + name + "_form").html(""); }
	if($("#" + name + "_form")) { $("#" + name + "_form").hide(); };
	if($("#" + name + "_busy")) { $("#" + name + "_busy").hide(); };
	if($("#" + name + "_link")) { $("#" + name + "_link").show(); };
}
// methods to show/hide "AJAX" form spans in table rows.
function show_busy_row(name,id) {
	if($("#tr_" + name + "_link_"+id)) { $("#tr_" + name + "_link_"+id).hide(); };
	if($("#tr_" + name + "_busy_"+id)) { $("#tr_" + name + "_busy_"+id).show(); };
}
function show_link_row(name,id) {
	if($("#tr_" + name + "_busy_"+id)) { $("#tr_" + name + "_busy_"+id).hide(); };
	if($("#tr_" + name + "_link_"+id)) { $("#tr_" + name + "_link_"+id).show(); };
}
// methods to show/hide busy widgets
function show_busy_widget(name) {
	if($("#" + name)) { $("#" + name).show(); };
} 
function hide_busy_widget(name) {
	if($("#" + name)) { $("#" + name).hide(); };
}

function get_width() {
  if($('#sortable_items')) {
    // get the total width of the containing element
    var total_width = $('#sortable_items').width()
    
    // subtract width of other elements and allow for padding
    var new_width_single = total_width - 180;
    var new_width_double = total_width - 210;
    
    // set the width on the elements
    $('.auto_width').css('width', new_width_single);
    $('.auto_width2').css('width', new_width_double/2);
  }
}

// -------------------------------------------------------
// Methods to batch add tags to multiple upload
// -------------------------------------------------------
function apply_tags(){
	var tags = $("input[name='batch_tags']").attr("value");
	var form_inputs = $(":input");
	
	for(var i=0; i < form_inputs.length; i++){
		if(form_inputs[i].name.match("tags")){
			if( form_inputs[i].value.length > 0 ){ form_inputs[i].value = form_inputs[i].value + ", " + tags; }
			else { form_inputs[i].value = tags; }
		}
	}
	
	// clear out the field
	$("input[name='batch_tags']").attr("value", "");
}