var base_url;
var offset;
var prev_offset;
var load_more;

//make tooltip for main menu links
function tooltip (selector, message)
{  
  var changeTooltipPosition = function(event) 
  {
    var tooltipX = event.pageX - 8;
	var tooltipY = event.pageY + 8;
	$('div.tooltip').css({top: tooltipY, left: tooltipX});
  };
 
  var showTooltip = function(event) 
  {
    $('div.tooltip').remove();
	$('<div class="tooltip">' + message + '</div>').appendTo('body');
	changeTooltipPosition(event);
  };
 
  var hideTooltip = function() 
  {
    $('div.tooltip').remove();
  };
 
  $(selector).bind(
  {
    mousemove : changeTooltipPosition,
	mouseenter : showTooltip,
	mouseleave: hideTooltip
  });
  return;    
}
//make external links
function external_links()
{
  $('a.external').attr('target','_blank');
  return;
}
//load contact form inside fancybox
function contact_form ()
{
  $("#contact_form").bind("submit", function() 
  {    
    if ($("#contact_name").val().length < 1 || $("#contact_email").val().length < 1 || $("#contact_message").val().length < 1 ) 
    {	  
      $("#contact_error").show();
      $("#contact_form input, #contact_form textarea").labelify({text: "label"});
	  $.fancybox.resize();
	  return false;
	}

	$.fancybox.showActivity();
    
	contact_url = base_url + 'contact/true';
    $.ajax(
    {  
	  type	: "POST",
	  cache	: false,
	  url	    : contact_url,
	  data	: $(this).serializeArray(),
	  success: function(data) 
      {
	    $.fancybox(data);
	  }
	});
    return false;
  });    
}

function init_contact_form ()
{    
  //check if contact is made through javascript
  if($('#contact_holder').length != 0)
  {
    $('#contact_holder').load(base_url + 'contact_form');   
    
    $("a#contact_link").fancybox(
    { 
      'onStart'        : function() 
      {
	    $("#contact_error").hide();
        $("#contact_form input, #contact_form textarea").labelify({text: "label"});
        $("#contact_form label").hide();
	  },
      'scrolling'      : 'no',
      'overlayOpacity' : 0.6,
      'overlayColor'   : '#fff',    
      'onComplete'     : function() 
      {
        contact_form();      
      }
    });
  } 
  return;
}

function init_company_info ()
{    
  //check if contact is made through javascript
  if($('#info_holder').length != 0)
  {
    $('#info_holder').load(base_url + 'company');   
    
    $("a#company_info").fancybox(
    { 
      'scrolling'      : 'no',
      'overlayOpacity' : 0.6,
      'overlayColor'   : '#fff'
    });
  } 
  return;
}

//copy to and paste from clipboard for main menu e-mail address
function zero_clipboard ()
{
  ZeroClipboard.setMoviePath( base_url + 'jsicss/ZeroClipboard.swf' );
  var clip = new ZeroClipboard.Client();
  var text = $('#to_clipboard').attr('href');
  text = text.split(':');
  clip.setText( text[1] );
  clip.glue( 'to_clipboard','email' ); 
  clip.addEventListener( 'onMouseOver', clip_mouse_over_handler );
  clip.addEventListener( 'onMouseOut', clip_mouse_out_handler );
  clip.addEventListener( 'onComplete', my_complete );
}
//message after copied to clipboard
function my_complete( client, text ) 
{
    //alert("Copied text to clipboard: " + text );
    $('.tooltip').text('now paste it in your e-mail client');
}
//for zero_clipboard()
function clip_mouse_over_handler ()
{
  $('#to_clipboard').trigger('mouseover'); 
}
//for zero_clipboard()
function clip_mouse_out_handler ()
{
  $('#to_clipboard').trigger('mouseout');  
}

