//$Id: pageoptions.js,v 1.10 2011/10/02 11:10:59 sasha@netcomposite.com Exp $
var waitCommonAjax =
 '<div id="PageOptionsLoading">' +
  '<p><img src="/assets/images/icons/ic_loading_processing_gray.gif" alt="Loading" />' +
  'Loading...</p>' +
 '</div>';

function selectSpreadOption(sp,title)
{
 var id = new Array('c','l','r','f','a','t');
 var k;
 if (sp != 't')
 {
  $('TagScreenIndex').style.display = 'none';
  $('SpreadCategory').style.display = 'block';
 } else
 {
  $('SpreadCategory').style.display = 'none';
  $('TagScreenIndex').style.display = 'block';
 }
 for (var i=0; i < id.length; i++)
 {
  k = $(id[i]);
  if (k)
  {
   if (sp == id[i])
   {
    k.style.display = 'block';
   } else
   {
    k.style.display = 'none';
   }
  }
 }
 $('SpreadOptionsSelect').innerHTML = title;
 if ($(sp) || sp == 't')
 {
  setSpreadOptionPage(sp, spreadOptionCurrentPage[sp])
 } else
 {
  return true;
 }
 $('SpreadOptionsSelect').className='';
 $('SpreadOptions').style.display='none'
 return false;
}

function selectSpreadOptionCurrent()
{
 var id = new Array('l','r','f','a');
 for (var i=0; i < id.length; i++)
 {
  k = $(id[i]);
  if (k && k.style.display != 'none')
  {
   return k.id;
  }
 }
 return false;
}

function selectSpreadOptionPrev()
{
 var e = selectSpreadOptionCurrent();
 if (!e) return false;
 if (!hashPages[e]) return false;
 var prev = spreadOptionCurrentPage[e] - 1;
 if (prev < 0 || prev * 5 > hashPages[e].length)
 {
  prev = parseInt((hashPages[e].length - 1) / 5);
 }
 setSpreadOptionPage(e, prev);
 return false;
}

function selectSpreadOptionNext()
{
 var e = selectSpreadOptionCurrent();
 if (!e) return false;
 if (!hashPages[e]) return false;
 var next = spreadOptionCurrentPage[e] + 1;
 if (next * 5 > hashPages[e].length - 1 || next < 0)
 {
  next = 0;
 }
 setSpreadOptionPage(e, next);
 return false;
}

function setSpreadOptionPage(e, p)
{
 var list = $(e);
 var res = '';
 spreadOptionCurrentPage[e] = p;
 if (hashPages[e])
 {
  for (i = p * 5; i < hashPages[e].length && i < (p + 1) * 5; i++)
  {
   res += '<a href="' + hashPages[e][i]['url'] + '">';
   res += '<img width="150" height="150" src="' + hashPages[e][i]['img'] + '" alt="' + hashPages[e][i]['summary'] + '"/>';
   res += ' <span><strong>' + hashPages[e][i]['currency'] + ' ' + hashPages[e][i]['price'] + '</strong>' + hashPages[e][i]['cn'] + '</span></a>';
  }
  $(e).innerHTML = res;
 }
 $('currentPagerValue').innerHTML = (spreadOptionCurrentPage[e] + 1) + ' of ' + spreadOptionCurrentPagesTotal[e];
 if (spreadOptionCurrentPagesTotal[e] > 1)
 {
  $('spreadPrev').style.display = 'inline';
  $('spreadNext').style.display = 'inline';
 } else
 {
  $('spreadPrev').style.display = 'none';
  $('spreadNext').style.display = 'none';
 }
 return false;
}

function listOptionSelect(a, b)
{
 var id = new Array('listOptionRPP','listOptionMode','listOptionOrderName','listOptionOrderPrice');
 for (var i=0; i < id.length; i++)
 {
  if (a != id[i])
  {
   $(id[i] + 'Selector').className='';
   $(id[i]).style.display='none';
  }
 }
 if ($(a).style.display=='none')
 {
  b.parentNode.style.position='relative';
  b.className='over';
  $(a).style.display='block';
 } else
 {
  b.className='';
  $(a).style.display='none';
  b.parentNode.style.position='static';
 }
 return false;
}


var ajaxRequests = {};

