$(window).ready(function(){
  $('div').pngFix();
  //$(window).resize(function() {setContainerPosition()});
  
  $("input.inner-label").focus(function(){
    if(this.value == $(this).attr('default')) this.value = '';
  }).blur(function(){
    if(this.value.length == 0) this.value = $(this).attr('default');
  }).blur();
  
  $('#login_form').submit(function(){
    var error = '';
    if(innerLabelInputIsEmpty('password',this)) error = dictionary.loginPasswordRequired;
    if(innerLabelInputIsEmpty('username',this)) error = dictionary.loginUsernameRequired;
    if(error.length) {
      alert(error);
      return false;
    }
    return true;
  })
  
  var flashMenu = false;
  $('a.products').click(function(){
	discountMenu = false
	$('a.discount').removeClass('selected');
	
    flashMenu = !flashMenu;
    $("#flashMenu").get(0).showMenu("products",flashMenu);
    if(flashMenu) $(this).addClass('selected');
    else $(this).removeClass('selected');
    return false;
  });
  
  var discountMenu = false
  $('a.discount').click(function(){
	flashMenu = false
	$('a.products').removeClass('selected');
	
    discountMenu = !discountMenu;
    $("#flashMenu").get(0).showMenu("discount",discountMenu);
    if(discountMenu) $(this).addClass('selected');
    else $(this).removeClass('selected');
    return false;
  });
  
  // kereskedo kereses
  $('#dealer_search').click(function() {
	  var params = '';
	  var country = $('#dealers_country').val();
	  var state   = $('#dealers_state').val();
	  var city    = $('#dealers_city').val();
	  var name    = $('#dealers_name').val();
	  if (country != '') {
		  params += '/country/'+country;
	  }
	  if (state != '') {
		  params += '/state/'+state;
	  }
	  if (city != '') {
		  params += '/city/'+city;
	  }
	  if (name != '') {
		  params += '/name/'+name;
	  }
	  document.location.href = "/dealers"+params;
	  return false;
  });
  
  // fooldali kerekpar kereso
  $('#bicycle_search').click(function() {
    var params = '';
    var brand = $('#bicycle_brand').val();
    var type  = $('#bicycle_type').val();
    var year  = $('#bicycle_year').val();
    var price_from  = $('#bicycle_price_from').val();
    var price_to    = $('#bicycle_price_to').val();
    
    if (brand != 0) {
      params += '/brand/'+brand;
    }
    if (type != 0) {
      params += '/type/'+type;
    }
    if (year != 0) {
      params += '/year/'+year;
    }
    if (price_from != dictionary.Artol && price_from != '') {
      params += '/price_from/'+price_from;
    }
    if (price_to != dictionary.Arig && price_to != '') {
      params += '/price_to/'+price_to;
    }
    document.location.href = "/bicycle_search"+params;
    return false;
  });
  
  // fooldali termek kereso
  $('#product_search').click(function() {
    var params = '';
    var brand = $('#product_brand').val();
    var type = $('#product_type').val();
    var year = $('#product_year').val();
    var price_from = $('#product_price_from').val();
    var price_to = $('#product_price_to').val();

    if (brand != 0) {
      params += '/brand/' + brand;
    }
    if (type != 0) {
      params += '/type/' + type;
    }
    if (year != 0) {
      params += '/year/' + year;
    }
    if (price_from != dictionary.Artol && price_from != '') {
      params += '/price_from/' + price_from;
    }
    if (price_to != dictionary.Arig && price_to != '') {
      params += '/price_to/' + price_to;
    }
    document.location.href = "/product_search" + params;
    return false;
  });
  
  // artol arig szovegek cserelgetese
  $('#bicycle_price_from, #product_price_from').focus(function() {
	  if ($(this).val() == dictionary.Artol) {
		  $(this).val('');
	  }
  });
  $('#bicycle_price_from, #product_price_from').blur(function() {
	  if ($(this).val() == '') {
		  $(this).val(dictionary.Artol);
	  }
  });
  $('#bicycle_price_to, #product_price_to').focus(function() {
	  if ($(this).val() == dictionary.Arig) {
		  $(this).val('');
	  }
  });
  $('#bicycle_price_to, #product_price_to').blur(function() {
	  if ($(this).val() == '') {
		  $(this).val(dictionary.Arig);
	  }
  });
  
  // szures tortenik a bicikli brandekben
  $('#bicycle_brand').change(function() {
	var brandSlug = $("option:selected", $(this)).val();
	brandSlug = brandSlug == 0 ? '' : brandSlug;
	getType('bicycle', '', brandSlug);
	getYear('bicycle', '', brandSlug, '');
  });
  
  //szures tortenik a bicikli tipusokban
  $('#bicycle_type').change(function() {
	var typeSlug = $("option:selected", $(this)).val();
	typeSlug = typeSlug == 0 ? '' : typeSlug;
	var brandSlug  = $("option:selected", $('#bicycle_brand')).val();
	brandSlug = brandSlug == 0 ? '' : brandSlug;
	getYear('bicycle', '', brandSlug, typeSlug);
  });
  
  //szures tortenik a product brandekben
  $('#product_brand').change(function() {
	var brandSlug = $("option:selected", $(this)).val();
	brandSlug = brandSlug == 0 ? '' : brandSlug;
	getType('product', '', brandSlug);
	getYear('product', '', brandSlug, '');
  });
  
  //szures tortenik a product tipusokban
  $('#product_type').change(function() {
	var typeSlug = $("option:selected", $(this)).val();
	typeSlug = typeSlug == 0 ? '' : typeSlug;
	var brandSlug  = $("option:selected", $('#bicycle_brand')).val();
	brandSlug = brandSlug == 0 ? '' : brandSlug;
	getYear('product', '', brandSlug, typeSlug);
  });
  
  $('#termek_kereso_panel_link,#akciok_panel_link').click(function(){frontPanelToggle(); return false});
  
  // Kereskedoink oldalon egymast kizaro keresesi lehetosegek biztositasa
  $('#dealers_country').focus(function() {
    $('#dealers_name').val('');
  });
  $('#dealers_state').focus(function() {
    $('#dealers_name').val('');
  });
  $('#dealers_state').change(function() {
    getCitiesToState($(this).val());
  });
  $('#dealers_country').change(function() {
    getStatesToCountry($(this).val());
  });
  $('#dealers_city').focus(function() {
    $('#dealers_name').val('');
  });
  $('#dealers_name').focus(function() {
    $('#dealers_country').val('');
    $('#dealers_state').val('');
    $('#dealers_city').val('');
  });
});

