//define container heights
  function heights (element) 
  {	   
    var hlist = new Array();
    hlist['contact'] = 675;
    hlist['cariera'] = 930;
    hlist['produse-financiare'] = 975;
    hlist['despre-noi'] = 943;
    hlist['home'] = 899;
    hlist['showroom'] = 797;
      
    return hlist[element];       
  }
    
  function pos (element) 
  {
    var plist = new Array();
    plist['contact'] = 0;
    plist['cariera'] = 787;
    plist['produse-financiare'] = 1717;
    plist['despre-noi'] = 2692;
    plist['home'] = 3635;
    plist['showroom'] = 4534;
      
    return plist[element];
  }
//ajax for showroom section
function showroom_ajax () 
{
  $('#showroom .ajax').click(function() 
  {      
    ajax_url = base_url + 'ajax/' + $(this).attr('title');
    $.ajax(
    {
      url: ajax_url,	
      type: "GET",
	  success: function (html) 
      {	
	    $('#showroom').html(html); 
        showroom_ajax (); 
        $("a.fancy").fancybox(
        {
			'titleShow'      : true,
		    'titlePosition'  : 'inside',
			'titleFormat'	 : function(){ 
					var link = $('#fancybox-img').attr('src');
					var full_text = '<a href="' + link + '" >Click aici pentru download</a>';
					return full_text;			
				},
          'overlayOpacity' : 0.6,
          'overlayColor'   : '#fff'
        });         			
	  }
    });	
    return false;	
  });
}
//ajax for contact section
function contact_ajax() 
{
  $('#contactSubmit').click(function() 
  {
      ajax_url = base_url + 'ajax/contact';      
      $.ajax(
      {
        url: ajax_url,	
        type: "POST",
        data: data,
	    success: function (html) 
        {	
	      $('.notice').html(html);            			
	    }
      });
      
      return false;	
    });          
}
//ajax for offer request form
function offer_ajax() 
{  
  $(".offer_req_submit").click(function() 
  {
      alert('click');
      
      return false;      
    }); 
   $("#offer_req_form").submit(function() 
   {
        alert('submit');
        return false;
   });          
}
//ajax for feedback form
function feedback_ajax() 
{
  $('#feedbackSubmit').click(function() 
  {
    ajax_url = base_url + 'ajax/contact/feedback';    
    $.ajax(
    {
      url: ajax_url,	
      type: "POST",
      data: data,
	  success: function (html) 
      {	
	    $('.notice').html(html);            			
	  }
    });
    return false;	
  });
}
//initial scrolling on page load
function init_scroll () 
{
  var url = window.location;
  var surl = url.toString();
  var parts= surl.split("/");
  var segment = parts[parts.length - 1];  
  
  var winH = 0, elemH = 0, elem = '', position =0;
  
  switch (segment) 
  {
    case 'showroom':
      $.scrollTo( 4534, 800 );
    break;
    
    case 'contact':
      $.scrollTo( 0, 500 );
    break;
    
    case 'feedback':
      $.scrollTo( 0, 500 );
    break;
    
    default:
      //hash doesn't exist, go to home
      if (!location.hash) 
      {
        //$.scrollTo( 3654, 500 );        
        winH = $(window).height();
        elemH = $('#home').height();   
        position = pos('home') + Math.round((heights('home') - winH) / 2); 
        position_string = position + 'px';        
        $.scrollTo( position_string, 700 );
      } 
    break;           
  }
}
//open map in fancybox
function fancy_map() 
{    
  $("a.openmap").fancybox(
  {
    'titleShow'      : false,
    'overlayOpacity' : 0.6,
    'overlayColor'   : '#fff'
  });         
}
//do section scrolling
function main_scrolling() 
{
  var winH = 0, elemH = 0, elem = '', position =0;    
  //vertical scrolling on click        
  $('.sidemenu > ul > li > a').click(function() 
  {
    elem = $(this).attr('href');
    element = elem.split('#')
    winH = $(window).height();
    elemH = $(elem).height();   
    position = pos(element[1]) + Math.round((heights(element[1]) - winH) / 2); 
    if (position < 0)
      position = 0;
    position_string = position + 'px';
    $.scrollTo( position_string, 700 );
        
    return false;
  });        
}
//change appearence on active inputs 
function active_inputs() 
{
  $('input.text , textarea , select').focus(function() 
  {
    $(this).addClass('focus');
  });
  $('input.text , textarea , select').blur(function() 
  {
    $(this).removeClass('focus');
  });    
}
//slide to on produse financiare section
function inner_slideto() 
{  
  $('#produse-financiare a.more, #produse-financiare p.more a').click(function() 
  {
    $('#produse-financiare .scroll').scrollTo($(this).attr('href'), 200);
    return false;
  });    
}
//validate forms
function validate_forms() 
{
  $("#contactForm").validate(
  {
    rules : 
    {
      contact_name: "required",
      contact_email: 
      {
        required: function(element) 
        {
          return ($("#contact_phone").val() == '');
        }
      },
      contact_message: "required"           
    },
    messages: 
    { 
      contact_name: 
      { 
        required: "Te rugam introdu numele !"
      }, 
      contact_email: 
      { 
        required: "Este necesar un numar de telefon sau un email !"                
      },
      contact_message: 
      { 
        required: "Te rugam introdu un mesaj !"                
      }            
    }
  });
    
  $("#feedbackForm").validate(
  {
    rules : 
    {
      feedback_name: "required",
      feedback_email: 
      {
        required: function(element) 
        {
          return ($("#feedback_phone").val() == '');
        }
      },
      feedback_message: "required"           
    },
    messages: 
    { 
      feedback_name: 
      { 
        required: "Te rugam introdu numele !"
      }, 
      feedback_email: 
      { 
        required: "Este necesar un numar de telefon sau un email !"                
      },
      feedback_message: 
      { 
        required: "Te rugam introdu un mesaj !"                
      }            
    }
  });
}
//open limo form
function limo_form () 
{
  $("#limoform").bind("submit", function() 
  {
    if ($("#limo_name").val().length < 1 || $("#limo_phone").val().length < 1 || $("#limo_time").val().length < 1 || $("#limo_hours").val().length < 1 || $("#limo_advance").val().length < 1 ) 
    {
	  $("#limo_error").show();
	  $.fancybox.resize();
	  return false;
	}

	$.fancybox.showActivity();
    
	limo_url = base_url + 'contact/limo/false/true';
    $.ajax(
    {
	  type	: "POST",
	  cache	: false,
	  url	: limo_url,
	  data	: $(this).serializeArray(),
	  success: function(data) 
      {
	    $.fancybox(data);
	  }
	});

	return false;
  });
}
//accordion behaviour on career
function accordion ()
{
  $("#accordion div").hide();
  
  $("#accordion h4 a").click(function()
  {        
    var div = $(this).parent().next("div"); 
	if ($(div).hasClass('opened')) 
    {
      $(div).slideToggle(400).removeClass("opened");
    }
    else
    {
      $(".opened").slideToggle(400).removeClass("opened");
      $(this).parent().next("div").slideToggle(400).addClass('opened');    
    }
    
    return false;    
  });
    
}