function commonAjaxRequest(opt, id, addon)
{
 if (!$(id)) return true;
 $(id).innerHTML = waitCommonAjax;
 if (addon)
 {
  value = addon;
  value['id'] = opt;
 } else
 {
  var value = new Array();
  value['id'] = opt;
 }
 ajaxRequests[opt] = id;
 var req=new j2httpRequest_js;
 req.onreadystatechange = function()
 {
  if (req.readyState == 4)
  {
   var result=req.responseJS;
   if (result['html'])
   {
    if (ajaxRequests[result['id']] && $(ajaxRequests[result['id']]))
    {
     $(ajaxRequests[result['id']]).innerHTML = result['html'];
     result['html'] = result['html'].replace(/[\n\r\t]/g,' ');
     var re = new RegExp(/<script.*?>.*?<!--(.*?)-->.*?<\/script>/gi);
     var m = re.exec(result['html']);
     if (m != null)



     {
      eval(m[1]);
     }
    }
   }
  }
 }
 if (this.script_name)
 {
  value['script_name'] = this.script_name;
 }
 if (this.tab_name)
 {
  value['tab_name'] = this.tab_name;
 }
 if (this.meta_data)
 {
  value['meta_data'] = this.meta_data;
 }
 req.open('GET', '/assets/templ/include_globals/options.php', true);
 req.send({'mode':'uri','value':value});
 return false;
}

function navScrollUp(c, categoryScrollRows)
{
 var up = $(c + 'NavigationScrollUp');
 var down = $(c + 'NavigationScrollDn');
 var list = $(c + 'NavigationScroll').getElementsByTagName("li");
 var off = 0;
 for (i = 0; i < list.length; i++)
 {
  if (list[i].className != 'ItemHidden')
  {
   off = i;
   break;
  }
 }
 off -= categoryScrollRows;
 if (off < 0)
 {
  off = 0;
 }
 for (i = 0; i < list.length; i++)
 {
  if (i >= off && i < off + categoryScrollRows)
  {
   list[i].className = '';
  } else
  {
   list[i].className = 'ItemHidden';
  }
 }
 if (off == 0)
 {
  up.className = '';
 } else
 {
  up.className = 'over';
 }
 if (off + categoryScrollRows < list.length)
 {
  down.className = 'over';
 } else
 {
  down.className = '';
 }
 return false;
}

function navScrollDn(c, categoryScrollRows)
{
 var up = $(c + 'NavigationScrollUp');
 var down = $(c + 'NavigationScrollDn');
 var list = $(c + 'NavigationScroll').getElementsByTagName("li");
 var off = 0;
 for (i = 0; i < list.length; i++)
 {
  if (list[i].className != 'ItemHidden')
  {
   off = i;
   break;
  }
 }
 off += categoryScrollRows;
 if (off + categoryScrollRows >= list.length)
 {
  off = list.length - categoryScrollRows;
 }
 if (off < 0)
 {
  off = 0;
 }
 for (i = 0; i < list.length; i++)
 {
  if (i >= off && i < off + categoryScrollRows)
  {
   list[i].className = '';
  } else
  {
   list[i].className = 'ItemHidden';
  }
 }
 if (off == 0)
 {
  up.className = '';
 } else
 {
  up.className = 'over';
 }
 if (off + categoryScrollRows < list.length)
 {
  down.className = 'over';
 } else
 {
  down.className = '';
 }
 return false;
}

function checkEmail(email)
{
 var reg = new RegExp(/^[a-z0-9_.-]+@[a-z0-9_.-]{2,32}\.[a-z]{2,4}$/i);
 if (reg.test($(email).value))
 {
  return true;
 } else
 {
  alert("Please enter valid e-mail address.");
 }
 return false;
}

function selectOptionCategories(c)
{
 for (var i in queries)
 {
  var t = $(queries[i]['id']);
  if (t)
  {
   if (i == c)
   {
    t.style.display = 'block';
   } else
   {
    t.style.display = 'none';
   }
  }
  var page = queries[c]['page'] + 1;
  $('listOptionOrderPageSelector').innerHTML='Page ' + page + ' of ' + queries[c]['pages'];
  var pager = '';
  for (i = 0; i < queries[c]['pages']; i++)
  {
   page = i + 1;
   pager = pager + '<li><a onclick="return selectOptionPage(\'' + c + '\',' + i + ')" href="#">Page ' + page + ' of ' + queries[c]['pages'] + '</a></li>' + "\n";
  }
  $('listOptionOrderPage').innerHTML=pager;
  $('listOptionCategoriesSelector').innerHTML=c;
  listIndexOptionSelect('listOptionCategories', $('listOptionCategoriesSelector'));
 }
 return false;
}

