var IE6 = (navigator.userAgent.indexOf('MSIE 6.0') > 0);
var oldIE = (IE6 || (navigator.userAgent.indexOf('MSIE 5.5') > 0));

$(window).load(function(){
  // style forms

  $('.product-search select').selectBox({css:'select-box'});
  $('.panel select').selectBox({css:'select-box'});
  $('.applyform select').selectBox({css:'select-box'});
  $('.product-search input').each( function()
  {
      $this = $(this);
      $this.before('<img src="/img/site/input_left.gif"/>');
      $this.after('<img src="/img/site/input_right.gif"/>');
  });

  // IE6 fake frames :O

  if (IE6)
  {
      $('html, body').css({'overflow':'hidden'});
      $('#wrap').css({'width':'100%'});
      $('#content-wrap').wrap('<div style="position: absolute; width: 100%; height: expression(document.body.offsetHeight); overflow: auto; z-index: 1;"></div>')
      $('#content-wrap').wrap('<div style="width: 900px; position: absolute; margin-left: expression(document.body.offsetWidth / 2 - 450); z-index:2"></div>');
      $('#header').wrap('<div style="width: 900px; position: absolute; margin-left: expression(document.body.offsetWidth / 2 - 450); z-index:100"></div>');
      $('#footer').wrap('<div style="width: 900px; position: absolute; margin-left: expression(document.body.offsetWidth / 2 - 450); z-index:50; bottom: 0px; "></div>');
      $('#content').css({'padding':'160px 0 110px 0', 'width':'900px'});

      // fix select boxes
      $('.select-box ul').css('position', 'static');
      $('.select-box ul').wrap('<span style="position:absolute"></span>');
      //alert('Finished loading');
  }

  $('#category').change(function() {
      $.get(
          "/pages/ajax_product_sub_categories/" + $(this).val(),
          {},
          function(data){
              //alert(data);
              $('#subcategory_container').html(data);
              //alert("category: " + data);
          }
      );
  });

  $('#area').change(function() {
      $.get(
          "/pages/ajax_product_stores/"+$(this).val(),
          {},
          function(data){
              $('#store_container').html(data);
          }
      );
  });

  $('#loan_area').change(function() {
      $.get(
          "/pages/ajax_product_stores/"+$(this).val()+"?buy_back=1",
          {},
          function(data){
              $('#store_container').html(data);
          }
      );
  });

  $('.store-locator #area').change(function() {
      $.get(
          "/pages/ajax_store_locator_stores/"+$(this).val(),
          {},
          function(data){
              $('.store-locator #store_container').html(data);
              //alert("stores: " + data);
          }
      );
  });

	$('.product-search #category').change(function() {
		$.get(
			"/pages/ajax_product_sub_categories/"+$(this).val(),
			{},
			function(data){
				$('.product-search #subcategory_container').html(data);
			}
		);
	});

	$('.product-search #area').change(function() {
		$.get(
			"/pages/ajax_product_stores/"+$(this).val(),
			{},
			function(data){
				$('.product-search #store_container').html(data);
			}
		);
	});

	$('#search_form #category').change(function() {
		$.get(
			"/pages/ajax_product_sub_categories/"+$(this).val(),
			{},
			function(data){
				$('#search_form #subcategory_container').html(data);
			}
		);
	});

	$('#search_form #area').change(function() {
		$.get(
			"/pages/ajax_product_stores/"+$(this).val(),
			{},
			function(data){
				$('#search_form #store_container').html(data);
			}
		);
	});

	$('#buy_form #category').change(function() {
		$.get(
			"/pages/ajax_product_sub_categories/"+$(this).val(),
			{},
			function(data){
				$('#buy_form #subcategory_container').html(data);
			}
		);
	});

	$('#buy_form #area').change(function() {
		$.get(
			"/pages/ajax_product_stores/"+$(this).val(),
			{},
			function(data){
				$('#buy_form #store_container').html(data);
			}
		);
	});

	$('.store-locator #area').change(function() {
		$.get(
			"/pages/ajax_store_locator_stores/"+$(this).val(),
			{},
			function(data){
				$('.store-locator #store_container').html(data);
			}
		);
	});

	$('.applyform #job_category').change(function() {
		$.get(
			"/pages/ajax_category_jobs/"+$(this).val(),
			{},
			function(data){
				$('.applyform #job_container').html(data);
			}
		);
	});

	$('#sell_form #area').change(function() {
		$.get(
			"/pages/ajax_store_locator_stores/"+$(this).val(),
			{},
			function(data){
				$('#sell_form #store_container').html(data);
			}
		);
	});

	$('.vacancy_container').each(function() {
		var container = this;

		$(container).find('.vacancy_category').each(function() {
			$(this).css({'cursor':'pointer'}
			).mouseover(function() {
				$(this).css({'text-decoration':'underline'});
			}).mouseout(function() {
				$(this).css({'text-decoration':'none'});
			}).click(function() {
				$(container).find('.vacancy').each(function() {
					$(this).toggle();
				});
			}).click();
		});
	});
});