// $Id: mea.js,v 1.1.2.1 2008/04/21 06:59:13 dopry Exp $

/**
 *  Auto Attach standard client side file input validation
 */
Drupal.meaValidateAutoAttach = function() {
  $("input[@type='file'].mea").change( function() {
    $('.mea-js-error').remove();
    /**
     *  add client side validation for the input[@file] accept attribute
     */
 
    if(this.accept.length>1){
      v = new RegExp('\\.('+(this.accept?this.accept:'')+')$','gi');
      if (!v.test(this.value)) {
        var error = 'The file ' + this.value + " is not supported.\n";
        error += "Only the following file types are supported: \n" + this.accept.replace(/\|/g, ' ');
        alert(error);
        // what do I prepend this to? 
        //$(this).prepend($('<div class="mea-js-error>"' + error + '</div>'));
        this.value = ''; 
        return false;
      }   
    }   
    /**
     * Add filesize validation where possible
     */

  }); 
}

// Global killswitch
if (Drupal.jsEnabled)
{
    $(document).ready(function()
    {
        getHeaderMea();
        Drupal.meaValidateAutoAttach();
    });
}

function getHeaderMea()
{
        var nid = $('#mea-graph-nid').val();
        var fieldname = $('#mea-graph-name').val();
        $.ajax({
			type:		'post',
			dataType :	'json',
            url: Drupal.settings.jstools.basePath + "mea/ajax/header/" + nid + "/" + fieldname,
            global: true,
            success: function (data)
            {
               // console.info("scroll = " + data.scroll[0]);
                var scrolls = new Array('scrollUp', 'scrollDown', 'scrollLeft', 'scrollRight');
                var output = "";
                output += '<div id="mea-graph-header">';
                for (var i = 0; i < data.filepath.length; i++)
                    {
                          var target = ((data.url[i].substr(0, 7) == "http://") ? '_blank' : '_parent');
                          var url = ((data.url[i].substr(0, 7) == "http://") ? data.url[i] : Drupal.settings.jstools.basePath + data.url[i]);
                          output += '     <a href="' + url + '" target="' + target + '"><img src="' + Drupal.settings.jstools.basePath + data.filepath[i] + '" width="670" height="210" /></a>';
                    }
                 output += '</div>';
                $('.field-items').parent().prepend(output);

               //$(".mea-graphique").css("background", "white");
                $('#mea-graph-header').before('<div id="nav">').cycle({
                        fx: scrolls[data.scroll[0]] + ',' + scrolls[data.scroll[1]] + ',' + scrolls[data.scroll[2]],
                        speed:  1000,
                        timeoutFn: calculateTimeout,
                     //   pause:  1,
                        before:  onBefore,
                        after:   onAfter,
                        pager:  '#nav'

                    });

                            $('.view-v-home-mea-graphique .field-item').eq(0).mouseover(function()
                            {
                                $('#nav a').eq(0).trigger('click');
                            //return false;
                            });

                            $('.view-v-home-mea-graphique .field-item').eq(1).mouseover(function()
                            {
                                $('#nav a').eq(1).trigger('click');
                          //  return false;
                            });
                            $('.view-v-home-mea-graphique .field-item').eq(2).mouseover(function()
                            {
                                $('#nav a').eq(2).trigger('click');
                          //  return false;
                            });
                     
            },
            error: function(data)
            {
              alert('An error occurred');
            }
          });
}

function calculateTimeout(currElement, nextElement, opts, isForward)
{
   var url = nextElement.firstChild.src;
   var tab = url.split('/');
   tab = tab[tab.length - 1].split('.');
   var title = tab[0].toLowerCase();
   var elementDelay = $("#mea-graphique-" + title).children(".mea-graph-delay").val();
   //console.info("delay = " + elementDelay);
   return elementDelay;
   //return 3000;
}

function onBefore(curr, next, opts)
{
    $(".mea-graphique").css("background", "#FFE7E2");
    var url = curr.firstChild.src;
    var tab = url.split('/');
    tab = tab[tab.length - 1].split('.');
    var title = tab[0].toLowerCase();
    // console.info("title = " + title);
    $("#mea-graphique-" + title).css("background", "white");

    url = next.firstChild.src;
    tab = url.split('/');
    tab = tab[tab.length - 1].split('.');
    title = tab[0].toLowerCase();
    //   console.info("title = " + title);
    $("#mea-graphique-" + title).css("background", "#FFA088");
}

function onAfter(curr, next, opts)
{
    $(".mea-graphique").css("background", "#FFE7E2");
    var url = curr.firstChild.src;
    var tab = url.split('/');
    tab = tab[tab.length - 1].split('.');
    var title = tab[0].toLowerCase();
    //   console.info("title = " + title);
    $("#mea-graphique-" + title).css("background", "white");

    url = next.firstChild.src;
    tab = url.split('/');
    tab = tab[tab.length - 1].split('.');
    title = tab[0].toLowerCase();
    //  console.info("title = " + title);
    $("#mea-graphique-" + title).css("background", "#FFA088");
}