var active_popup = 1

function clear_selection() //odznaczas aktywne pole formularza
{
	document.getElementById('parameters').focus();
}

function set_color(nr, color) //koloruje wiersz gdzie ilosc pozycji jest wieksza niz 1
{
k='cart_item_product[' + nr + '][quantity]';
if(document.forms[0][k].value == '0'){
 document.getElementById('parameters').rows[nr].style.background='none';}
else {
 document.getElementById('parameters').rows[nr].style.background='url(/images/graphics/'+color+'.gif)';
 document.getElementById('parameters').rows[nr].style.background='url(/images/graphics/'+color+'.gif)';
}
clear_selection();
}

function overlay(id, lang)
{
    document.body.style.overflowY = 'hidden';
	document.getElementById('overlay').style.display = 'block';
	selects = document.getElementsByTagName("select")
	for(i=0; i< selects.length; i++)
	{
		selects[i].style.visibility = 'hidden'
	}
	change_links(id)
	Element.hide('popup_right'); new Ajax.Updater('popup_right', '/product/popup?popup_id='+id+'&language=' + lang, {asynchronous:true, evalScripts:true, onComplete:function(request){change_links(id);Element.show('popup_right')}}); return false;
}

function overlay_hide()
{
    document.body.style.overflowY = 'visible';
	selects = document.getElementsByTagName("select")
	for(i=0; i< selects.length; i++)
	{
		selects[i].style.visibility = 'visible'
	}
	document.getElementById('overlay').style.display = 'none';
    //aby odswiezyc widok w operze ponizszy kod
	var skad = document.body.parentNode.scrollTop;
	window.scrollTo(0,skad + 2);
}

function change_links(active)
{
	document.getElementById('popup' + active_popup).className = '';
	active_popup = active
	document.getElementById('popup' + active_popup).className = 'active';
	document.getElementById('popup' + active_popup).blur();
}

photos = new Array()
indicator = new Image()
indicator.src = '/images/graphics/indicator_square.gif'
function photo_change(number, text_lang)
{
	var pom = number - 1;
	if (number > 0) document.getElementById('photo').src = photos[pom].src
	var text = text_lang + ': ';
	var ile = photos.length
    if (number < 0)	pom = 0; 
	for(i=0; i<ile; i++)
	{
		if (pom==i)
		  text = text + '<span>'+(i+1)+'</span>';
		else text = text + '<a href="javascript: photo_change('+(i+1)+',\''+text_lang+'\');">'+(i+1)+'</a>';
	}
	document.getElementById('photos').innerHTML = text
}

var Puchland = new Hash();
Event.observe(document, 'dom:loaded', initProduct);
function initProduct() {
    Puchland.individual_sizes.lastStyle();
    Puchland.individual_sizes.current_index = $('parameters').select('.size').size();
    Event.observe('individual_size', 'submit', Puchland.individual_sizes.newSize);
    $$('.quantity').each(function(item){
      Event.observe(item, 'change', Puchland.highlightRows.changeBackground);
    })
    $('parameters').select('.remove').each(function(item){
      Event.observe(item, 'click', Puchland.individual_sizes.removeSize);  
    })
}
Puchland.individual_sizes = {
    current_index: 0,
    add_current_index: function() {
        Puchland.individual_sizes.current_index += 1;
        return Puchland.individual_sizes.current_index;
    },
    newSize: function(e){
        e = (e) ? e : window.event;
        f = e.element();
        error = $('error_individual');
        sizes = $$('.size');
        a = new Array();
        b = new Array();
        sizes.each(function(item, i){
            t = item.down('span').innerHTML;
            unit = t.include('см') ? 'см' : 'cm';
            w = t.split(unit)[0].replace(/[^(\d*)]/gi, "");
            l = t.split(unit)[1].replace(/[^(\d*)]/gi, "");
            a[i] = w;
            b[i] = l;
        })
        
        new_w = parseInt($('individual_size_width').getValue());
        new_l = parseInt($('individual_size_length').getValue());
        
        var exist = false;
        a.each(function(item, i){
            if((item == new_w && b[i] == new_l) || (item == new_l && b[i] == new_w)) {
                exist = true;
            }
        })
        if(exist == true)
            error.show();
        else {
              error.hide();
              f.request({parameters: {i: Puchland.individual_sizes.add_current_index()},onSuccess: function(response) {
                      $('parameters').select('tr').last().insert({after: response.responseText})
                      inserted = $$('.quantity').last();
                      remove = $('parameters').select('.remove').last();
                      Event.observe(inserted, 'change', Puchland.highlightRows.changeBackground);
                      Event.observe(remove, 'click', Puchland.individual_sizes.removeSize);
                      tr = inserted.up('tr');
                      new Effect.Appear(tr);
                      Puchland.individual_sizes.setDisplay();
              }});
        }
        e.stop();
    },
    removeSize: function(e){
        e = (e) ? e : window.event;
        var a = e.element();
        var id = a.id.split('_')[1];
        var tr = a.up('tr');
        var url = location.protocol + '//' + location.host + '/' + location.pathname.split('/')[1];
        new Ajax.Request(url + '/individual_size/delete', {
			method: 'post',
			parameters: 'id=' + id,
			onSuccess: function() {
                            new Effect.Fade(tr, {afterFinish: function(){
                              tr.remove();
                              Puchland.individual_sizes.setDisplay();        
                            }});
                        }})
     
        e.stop();
    },
    lastStyle: function(){
        t = $('parameters');
        t.select('.last').invoke('removeClassName', 'last');
        tr = t.select('tr').last();
        if(tr.hasClassName('info')) tr = tr.previous();
        tr.select('td').invoke('addClassName', 'last');
    },
    infoChangeDisplay: function(){
        var p = $('parameters');
        var c = p.select('.individual').size();
        if(c>0) p.down('.info').show();
          else p.down('.info').hide();
    },
    setDisplay: function(){
        p = $('parameters')
        Puchland.individual_sizes.infoChangeDisplay();
        Puchland.individual_sizes.lastStyle();
    }
}
Puchland.highlightRows = {
    changeBackground: function(e){
        e = (e) ? e : window.event;
        s = e.element();
        tr = s.up('tr');
        (parseInt(s.value) == 0) ? tr.removeClassName('light') : tr.addClassName('light');
        e.stop();
    } 
}