function selectOptionPage(c, p)
{
 var list = $(queries[c]['id']);
 var cont = '';
 for (i = p * 4; i < queries[c]['length'] && i < (p + 1) * 4; i++)
 {
    cont = cont + '<div class="Tray">';
    cont = cont + '<a href="' + queries[c]['search'][i]['url'] + '">';
    cont = cont + '<img src="' + queries[c]['search'][i]['image'] + '" ';
    cont = cont + 'alt="' + queries[c]['search'][i]['summary'] + '"/></a>';
    cont = cont + '<h1>' + queries[c]['search'][i]['cn'] + '</h1>';
    cont = cont + '<p><a href="' + queries[c]['search'][i]['url'] + '">';
    cont = cont + queries[c]['search'][i]['summary'] + '</a></p>';
    cont = cont + '<strong>' + queries[c]['search'][i]['price'] + '</strong>';
    cont = cont + '</div>';
 }
 queries[c]['page'] = p;
 var page = p + 1;
 $('listOptionOrderPageSelector').innerHTML='Page ' + page + ' of ' + queries[c]['pages'];
 list.innerHTML = cont;
 listIndexOptionSelect('listOptionOrderPage', $('listOptionOrderPageSelector'));
 return false;
}

function listIndexOptionSelect(a, b)
{
 var id = new Array('listOptionCategories','listOptionOrderPage');
 for (var i=0; i < id.length; i++)
 {
  if (a != id[i])
  {
   $(id[i] + 'Selector').className='';
   $(id[i]).style.display='none';
  }
 }
 if ($(a).style.display=='none')
 {
  b.parentNode.style.position='relative';
  b.className='over';
  $(a).style.display='block';
 } else
 {
  b.className='';
  $(a).style.display='none';
  b.parentNode.style.position='static';
 }
 return false;
}

function newLoadNavSet(current, id)
{
	if (current.tagName.toUpperCase()!="A") return false;
	var params = current.href.parseQuery();
	var block = current.up('div.content');
	block.down('div.ContentLandingID').hide();
	block.down('div.ContentLandingLoading').appear({duration: 1.5});
	new Ajax.Updater(block,'/assets/templ/include_globals/'+id+'.html',{parameters:params});
	return false;
}

function naNavSet(ntp, idNew, idBlock)
{
 var value = new Array();
 if (idNew)
 {
  value['id'] = idNew;
 } else
 {
  value['id'] = 'index_new_arrival';
 }
 value['ntp'] = ntp;
 var req=new j2httpRequest_js;
 req.onreadystatechange = function()
 {
  if (req.readyState == 4)
  {
   var result=req.responseJS;
   if (result['html'])
   {
    $('ajaxNewArrival').innerHTML = result['html'];
   }
  }
 }
 req.open('GET', '/assets/templ/include_globals/options.php', true);
 req.send({'mode':'uri','value':value});
 if (idBlock)
 {
  $(idBlock).style.display = 'none';
  $(idBlock+'Loading').style.display = 'block';
 } else
 {
  $('ContentLandingNewArrivalsID').style.display = 'none';
  $('ContentLandingNewArrivalsLoading').style.display = 'block';
 } 
 return false;
}

function sendUrlWidget(el)
{
	if (el.tagName.toUpperCase()!="A") return false;
	if (el.href.indexOf('.html') == -1)
	{
		var url = el.href.replace('?','index-carousel.html?');
	} else
	{
		var url = el.href.replace('.html','-carousel.html');
	}
	var req=new j2httpRequest_js;
	req.caching = true;
	req.onreadystatechange = function()
	{
		if (req.readyState == 4)
		{
			for (var id in req.responseJS)
			{
				try
				{
					var elem = $(id);
					if (elem) elem.update(req.responseJS[id]);

				} catch(ex) {};
			}
		}
	}
	req.open('GET', url, false);
	req.send();
	return false;
}

function naNavNewSet(current,id)
{
 var req=new j2httpRequest_js;
 req.onreadystatechange = function()
 {
  if (req.readyState == 4)
  {
   var result=req.responseJS;
   if (result['html'])
   {
    $(id).innerHTML = result['html'];
   }
  }
 }
 req.open('GET', '/assets/templ/include_globals/options.php', true);
 req.send({'mode':'uri','value':value});
 $('ContentLandingNewArrivalsID').style.display = 'none';
 $('ContentLandingNewArrivalsLoading').style.display = 'block';
 return false;
}
