// level 0 page

function swapImage(inImageName, inImageSrc)
{
	$(inImageName).src = inImageSrc;
}

function initYearPage()
{
	initWorksThumbs()
}

function initWorksThumbs()
{
	hideWorks();
	var first = true;
	$A( document.getElementsByIdPrefix('link_', document.getElementsByClassName('works_thumbs', 'content')[0]) ).each(function(value, index) {
		var medImageLink = 	$( value.id.split('link_')[1] ).getElementsByTagName( 'A' )[0];
		value.href = medImageLink.href;
		medImageLink.href = 'javascript:void(0)';
		Event.observe(value, 'mouseover', onWorkLinkOver, false);
		if(first){
			try{
				Element.show(value.id.split('link_').join(''));
			}catch(e){}
			first = false;
		}
	});
}

function hideWorks()
{
	$A($('work_previews').getElementsByTagName('div')).each(function(value, index) {
		Element.hide(value);
	});
}

function onWorkLinkOver(inEvt){
	var prodId = Event.findElement(inEvt, 'A').id.split('link_').join('');
	hideWorks();
	try{
		Element.show($(prodId));
	}catch(e){}
}

function onWorkLinkClick(inEvt){
	var prodId = Event.findElement(inEvt, 'A').id.split('link_').join('');
	hideWorks();
	try{
		Element.show($(prodId));
	}catch(e){}
}

function initWorkPage()
{
	window.top.initLightbox();
}


function initLevel1Links()
{
	var list = $('list_products');
	var liEls = list.getElementsByTagName('li');
	productsListBaseDim = (Element.getDimensions(liEls[0]).width + 15);

	//resize list to fit horizontally
	var reqW = liEls.length * productsListBaseDim;
	Element.setStyle(list, {width: String(reqW)+'px'})

	if(liEls.length > 6){
		//init arrows
		Event.observe($('link_arrow_left'), 'click', onLeftArrowClick, false);
		Event.observe($('link_arrow_right'), 'click', onRightArrowClick, false);
		$('link_arrow_left').href = 'javascript:void(0)';
		$('link_arrow_right').href = 'javascript:void(0)';
	}else{
		Element.hide('link_arrow_left');
		Element.hide('link_arrow_right');
	}

	//set the iframe src to the first link href
	document.getElementsByName('prodframe')[0].src = document.getElementsByClassName('list_products_link', 'list_products')[0].href;
}

function onProdLinkClick(inEvt){
	var prodId = Event.element(inEvt).id.substr(5);
	$A(document.getElementsByIdPrefix('prod_', 'prod_container')).each(function(value, index) {
		Element.hide(value);
	});
	try{
		Element.show($(prodId));
	}catch(e){}
}

function onLeftArrowClick()
{
	var currentPos = Number(Element.getStyle($('list_products'), 'left').split('px').join(''));
	var nextPos = currentPos + productsListBaseDim;
	if(nextPos > 0){
		Element.setStyle($('list_products'), {left: '0px'})
		return;
	}
	productsListSlide = new Animator({
							duration: 500
						});
	productsListSlide.addSubject(new NumericalStyleSubject($("list_products"), 'left', currentPos, nextPos));
	productsListSlide.toggle();
}

function onRightArrowClick()
{
	var currentPos = Number(Element.getStyle($('list_products'), 'left').split('px').join(''));
	var nextPos = currentPos - productsListBaseDim;
	var minLeftPos = (-1)* (Number(Element.getStyle($('list_products'), 'width').split('px').join('')) - Number(Element.getStyle($('list_products_container'), 'width').split('px').join('')));
	minLeftPos += 15;
	if(nextPos < minLeftPos){
		Element.setStyle($('list_products'), {left: minLeftPos+'px'})
		return;
	}
	productsListSlide = new Animator({
							duration: 500
						});
	productsListSlide.addSubject(new NumericalStyleSubject($("list_products"), 'left', currentPos, nextPos));
	productsListSlide.toggle();
}

// level 1 page

function initproductsLinks()
{
	hideProducts();
	var first = true;
	$A(document.getElementsByClassName('list_product_views', 'list_subcat')).each(function(value, index) {
		value.href = 'javascript:void(0)';
		Event.observe(value, 'click', onProductLinkClick, false);
		if(first){
			try{
				Element.show(value.id.split('linktoproduct_').join(''));
			}catch(e){}
			first = false;
		}
	});
}

function initLargeImagesLinks()
{
	var elements = new Array();
	elements = elements.concat( document.getElementsByClassPrefix('jspop') );
	elements = $A(elements);
	elements.each(function(el, index){
		if(el.tagName == 'A'){
			el.onclick = onPopupOpenRequest.bindAsEventListener(el);
		}
	});
}

function onProductLinkClick(inEvt)
{
	var prodId = Event.findElement(inEvt, 'a').id.split('linktoproduct_').join('');
	hideProducts();
	try{
		Element.show($(prodId));
	}catch(e){}
}

function hideProducts()
{
	$A(document.getElementsByClassName('prod_box', 'prod_container')).each(function(value, index) {
		Element.hide(value);
	});
}

function onPopupOpenRequest(inEvt)
{
	var el = Event.findElement(inEvt, 'A');
	var pUrl = el.getAttribute('href');
	centerPopup(pUrl, 'jspop', screen.width/2, screen.height/2);
	return false;
}

function centerPopup(popup_url,popup_name,popup_with,popup_height,reopen,myScrollbar,myLeftPos,myTopPos) {
  if(!myLeftPos)  {myLeftPos    = 5;}
  if(!myTopPos)   {myTopPos     = 15;}
  if(!myScrollbar){myScrollbar  = 1;}

  var popup_left                = (window.screen.width/2)  - (popup_with/2 + myLeftPos);
  var popup_top                 = (window.screen.height/2) - (popup_height/2 + myTopPos);
  if ((typeof popup_window != 'object') || (typeof popup_window == 'object' && popup_window.closed)) {
    if (document.all) {
      var xyPos                 = 'left=' + popup_left + ',top=' + popup_top;
    } else {
      var xyPos                 = 'screenX=' + popup_left + ',screenY=' + popup_top;
    }

    popup_window                = window.open(popup_url, popup_name, "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=" + myScrollbar + ",resizable=yes,width=" + popup_with + ",height=" + popup_height + ",copyhistory=no," + xyPos + "");
    popup_window.opener         = self;
    popup_window.focus();
    winUrl                      = popup_url;
    windowWidth                 = popup_with;
    windowHeight                = popup_height;
  } else {
    if ((winUrl != popup_url) || reopen) {
      popup_window.location.href = popup_url;
    }
    if ((windowWidth + windowHeight > 0) && (popup_with != windowWidth || popup_height != windowHeight || myLeftPos != diffWidth || myTopPos != diffHeight)) {
      var newWidth              = popup_with  - windowWidth;
      var newHeight             = popup_height - windowHeight;
      popup_window.resizeBy(newWidth,newHeight);
      popup_window.moveTo(popup_left,popup_top);
    }
    popup_window.focus();
    winUrl                      = popup_url;
    windowWidth                 = popup_with;
    windowHeight                = popup_height;
  }
  diffWidth                     = myLeftPos;
  diffHeight                    = myTopPos;
}

function simpleCenter(w, wWidth, wHeight)
{
	w.moveTo((window.screen.width/2 - wWidth/2), (window.screen.height/2 - wHeight/2));
}
