var menu_opened = false;

function mainNav(){
  $j('li.li-sub').mouseover(function(i){
      menu_opened = true;
      $j('div.drop').hide();
      $j('#' + $j(this).attr('rel')).width($j(this).width()).show();
  });
  /*
	$$('li.li-sub').each(function(s){
		Event.observe(s, 'mouseover', function(event) {
			var element = Event.element(event);
			element.addClassName('light');
			menu_opened = true;
			$$('div.drop').each(function(s){
				$(s).hide();
			});
			$(element.rel).show();
		}); 
	});
  */
	$('contentbox').observe('mouseover', function(event) {
		if (!menu_opened) return;
		$$('div.drop').each(function(s){
			$(s).hide();
		});
		$$('a[rel]').each(function(s){
			$(s).removeClassName('light');
		});
		menu_opened = false;
	});

	$('custhead').observe('mouseover', function(event) {
		if (!menu_opened) return;
		$$('div.drop').each(function(s){
			$(s).hide();
		});
		$$('a[rel]').each(function(s){
			$(s).removeClassName('light');
		});
		menu_opened = false;
	});

  $('edit-change-info').observe('mouseover', function(event) {
	    if (!menu_opened) return;
	    $$('div.drop').each(function(s){
	      $(s).hide();
	    });
	    $$('a[rel]').each(function(s){
	      $(s).removeClassName('light');
	    });
	    menu_opened = false;
	});
	 

	$$('div.drop').each(function(s){
		$(s).hide();
	});
}


var browsers = 'IE';
var config = {
		hovers: new Array(
			{
				parentId:'menu',
				tagName:'li',
				tagPosition:'all',
				className:'hover'
			}
		)
	};

/* CODE AREA */

/* hovers */

var initHovers = function()
{
	for (var index in config.hovers){
		var conf = config.hovers[index];
		var topLevelElement = document.getElementById(conf.parentId);
		if (topLevelElement){
			var subElements = topLevelElement.getElementsByTagName(conf.tagName);
			for (var i = 0; i < subElements.length; i++){
				if (conf.tagPosition == 'all' || (conf.tagPosition == 'children' && subElements[i].parentNode.id == conf.parentId))
				{
					subElements[i].configIndex = index;
					subElements[i].onmouseover = function(){
						this.className += ' ' + config.hovers[this.configIndex].className;
					};
					subElements[i].onmouseout = function(){
						this.className = this.className.replace(config.hovers[this.configIndex].className,'');
					};
				}
			}
		}
	}
}

/* common */

var currentState = {};

var opera = window.opera;
var safari = navigator.appVersion.indexOf("Safari") != -1;
var ie = document.all && !window.opera;
var ff = navigator.userAgent.indexOf("Firefox") != -1;

var addEvent = function(browsers,event,initMethod)
{
	if (opera && (browsers.match(/Opera/) || browsers.match(/all/))) {
		window.addEventListener(event, initMethod, false);
	} else if (safari && (browsers.match(/Safari/) || browsers.match(/all/))) {
		window.addEventListener(event, initMethod, false);
	} else if (ie && (browsers.match(/IE/) || browsers.match(/all/))) {
		window.attachEvent("on" + event, initMethod);
	} else if (ff && (browsers.match(/FF/) || browsers.match(/all/))) {
		window.addEventListener(event, initMethod, false);
	} else if (!ie && !ff && !safari && !opera) {
		if (window.addEventListener){
			window.addEventListener(event, initMethod, false);
		}
		else if (window.attachEvent){
			window.attachEvent("on" + event, initMethod);
		}
	}
}

addEvent(browsers,'load',initHovers);

var old_symbol = 'a';

var brandBySymbol = function(symbol){
	new Ajax.Updater("brands", "/ajax.php?function=showBySymbol&symbol=" + symbol);


	if(document.getElementById("sym_" + old_symbol))document.getElementById("sym_" + old_symbol).style.color = '#02638E';
	if(document.getElementById("sym_" + old_symbol))document.getElementById("sym_" + old_symbol).style.fontSize = '14px';

	if(document.getElementById("sym_" + symbol))document.getElementById("sym_" + symbol).style.color = '#E71939';
	if(document.getElementById("sym_" + symbol))document.getElementById("sym_" + symbol).style.fontSize = '16px';

	old_symbol = symbol;
}

var productsBySymbol = function(symbol){
	new Ajax.Updater("products", "/ajax.php?function=productsBySymbol&symbol=" + symbol);

	if(document.getElementById("sym_" + old_symbol))document.getElementById("sym_" + old_symbol).style.color = '#02638E';
	if(document.getElementById("sym_" + old_symbol))document.getElementById("sym_" + old_symbol).style.fontSize = '14px';

	if(document.getElementById("sym_" + symbol))document.getElementById("sym_" + symbol).style.color = '#E71939';
	if(document.getElementById("sym_" + symbol))document.getElementById("sym_" + symbol).style.fontSize = '16px';

	old_symbol = symbol;
}

