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

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

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

  // IE6 fake frames :O

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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