/*
//----------------------------------------------------------------------------
// Copyright (c) 2006-2011 Asymmetric Software. Innovation & Excellence.
// Author: Mark Samios
// http://www.asymmetrics.com
// jQuery: General Support Functions for the I-Metrics CMS
//----------------------------------------------------------------------------
// I-Metrics CMS
//----------------------------------------------------------------------------
// Script is intended to be used with the following:
//
// osCommerce, Open Source E-Commerce Solutions
// http://www.oscommerce.com
// Copyright (c) 2003 osCommerce
//
// jQuery JavaScript Library
// http://jquery.com
// Copyright (c) 2009 John Resig
//----------------------------------------------------------------------------
// Released under the GNU General Public License
//----------------------------------------------------------------------------
*/
var general = {
  period: 1000,
  cookies: [],

  cookie_init: function(cookie_name) {
    if( document.cookie != 'undefined' ) {
      var cookies_array = document.cookie.split(';');
      for( var i in cookies_array) {
        var parts = cookies_array[i].split('=');
        general.cookies[parts[0]] = parts[1];
      }
    }
  },

  cookie_get: function( name ) {
    var result = '';
    if( typeof general.cookies[name] != 'undefined' ) {
      result = general.cookies[name];
    }
    return result;
  },

  cookie_set: function( name, value ) {
    general.cookies[name] = value;
  },

  cookie_update: function() {
    result = '';
    for( var i in cookies ) {
      result += i+'='+cookies[i]+';';
    }
    document.cookie = result;
  },

  stop_timer: function(timer) {
    if( typeof timer == 'undefined' || !timer ) return false;
    clearTimeout(timer);
    return true;
  },

  start_timer: function(callback, period) {
    if( typeof period == 'undefined' || !period ) {
      period = general.period;
    }
    return setTimeout(callback, period);
  },

  show_overlay: function() {
    var fancy_css = {
      'display': 'inline',
      'opacity': 0.5
    }
    $('#fancybox-overlay').css(fancy_css);
  },

  hide_overlay: function() {
    $('#fancybox-overlay').css('display', 'none');
  },

  send_request: function(href, parameters) {
    var type = 'POST';

    if( typeof parameters == 'undefined' || !parameters.length ) {
      parameters = '';
    }

    return $.ajax({
      type: type,
      url: href,
      data: parameters,
      dataType: 'html',
      beforeSend: function(msg, settings) {
        $.fancybox.showActivity();
        general.show_overlay();
        return true;
      }, 
      dataFilter: function(msg, type) {
        var data_array = [];
        if( msg.length ) {
          msg = $.base64Decode(msg);
          data_array = $.unserialize(msg);
        } else {
          data_array['error'] = 'empty result';
        }
        return data_array;
      },
      complete: function(msg){},
      success: function(msg) {
        $.fancybox.hideActivity();
        general.hide_overlay();
      }
    });
  },

  notice: function(msg, $sel, title) {
    var options = {
     'autoScale'     : true,
     'transitionIn'  : 'elastic',
     'transitionOut' : 'elastic'
    };
    
    if( typeof $sel != 'undefined' && $sel.length ) {
      options.orig = $sel;
    }
    if( typeof title != 'undefined' && title.length ) {
      options.title = title;
    } else {
      options.title = 'Notice';
    }
    $.fancybox(msg, options);
  },

  launch: function() {
    general.cookie_init();

    $('.toggler').click(function(e) {
      e.preventDefault();
      $(this).next().slideToggle('fast');
    });

    $('a.split_inline').live('click', function(e) {
      e.preventDefault();
      var $this = $(this);
      var link = $this.attr('href');
      var $container = $this.closest('.split_container');
      if( $container.length ) {

        general.send_request(link).success(function(msg) {
          if( typeof msg['data'] != 'undefined' ) {
            $container.replaceWith(msg['data']);

          } else if( typeof msg['error'] != 'undefined' ) {
            general.notice(msg['error'], $container);
          }
        });
      }
    });

    $('select.change_submit').change(function() {
      var $parent = $(this).closest("form");
      $parent.submit();
    });

    $('a.bsubmit').live("click", function(e) {
      e.preventDefault();
      var name = $(this).attr('name');
      var $parent = $(this).closest("form");
      if( $parent.length ) {
        if( typeof name != 'undefined' && name.length ) {
          var action = $parent.attr('action');
          var sep = '';
          if( action.length ) {
            sep = (action.indexOf('?') == -1 ? '?' : '&');
          }
          action += sep + 'action=' + name;
          $parent.attr('action', action);
        }
        if( typeof $(this).attr('link') != 'undefined' ) {
          $parent.append('<input type="hidden" name="'+$(this).attr('link')+'" value="0" />');
        }
        $parent.submit();
      }
    });

    $('span.frag').live("click", function(e) {
      e.preventDefault();
      var class_tags = $(this).attr('class');
      var tmp_array = class_tags.split(' ');
      link = $.base64Decode(tmp_array[0], true);
      $(location).attr('href', link);
    });

    // checkboxes multi-select
    $('form a.page_select').click(function(e) {
      e.preventDefault();

      var id = $(this).attr('href').substr(1);
      var status = -1;
      var $parent = $(this).closest("form");
      var checkboxes_array = $parent.find('input:checkbox');

      for(var i=0; i < checkboxes_array.length; i++) {
        var check = checkboxes_array[i].name.split('[');
        if( check[0] != id ) continue;

        if( status < 0 ) {
          status = $(checkboxes_array[i]).is(':checked');
          status = status?false:true;
        }
        $(checkboxes_array[i]).attr('checked', status);        
      }
    });

    $('form input[type=checkbox]').live("click", function(e) {

      if( !e.shiftKey && !e.ctrlKey ) {
        return;
      }

      var base = this.name.split('[');
      base = base[0];

      var status = false;
      var $parent = $(this).closest("form");
      var checkboxes_array = $parent.find('input[type=checkbox]');
      if( checkboxes_array.length < 8 ) return;

      if( $(this).is(':checked') ) {
        status = true;
      }

      if( e.shiftKey ) {
        for(var i=0; i<checkboxes_array.length; i++) {
          var tmp = checkboxes_array[i].name.split('[');
          if( tmp[0] != base ) continue;

          if( checkboxes_array[i].name == this.name ) {
            break;
          }
        }
        for(; i<checkboxes_array.length; i++) {
          var tmp = checkboxes_array[i].name.split('[');
          if( tmp[0] != base ) continue;

          $(checkboxes_array[i]).attr('checked', status);
        }
      } else {
        for(var i=checkboxes_array.length-1; i>=0; i--) {
          var tmp = checkboxes_array[i].name.split('[');
          if( tmp[0] != base ) continue;

          if( checkboxes_array[i].name == this.name ) {
            break;
          }
        }
        for(; i>=0; i--) {
          var tmp = checkboxes_array[i].name.split('[');
          if( tmp[0] != base ) continue;

          $(checkboxes_array[i]).attr('checked', status);
        }
      }
    });
  }
}

$(document).ready(function(){
  $jqWrap = general;
  general.launch();
});