function getCitiesToState(stateSlug) {
  $.getJSON(
    '/getcities_to_state/'+stateSlug,
    function (data) {
      var options = '<option value="">'+dictionary.Varos+'</option>';
      for (var key in data) {
        options += '<option value="'+key+'">'+data[key]+'</option>';
      }
      $('#dealers_city').html(options);
    }
  );
}

function getStatesToCountry(countrySlug) {
  $.getJSON(
    '/getstates_to_country/'+countrySlug,
    function (data) {
      var options = '<option value="">'+dictionary.State+'</option>';
      for (var key in data) {
        options += '<option value="'+key+'">'+data[key]+'</option>';
      }
      $('#dealers_state')
        .html(options)
        .change();
    }
  );
}

//fooldali kerekparkereso - brand lekero
function getBrand(form, selectedKey) {
  var $key = form == 'product' ? 'TermekGyartoja' : 'KerekparGyartoja'; 
  $.get(
	'/getbrand_ajax/'+form,
	{},
	function(data) {
		var options = '';
		for (var key in data) {
			var selectText = selectedKey == key ? ' selected="selected"' : '';
			options += '<option value="'+key+'"'+selectText+'>'+data[key]+'</option>';
		}
		var baseOption = '<option value="0">'+dictionary[$key]+'</option>';
		$('#'+form+'_brand').html(baseOption+options);
	},
	'json'
  );
}

//fooldali kerekparkereso - tipus lekero
function getType(form, selectedKey, brand) {
  var $key = form == 'product' ? 'TermekTipusa' : 'KerekparTipusa'; 
  $.get(
	'/gettype_ajax/'+form+(brand != '' ? '/brand/'+brand : ''),
	{},
	function(data) {
		var options = '';
		for (var key in data) {
			var selectText = selectedKey == key ? ' selected="selected"' : '';
			options += '<option value="'+key+'"'+selectText+'>'+data[key]+'</option>';
		}
		var baseOption = '<option value="0">'+dictionary[$key]+'</option>';
		$('#'+form+'_type').html(baseOption+options);
	},
	'json'
  );
}

//fooldali kerekparkereso - ev lekero
function getYear(form, selectedKey, brand, type) {
  var $key = form == 'product' ? 'TermekKiadasEve' : 'KiadasEve'; 
  $.get(
	'/getyear_ajax/'+form+(brand != '' ? '/brand/'+brand : '')+(type != '' ? '/type/'+type : ''),
	{},
	function(data) {
		var options = '';
		for (var key in data) {
			var selectText = selectedKey == key ? ' selected="selected"' : '';
			options += '<option value="'+key+'"'+selectText+'>'+data[key]+'</option>';
		}
		var baseOption = '<option value="0">'+dictionary[$key]+'</option>';
		$('#'+form+'_year').html(baseOption+options);
	},
	'json'
  );
}

var in_slide_action = false;
function frontPanelToggle(){
  if(in_slide_action) return false;
  in_slide_action = true;
  if($('#termek_kereso_panel').css('display') != 'none'){
    $('#termek_kereso_panel').slideUp('fast', function(){
    	$('#akciok_panel').show();
    	$('#akciok_panel_link').attr('class','tab_akciok');
      $('div.akciok').css('height',133);
      in_slide_action = false;
    });
  }
  else {
    $('div.akciok').css('height',34);
    $('#akciok_panel_link').attr('class','tab_akciok_open');
    $('#akciok_panel').hide();
    $('#termek_kereso_panel').slideDown('fast',function(){
      in_slide_action = false;
    });
  }
}

function setContainerPosition() {
  w = $(window).width()/2-$("#container").width()/2;
  $("#container").css("left",w);
  $("#footer").css('top',$('#container').offset().top+$('#container').height());
  $('#footer').css('width',$(window).width());  
  $('.footer_content').css('left',w+1);   
}

function innerLabelInputIsEmpty($name, $form){
  var inp = $('input[name="'+$name+'"]', $form);
  return inp.val() == inp.attr('default') || inp.val().length == 0;
}

function loadCrawler(){
  var links = $("div.crawler a");
  $('div.subcrawlers div').eq(0).show();
  
  var $cnt = 0;
  links.each(function(){
    var $c = $cnt;
    $(this).hover(function(){
      $('div.subcrawlers div').hide();
      $('div.subcrawlers div').eq($c).show();
      //setContainerPosition()
    });
    $cnt++;
  });
  
  var selected_link = null;
  $('div.subcrawlers div').hover(function(){
	selected_link = $('a.selected',$(this));
	selected_link.removeClass('selected');
  },function(){
	selected_link.addClass('selected');
  })
}

function emailIsValid(email) {
  return /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(email);
}

function fieldValue(name,form){
  var isinput = $('input[name="'+name+'"]',form).length;
  if(isinput)
	return $('input[name="'+name+'"]',form).val();
  else
    return $('select[name="'+name+'"] option:selected',form).val();
}