// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
var wait_time = 0.2;           //second
var hide_time = 1.5;           //second
var current_menu = 0;       //reference
var menu_count = 4;
var menuObject = Array();
var submenuObject = Array();         
var timer;
var current_lang;             //current language
var current_uri;
//var path = "http://www.puchland.com"
var path = ""
//var path = "http://localhost"



//----------------------CART COUNTRIES SELECT--------------------------------------------

function cart_countries_sel_show() {
    $('country_sel_list').toggle();
}

function cart_countries_sel_select( countryname_id, countryname ) {
    $('country_sel_list').hide();
    $('cart_delivery_costs').setStyle({ visibility: 'hidden'});
    $('country_sel').down('.btn').down().update( countryname );
    new Ajax.Updater('cart_shipping', '/cart/change_language/'+countryname_id, {asynchronous:true});
}


//--------------------------LANGUAGE FUNCTION--------------------------------------------
function change_language(lang)
{
 if(lang != '') {
   window.location = path + '/' + lang;
 }
 else {
      window.location = path;}
}

function set_current_lang(lang, uri)
{
  current_lang = lang;	
  current_uri = uri;
}

//--------------------------MENU FUNCTION----------------------------------------------

function clearTimerMenu()
{
	try{clearTimeout(timer);}catch(err){}
}

function showObject(number)
{
	try
	{
		clearTimerMenu();
		if(submenuObject[number] != current_menu)
		{
		    if(current_menu != 0) hideObject(current_menu.number)
			current_menu = submenuObject[number];
		}
		submenuObject[number].style.display = 'block';
	}
	catch(err){}
}

function showObjectAfterTime(number)
{
	clearTimerMenu();
	timer=setTimeout("showObject("+number+");", wait_time*1000);
}


function hideObject(number)
{
	try{submenuObject[number].style.display = 'none';}catch(err){}
}

function hideObjectAfterTime(number)
{
	clearTimerMenu();
	timer=setTimeout("hideObject("+number+");", hide_time*1000);
}

start_menu = function()
{
    document.getElementById("menu").className = "noli"
	
	for(i=1; i<=menu_count; i++)
	{
		
		menuObject[i] = document.getElementById("menu"+i)
		menuObject[i].number = i 
		submenuObject[i] = menuObject[i].getElementsByTagName("UL")[0]
		submenuObject[i].number = i

		menuObject[i].onmouseover = function(){showObjectAfterTime(this.number)}
        menuObject[i].onmouseout = function(){hideObjectAfterTime(this.number)}
		submenuObject[i].onmouseover = function(){clearTimerMenu()}
		submenuObject.onmouseout = function(){hideObjectAfterTime(this.number)}
	}
}

window.onload = start_menu;


//-----------------------------INNE-------------------------------------
function getElementsByClassName(cn){
  var arr = new Array(); 
  var els = document.getElementsByTagName("*");
  var exp= new RegExp("^(.* )?"+cn+"( .*)?$", "g");
  for (var i = 0; i < els.length; i++ ){
    if (exp.test(els[i].className)){
      arr.push(els[i]);
    }
  }
  return arr;
}

function show_s(name)
{
	document.getElementById('s'+name).style.visibility = 'visible'
}

function hide_s(name)
{
	document.getElementById('s'+name).style.visibility = 'hidden'
}


function check_first()
{
	document.forms[0][0].focus()	
}







//Event.observe(document, 'dom:loaded', initScrollingComments);
/*
function initScrollingComments(){
    scrollingComments = new ScrollingComments();
}


ScrollingComments = Class.create();
ScrollingComments.prototype = {

  initialize: function() {

    if ( $('scrolling_comments') ) {
      new PeriodicalExecuter( this._scrollComments, 10 );
    }
    
  },

  _scrollComments: function() {
    var list = $('scrolling_comments_list');
    var item = list.down();
    list.insert(item);
    list.setStyle( {top: '0px'} );
    new Effect.Move(list, { x: 0, y: -90, mode: 'relative', duration: 1.0, fps: 50 });
  }

}
*/
Event.observe(document, 'dom:loaded', initPage);

function initPage() {
    $$('.change_radio').each(function(item){
        Event.observe(item, 'click', Radios.change_radio.bind(item))
    })
    $$('.bestseller').each(function(item){
        Event.observe(item, 'click', function(e){
            e = (e) ? e : window.event;
            var a = e.element().up('.product').down('a');
            window.location = a.href;
        })
    })
}

Radios = {
    change_radio : function(e) {
        e = (e) ? e : window.event;
        var img = e.element();
        var r = img.previous('input').checked = true;
    }
}