function download_fancy_image()
{

}

base_url = $('meta[name=base]').attr('content');
//main function
$(document).ready(function () 
{
  //on load functions
  init_scroll ();                      //initial scroll on load, based on url
  
  accordion();                         //custom made accordion for career  
  $('a.pdf').attr('target','_blank');  // pdf's open in a new tab
  //on click functions
  main_scrolling();                    //scroll to sections 
  showroom_ajax();                     //run ajax on showroom
  contact_ajax();                      //run ajax on contact
  feedback_ajax();                     //run ajax on feedback
  //open product images in fancybox
  $("a.fancy").fancybox(
  {
    'titleShow'      : true,
    'titlePosition'  : 'inside',
	'titleFormat'	 : function(){ 
			var link = $('#fancybox-img').attr('src');
			var full_text = '<a href="' + link + '" >Click aici pentru download</a>';
			return full_text;			
		},
    'overlayOpacity' : 0.6,
    'overlayColor'   : '#fff'
  });             
  //open limo lease form  
  $("a#limolink").fancybox(
  { 
    'onStart'        : function() {
	    $("#limo_error").hide();
	},
    'scrolling'      : 'no',
    'overlayOpacity' : 0.6,
    'overlayColor'   : '#fff',    
    'onComplete'     : function() 
    {
      limo_form();      
    }
  });
  
  fancy_map();                         //open map using fancybox
  active_inputs();                     //customize active inputs
  inner_slideto();                     //slide to paragraphs
  validate_forms();                    //validate forms  
})
