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

// These are john-approved javascript functions

// Except this one, it sucks, but so does the rest of the application... This is for the search field
function onLoad(){ 
	initClipboard();
}

function initClipboard(){ 
	createClip('only');
	createClip('html');
	createClip('bb');
}

function createClip(preffix) {
    var buttonId = preffix + '_link_copy';
    $('#' + buttonId).css({"background-color" : "LightGrey"});
    
	var clip = new ZeroClipboard.Client();
    clip.setHandCursor(true);
    clip.setText($('#' + preffix + '_link').val());
    
    clip.addEventListener('complete', function(client, text) {
        $('#copy_flash_message').show(1000).animate({"opacity": 1}, 3000).hide(1000);
    });
    
    clip.addEventListener('mousedown', function(client, text) {
        $('#' + buttonId).css({"background-color" : "#359DFF"});
    });
    
    clip.addEventListener('mouseup', function(client, text) {
        $('#' + buttonId).css({"background-color" : "LightGrey"});
    });
    
    clip.glue(preffix + '_link_copy');
}

function toggleVendors(product_id){
    var last_vendors_box = $('#last_vendors_' + product_id);
    last_vendors_box.toggle();
    $('#toggle_vendors_button_' + product_id).val(last_vendors_box.is(':visible') ? "Show only ten vendors" : "Show all vendors");        
} 

function goResult() {
	if (results && results.hasActivatedResultAndVersion() && document.all) {
		results.goToActivatedVersion();
	} else {
		window.location = "/search/searchProductByProductName?productName="+ document.getElementById("searchForm_searchString").value
	}
}
	
//Returns true or false, depends on the hidden search option field being filled out
function isBuylist() {
  if($('#searchOptHiddenValue').val() == 'variants') {
    return false
  }
  else {
    return true
  }
}

var plusImagePath = '/images/plus.jpg';
var minusImagePath = '/images/minus.jpg';

var formIdPostfix = '_form';
var toggleIdPostfix = '_toggle';
var containerIdPostfix = '_container';

//The function for toggling a product
function toggleProduct(productName) {
	toggleSelect = '#'+productName+toggleIdPostfix;
  var toggleSrc = $(toggleSelect).attr('src');

  if(toggleSrc == minusImagePath) {
		closeProduct(productName);
  }
  else {
		openProduct(productName);
  }
}

//Doesn't actually show the product, just initiates the ajax
function openProduct(productName) {
	formSelect = '#' + productName + formIdPostfix;
	$(formSelect).trigger('onsubmit');
	
	toggleSelect = '#'+productName+toggleIdPostfix;
  $(toggleSelect).attr({src : minusImagePath});
}

//Called automatically by the oncomplete ajax call
function showProduct(productName) {
	containerSelect = '#'+productName+containerIdPostfix;
  $(containerSelect).show();
}

//Just hides the product info
function closeProduct(productName) {
	containerSelect = '#'+productName+containerIdPostfix;
	$(containerSelect).hide();
	
	toggleSelect = '#'+productName+toggleIdPostfix;
	$(toggleSelect).attr({src : plusImagePath});
}

function openProductCategory(productName, categoryId) {
	categoryIdSelect = '#' + productName + formIdPostfix + ' #category_id';
	$(categoryIdSelect).attr({value : categoryId});
	openProduct(productName);
}

function setQty(productName, qtyInt){
  qtyFormSelect = $('#' + productName + formIdPostfix + ' #qty')
  qtyFormSelect.attr({value : qtyInt});
  openProduct(productName);
}

function openProductSort(productName, sort) {
	setSort(productName,sort);
	openProduct(productName);
}

function setSort(productName, sort) {
	sortSelect = '#' + productName + formIdPostfix + ' #sort';
	$(sortSelect).attr({value : sort});
}

function resetProduct(productName) {
  setQty(productName, 0);
}

//Toggles visibility of a cart
function toggleCart(type) {
	if (type == 'buylist') {
  	element = $('#sidebarcontainer_buy')
  }
	else {
		element = $('#sidebarcontainer_sell')
	}
	
	if(element.is(':hidden')) {
		element.slideDown(200);
	}
	else {
		element.slideUp(200);
	}
}

//Expands the cart quickly, and then contracts it after 1.5 seconds
function quickExpandCurrentCart() {
  element = null;
  
  var currentCart = getCurrentCart();
  currentCart.slideDown(200);
  setTimeout("contractCurrentCart();",2700);
}

function quickExpandBothCarts() {
  getBuylistCart().slideDown(200);
	getShoppingCart().slideDown(200);
  setTimeout("contract(getBuylistCart());",2700)
  setTimeout("contract(getShoppingCart());",2700)
}

//Slide up the current cart
function contractCurrentCart() {
  var currentCart = getCurrentCart();
  contract(currentCart);
}

//Slide up the current cart
function expandCurrentCart() {
  var currentCart = getCurrentCart();
  expand(currentCart);
}

function contract(jquerySelector) {
	if (!jquerySelector.is(':hidden')) {
    jquerySelector.slideUp(200);
  }
}

function expand(jquerySelector) {
	if (jquerySelector.is(':hidden')) {
    jquerySelector.slideDown(200);
  }
}

//This returns a JQuery object of the current cart
function getCurrentCart() {
  if(isBuylist()) {
    return getBuylistCart();
  }
  else {
    return getShoppingCart();
  }
}

function getBuylistCart() {
	return $('#sidebarcontainer_buy')
}

function getShoppingCart() {
	return $('#sidebarcontainer_sell')
}

function addToCart(addButton) {
  var prodVarClientId = $(addButton).parent().parent().find("input").val();
  var qty = $(addButton).parent().parent().find("input:eq(1)").val();
  var avlqty = $(addButton).parent().parent().find(".GB_item_qty").html();

  if (parseInt(qty) > parseInt(avlqty)) {
    alert("required quantity is more than available quantity");
  }
  else {
    gotoShoppingCard(prodVarClientId, qty);
    //$("#sidebarcontainer_sell").slideDown(300);
  }
}

function showHide()
  {
    var divstyle = new String();
    divstyle = document.getElementById("card2_sec3").style.visibility;

    if (divstyle.toLowerCase()=="visible" || divstyle == "") {
      document.getElementById("card2_sec3").style.visibility='hidden';
      document.getElementById("card2_sec3").style.display='none';

    } else {
      document.getElementById("card2_sec3").style.visibility='visible';
      document.getElementById("card2_sec3").style.display='block';
    }
  }

function changeImageAndClass(variantType) {
    isBuylistMode = variantType == 'buylist_variant'
    imagePath = isBuylistMode ? "/images/img/s_selling.png" : "/images/img/s_buying.png"
    className = isBuylistMode ? "sell" : "buy"
    
    $('#searchImage').attr({src : imagePath});
    $('#content').attr({'class' : className});

    $('#searchOptHiddenValue').val(variantType);
    $("input[@name='variantType']").val(variantType)

    if(variantType == 'variants') {
        $('#sellingtray').hide();
        $('#buyingtray').show();
    } else {
        $('#buyingtray').hide();
        $('#sellingtray').show();
    }
}