//scroll autoload on work page
function init_scroll_load()
{
  load_more = true;  
  var current_location = location.href.split( '/' );
  offset = parseInt(current_location[current_location.length-1]);
  if (isNaN(offset))
  {
    offset = 5;
  }  
  else
  {
    offset = offset + 5;
  }  
  return;  
}
//actual ajax call on scroll down
function scroll_load(req_type, tag_name) 
{   
  if (tag_name)
  {
    ajax_url = base_url + req_type + '/' + tag_name + '/' + offset + '/true';  
  } 
  else
  {
    ajax_url = base_url + req_type + '/' + offset + '/true'; 
  } 
  //alert(ajax_url);
  
  $.ajax(
  {
    url:      ajax_url,
    success:  function(data, XHR) 
    {
      $(".cols_w:last").after(data);
      offset = parseInt(offset + 5);                  
    },    
    complete: function (XMLHttpRequest, textStatus)
    {
      if(textStatus == 'error')
      {
        load_more = false;
      }
      //scroll_top();
    },    
    type: "POST"
  });  
  
  return;  
}
//checks if the current call has been made before
function check_scroll (req_type, tag_name)
{   
  $(window).scroll(function()
  {
    if( (($(window).scrollTop() + 100) >= $(document).height() - $(window).height()) && load_more == true)
    {	       
      //alert('prev offset: ' + prev_offset  + ' offset: '+ offset + ' load_more: ' + load_more); 
      if (prev_offset != offset)
      {
        prev_offset = offset;
        scroll_load(req_type, tag_name);
        return;
      }
      else
        return;
	}
  });
}
//scrolls page to top
function scroll_top ()
{
  var done_scrolling = 1;
  
  $('.wtop, .top').live('click', function() 
    {
      if (done_scrolling == 1)
      {
        done_scrolling = 0;
        $.scrollTo( '5px', 500, {axis:'y', onAfter:function(){ /*alert('done');*/ done_scrolling = 1; } });
      }
      return false;    
    });    
}

(function ($) {

  // Log all jQuery AJAX requests to Google Analytics
  $(document).ajaxSend(function(event, xhr, settings){
    if (typeof _gaq !== "undefined" && _gaq !== null) {
      _gaq.push(['_trackPageview', settings.url]);
    }
  });

})(jQuery);

//main
$(document).ready(function() 
{  
  base_url = $('meta[name=base]').attr('content');    
  external_links();
  //top menu behaviour 
  if (!jQuery.browser.msie && parseInt(jQuery.browser.version) != 6) 
  {
    $('a#contact_link').attr('href','#contact_form').click(function() {return false;});     //block redirect on click 
    $('a#company_info').attr('href','#company_info_holder').click(function() {return false;});     //block redirect on click 
  
    init_company_info(); 
    init_contact_form();
  }
                                                                      
  zero_clipboard();
  tooltip("#gmap", $('#gmap').attr('rel'));  
  tooltip("#email div", $('#to_clipboard').attr('rel'));
  
  if ($('#content').hasClass('scroll_load_tag'))
  {
    init_scroll_load();
    tag_name = $('#tag_name').text();
    check_scroll('tag', tag_name);  
    $('.pagination').remove();
  } 
  
  if ($('#content').hasClass('scroll_load_work'))
  {
    init_scroll_load();
    check_scroll('work');  
    $('.pagination').remove();
  } 
  
  if ($('#content').hasClass('scroll_load_home'))
  {
    init_scroll_load();
    check_scroll('index');  
    $('.pagination').remove();
  } 
  
  if ($('#content').hasClass('labelify'))
  {
    $("#contact_form input, #contact_form textarea").labelify({text: "label"}); 
    $("#contact_form label").hide(); 
    $("#contact_error").hide();
  } 
  
  if($('#content').hasClass('swfobject'))
  {
	if ($(".swf_object").length > 0){
		var swflink = $(".swf_object").html();
		var swfArray = swflink.split('|');		
		$('.swf_object').flash({swf:swfArray[0],height:swfArray[1],width:swfArray[2]});
    }
	
  }
  
  if ($('.wtop').length > 0)
  {
    scroll_top();  
  }
   
});
Cufon.replace('dt, h1#title');
