


var Omni = function(){ }
Omni.sitemap = {
    cache:'',
    opened:false,
    animating:false,
    open:function(){
        if(Omni.sitemap.cache == ''){
            $.ajax({ type: "POST", url: "/sitemap/?ajax=true",
                success:function(response){
                    Omni.sitemap.cache = response;
                    $('#map .wrp').html(Omni.sitemap.cache);
                    Omni.sitemap.post_open()
                }
            })
        } 
        else Omni.sitemap.post_open();
    },
    post_open:function(){
        Omni.sitemap.animating = true;         
        $('#map').fadeIn('fast');
        var targetOffset = $('#footer').offset().top
        $('html,body').animate({scrollTop: targetOffset}, 1000, function(){
            Omni.sitemap.animating = false;         
        });
        Omni.sitemap.opened = true;
        $('#openmap').removeClass('mapdown')
        $('#openmap').addClass('mapup')
        
    },
    close:function() {
        if(Omni.sitemap.opened){
            var targetOffset = $('#chassis').offset().top
            $('html,body').animate({scrollTop: targetOffset}, 1000, function(){
                $('#map').fadeOut('fast');
            });
            Omni.sitemap.opened = false;
            $('#openmap').addClass('mapdown')
            $('#openmap').removeClass('mapup')
        }
    }
    
}











PdfForm = function(){ 
    this.init();
}

PdfForm.prototype = { 

  cache: new Array(),

  init: function() {
 
    var t = this
    var id
     
    if($('.formlink').length>0)
    {
      t.behaviour()
    } 

  },/* end innit */
  
  behaviour: function(aj_id) {
    
    var t = this

    if(aj_id) /* just the replaced div */
    {
      $('#pdf-form-for-'+aj_id+' .formlink').each(function() {
        $(this).click(function() {
          
          id = $(this).attr('id').split('_')[1]
          t.getForm(id)
          return false

        }); /* end onlclick */
      });
    }
    else  /* else everything */
    {
      $('.formlink').each(function() {
        $(this).click(function() {
          
          id = $(this).attr('id').split('_')[1]
          t.getForm(id)
          return false

        }); /* end onlclick */
      });
    }

  },

  getForm: function(id) {

    var t = this

          /* do request to get the form */
          $.ajax({
      			type: "GET",
      			url: '/productpdf/',
      			data: "pid="+id,
              
        			success: function(form){

                var myRegExp = /<form/;
                var match = form.search(myRegExp);

                if(match == -1) // cached user
                {
                  window.location = form
                } 
                else
                {
                  // remove any forms present
                  if(t.cache.length>0)
                  {
                    for(var i in t.cache) 
                    {
                      $('#pdf-form-for-'+i).html(t.cache[i])
                      t.behaviour(i)
                    }
                    t.cache = new Array()          
                  }
                  // cache static HTML 
                  t.cache[id] = $('#pdf-form-for-'+id).html()
                  // replace with form
                  if(form)
                  {
                    $('#pdf-form-for-'+id).fadeOut('fast', function() {
                      $('#pdf-form-for-'+id).html(form)
                      $('#pdf-form-for-'+id).fadeIn('fast')
                    });
                  }
                }
              }
          });
  }

}



$(document).ready(function() {
    
  $('#extracopy').each(function(){
    $(this).css('display', 'none')
  });

  /* if product page and links to PDF's exist */
  if($('.formlink').length>0)
    pdf = new PdfForm();
  

    $(function() {
    	$('.photo-gallery a').lightBox({
        fixedNavigation:true,
        txtImage: $(this).attr('title')
      });
    });

  $('.gallery').each(function(){
    $(this).css('display', 'none')
  });

  if($('.more').length>0)
  {

   //$('.more').fadeIn()
  }

  if($('.selected').length>0)
  {
    var g = $('.selected').next('li').attr('id')
    $('#'+g).slideDown('slow')
  }
     
  if($('label[for=id_details]').length>0)
  {
    $('label[for=id_details]').css('font-size','12px')
    $('label[for=id_subscribe]').css('font-size','12px')
  }

	$('.extracopy').click( function() {
    $('.extracopy').fadeOut('fast')
    $('#extracopy').slideDown(400, function(){
      $('.hidecopy').fadeIn('slow')
    });
    return false
  });

	$('.hidecopy').click( function() {
    $('.hidecopy').fadeOut('fast')
    $('#extracopy').slideUp(200, function(){
      $('.extracopy').fadeIn('fast')
    });
    return false
  });

	$('.more').click( function() {
    $('.more').fadeOut('fast')
    $('#container').slideDown(400, function(){
      $('.hide').fadeIn('slow')
    });
    return false
  });

	$('.hide').click( function() {
    $('.hide').fadeOut('fast')
    $('#container').slideUp(200, function(){
      $('.more').fadeIn('fast')
    });
    return false
  });


  if($('.special_feature').length>0)
  { 
    $('.special_feature').each(function() {
      $(this).click(function() {
        window.location=$(this).attr('rel')
      });
    }); 
  }
  
  
  /* APPLICATION PAGE */
  /* use ajax to get a job description */
  $('#id_jobposition').change(function(event){
    
    $('#job_summary').html('')
    jobpos_id = $(this).val().toString()
    
    if(jobpos_id!='')
    {
      $.ajax({
      	type: "POST",
        url: '/jobdescription/'+jobpos_id+'/',
      	    success: function(response){
            $('#job_summary').html(response)
	    $('#job_summary').fadeIn()
          }
      })
    }

  });
  
  



  /* site map */
	$('#openmap').click(function() {
        if(!Omni.sitemap.opened)
            Omni.sitemap.open();
        else
            Omni.sitemap.close();
        return false;
    });



});