getPaging = function(pages, page){
		if ($('paging')) {
			if (page > pages) pages = page;
			if (pages > 1) {
				var html = '';
				html += '<table cellpadding="2" cellspacing="0" border="0" align="center">';
				html += '<tr>';
				html += '<td' + (page == 1 ? ' style="visibility: hidden;"' : '') + '><a href="#" onclick="getPage(' + (page-1) + '); return false;">prev</a>&nbsp;&nbsp;</td>';
				if (page != 1 && pages > 1) html += '<td align="center"><a href="#" onclick="getPage(1); return false;"><span class="pbg2">1</span></a>';
				else if (page == 1) html += '<td><b>1</b></td>';
				var start = page - 3 > 1 ? page - 3 : 2;
				var end = page + 3 < pages ? page + 3 : pages - 1;
				if (page < 3 && pages > 4) { start = 2; end ++; if (page == 1) end ++; }
				if (page > pages - 2 && pages > 4) { start --; end = pages - 1; if (page == pages) start --; }
				if (page == 1 && page < 3 && pages <= 4) end ++;
				if (page == pages && page > pages - 2 && pages <= 4) start -- ;
				if (start < 2) start = 2;
				if (end > pages - 1) end = pages - 1;
				if (start > 2) html += '<td>|</td><td>...</td>';
				for (var i = start; i <= end; i ++) {
					if (i == page) html += '<td>|</td><td><b>' + i + '</b></td>';
					else html += '<td>|</td><td><a href="#" onclick="getPage(' + i + '); return false;">' + i + '</a></td>';
				}
				if (end < pages - 1) html += '<td>|</td><td>...</td>';
				if (page != pages && pages > 1) html += '<td>|</td><td><a href="#" onclick="getPage(' + pages + '); return false;">' + pages + '</a></td>';
				else if (page == pages) html += '<td>|</td><td><b>' + pages + '</b></td>';
				html += '<td' + (page == pages ? ' style="visibility: hidden;"' : '') + '>&nbsp;&nbsp;<a href="#" onclick="getPage(' + (page+1) + '); return false;">next</a></td>';
				html += '</tr>';
				html += '</table>';
				$('paging').update(html);
				$('paging2').update(html);
				$('paging').show();
				$('paging2').show();
			} else $('paging').hide();
		}
}

getPage = function(page){
	$('hrefPage').value = page;
	$('pageForm').submit();
}

String.prototype.stripSlashes = function()
{
	return this.replace(/\\/g, '');
}


/* Descript */

var inputOnFocus = function(id, txt, type) 
{
  if ($j('#' + id).val() == txt)
  {
    try {
      document.getElementById(id).setAttribute("type", type);  
    } catch (e) {
     
    }
    $j('#' + id).val('').focus().removeClass('descript').parent("div.input").removeClass('descript');
  }
}

var inputOnBlur = function(id, txt, type) 
{
  if ($j('#' + id).val() == '')
  {
    try {
      document.getElementById(id).setAttribute("type", "text");  
    } catch (e) {
     
    }
    $j('#' + id).val(txt).addClass('descript').parent("div.input").addClass('descript');
  }
}

var showToHide = function(arId)
{
  /*
  var arId = [
        { id : 'g_r_name', txt : "name", type: 'text', title: "Name" },
        { id : 'g_r_password', txt : "password", type: 'password' },
        { id : 'g_r_confirm_password', txt : "confirm password", type: 'password' }
      ];
  */
  for (i in arId)
  {
    if (arId[i].id)
    {
      if (!arId[i].title) 
      {
        arId[i].title = arId[i].txt;
      }
      var inpId = '#' + arId[i].id;
      $j(inpId).focus( function () {
        inputOnFocus(this.id, $j(this).attr('txt'), $j(this).attr('savetype'));
      }).blur(function () {
        inputOnBlur(this.id, $j(this).attr('txt'), $j(this).attr('savetype'));
      }).each(function (ii) {
        if (!$j(this).val() || $j(this).val()==arId[i].txt)
        {
          try {
            document.getElementById($j(this).attr('id')).setAttribute("type", "text");
          } catch (e) {
           
          }
          $j(this).val(arId[i].txt).addClass('descript').parent("div.input").addClass('descript');
        }
        
      }).attr('txt', arId[i].txt).attr('savetype', arId[i].type).attr('title',arId[i].title);
    }
  }
}
