$(document).ready(function() {
						   
	/* Init Main Header Menu
	-------------------------------------------------------------------------------------------------------------------------- */
	
	ddsmoothmenu.init({
		mainmenuid: "navigation", //menu DIV id
		orientation: 'h', //Horizontal or vertical menu: Set to "h" or "v"
		classname: 'ddsmoothmenu', //class added to menu's outer DIV
		contentsource: "markup" //"markup" or ["container_id", "path_to_menu_file"]
	});
	
	/* Testimonials 
	-------------------------------------------------------------------------------------------------------------------------- */
	
	if( $('.testimonial_item').size() > 1 ) {
		
		testimonialsIndex = 0;
		totalTestimonials = $('.testimonial_item').size();
		
		 $('.testimonial_item').each(function(index) {
			$(this).hide();
		 });
		 
		 $(".testimonial_item:eq("+testimonialsIndex+")").show();
		 
		 t = setInterval("testimonials()", 10000);
		 
		 
	}
	
	/* Slider ( if div exist ) 
	-------------------------------------------------------------------------------------------------------------------------- */
	
	if ( $("#slider").length ) {
		
		
		$(".paging").show();
		$(".paging a:first").addClass("active");
		
		var imageWidth = $(".window").width();
		var imageSum = $(".image_reel img").size();
		var imageReelWidth = imageWidth * imageSum;
		
		$(".image_reel").css({'width' : imageReelWidth});
		
		
		$(".image_reel div").hover(function() {
			clearInterval(play); 
		}, function() {
			rotateSwitch(); 
		});	
		
		
		$(".paging a").click(function() {
			$active = $(this); 
			clearInterval(play);
			rotate(); 
			rotateSwitch(); 
			return false; 
		});
		
		rotate = function(){
			var triggerID = $active.attr("rel") - 1; 
			var image_reelPosition = triggerID * imageWidth; 
		
			$(".paging a").removeClass('active'); 
			$active.addClass('active'); 
		
			$(".image_reel").animate({
				left: -image_reelPosition
			}, 500 );
		
		}; 
		
		rotateSwitch = function(){
			play = setInterval(function(){ 
				$active = $('.paging a.active').next(); 
				if ( $active.length === 0) { 
					$active = $('.paging a:first');
				}
				rotate(); 
			}, 7000); 
		};
		
		rotateSwitch();
	}
	
	
	/* Resize heading Boxes
	-------------------------------------------------------------------------------------------------------------------------- */
	
	//alert( $('#leftBox').height() + "----" + $('#rightBox').height() );
	
	if( $('#leftBox').height() > $('#rightBox').height() ){
		$('#rightBox').height($('#leftBox').height());
	}else if(  $('#rightBox').height() > $('#leftBox').height() ) {
		$('#leftBox').height($('#rightBox').height());	
	}
	
	/* Page About 
	-------------------------------------------------------------------------------------------------------------------------- */
	
	if( $('.aboutRow .counters').size() > 0 ) {
		
		 $('.aboutRow .counters').each(function(index) {
			incCounter($(this).attr('id'), 0, $(this).html());
		 });
		 
	}
	
	
	
	/* Page News 
	-------------------------------------------------------------------------------------------------------------------------- */
	
	if( $('.newsRow').size() > 0 ) {
		
		 var maxNewsPerPage = 4;
		 var totalNews = $('.newsRow').size();
		 var currentPos = 0;
		 
		 var newsHeight = $('.newsRow:eq(1)');
		 newsHeight = newsHeight.position();
		 newsHeight = newsHeight.top;
		 
		 $('#newsContainer').height((newsHeight-10)*maxNewsPerPage);
		 
		 $('#newsNext').click(function(){
			loadNews('+');	
			return false;
		 });
		
			
		 $('#newsPrevious').click(function(){
			loadNews('-');	
			return false;
		 });
		 
		 /* Pagination
		 ------------------------------------------- */
		 var totalPages = Math.ceil(totalNews/maxNewsPerPage);
		 
		 for(var i = 1; i <= totalPages; i++){
			 $('.newsNav .middle').append('<a href="#">'+i+'</a>');
			 
			 if(i < totalPages) {
			 	$('.newsNav .middle').append(' | ');
			 }
		 }
		 
		 $('.newsNav .middle a:first-child').addClass('selected');
		 
		 $('.newsNav .middle a').each(function(index) {
			$(this).click(function(){
				loadNews($(this).html());	
				return false;
			});
		 });
		 
		 $('#newsPrevious').animate({opacity:0}, 0);
		 
		/* End Pagination 
		------------------------------------------- */	
		
		 function loadNews(e){
		
			if(e == '+'){
				currentPos= currentPos + maxNewsPerPage;
				if(currentPos >= totalNews)
					currentPos = 0;
			}else if(e == '-'){
				currentPos= currentPos - maxNewsPerPage;
				if(currentPos < 0)
					currentPos = totalNews - maxNewsPerPage;
			}else{
				currentPos= (e-1) * maxNewsPerPage;
			}
			
			$(".newsNav .middle a").each(function(index) {
				$(this).removeClass('selected');
			});
			
			$(".newsNav .middle a:eq("+currentPos/4+")").addClass('selected');
			if(currentPos/4 == 0) {
					 $('#newsPrevious').animate({opacity:0}, 0);
			}else{
					 $('#newsPrevious').animate({opacity:1}, 0);
			}
			
			var p = $(".newsRow:eq("+currentPos+")");
			var position = p.position();
			
			
			$('#newsScroll').animate({
			
			top: -position.top,
			
			}, 600, function() {
			// Animation complete.
			});
				
		}
		 
	}
	
	/* End
	-------------------------------------------------------------------------------------------------------------------------- */
	
	/* Page Press 
	-------------------------------------------------------------------------------------------------------------------------- */
	
	if( $('.pressRow').size() > 0 ) {
		
		 var maxpressPerPage = 3;
		 var totalpress = $('.pressRow').size();
		 var currentPos = 0;
		 
		 var pressHeight = $('.pressRow:eq(1)');
		 pressHeight = pressHeight.position();
		 pressHeight = pressHeight.top;
		 
		 $('#pressContainer').height((pressHeight-10)*maxpressPerPage);
		 
		 $('#pressNext').click(function(){
			loadpress('+');	
			return false;
		 });
		
			
		 $('#pressPrevious').click(function(){
			loadpress('-');	
			return false;
		 });
		 
		 /* Pagination
		 ------------------------------------------- */
		 var totalPages = Math.ceil(totalpress/maxpressPerPage);
		 
		 for(var i = 1; i <= totalPages; i++){
			 $('.pressNav .middle').append('<a href="#">'+i+'</a>');
			 
			 if(i < totalPages) {
			 	$('.pressNav .middle').append(' | ');
			 }
		 }
		 
		 $('.pressNav .middle a:first-child').addClass('selected');
		 
		 $('.pressNav .middle a').each(function(index) {
			$(this).click(function(){
				loadpress($(this).html());	
				return false;
			});
		 });
		 
		 $('#pressPrevious').animate({opacity:0}, 0);
		 
		/* End Pagination 
		------------------------------------------- */	
		
		 function loadpress(e){
		
			if(e == '+'){
				currentPos= currentPos + maxpressPerPage;
				if(currentPos >= totalpress)
					currentPos = 0;
			}else if(e == '-'){
				currentPos= currentPos - maxpressPerPage;
				if(currentPos < 0)
					currentPos = totalpress - maxpressPerPage;
			}else{
				currentPos= (e-1) * maxpressPerPage;
			}
			
			$(".pressNav .middle a").each(function(index) {
				$(this).removeClass('selected');
			});
			
			$(".pressNav .middle a:eq("+currentPos/3+")").addClass('selected');
			if(currentPos/3 == 0) {
					 $('#pressPrevious').animate({opacity:0}, 0);
			}else{
					 $('#pressPrevious').animate({opacity:1}, 0);
			}
			
			var p = $(".pressRow:eq("+currentPos+")");
			var position = p.position();
			
			
			$('#pressScroll').animate({
			
			top: -position.top,
			
			}, 600, function() {
			// Animation complete.
			});
				
		}
		 
	}
	
	/* End
	-------------------------------------------------------------------------------------------------------------------------- */	
	
	
	/* Testimonials  
	-------------------------------------------------------------------------------------------------------------------------- */
	
	if( $('.testimonialsRow').size() > 0 ) {
		
		 var maxtestimonialsPerPage = 4;
		 var totaltestimonials = $('.testimonialsRow').size();
		 var currentTPos = 0;
		 
		 var tHeight = $('.testimonialsRow:eq(1)');
		 tHeight = tHeight.position();
		 tHeight = tHeight.top;
		 
		 $('#testimonialsContainer').height((tHeight-10)*maxtestimonialsPerPage);
		 
		 $('#testimonialsNext').click(function(){
			loadTestimonials('+');	
			return false;
		 });
		
			
		 $('#testimonialsPrevious').click(function(){
			loadTestimonials('-');	
			return false;
		 });
		 
		 /* Pagination
		 ------------------------------------------- */
		 var totalTPages = totaltestimonials/maxtestimonialsPerPage;
		 
		 for(var i = 1; i <= totalTPages; i++){
			 $('.testimonialsNav .middle').append('<a href="#">'+i+'</a>');
			 
			 if(i < totalTPages) {
			 	$('.testimonialsNav .middle').append(' | ');
			 }
		 }
		 
		 $('.testimonialsNav .middle a:first-child').addClass('selected');
		 
		 $('.testimonialsNav .middle a').each(function(index) {
			$(this).click(function(){
				loadTestimonials($(this).html());	
				return false;
			});
		 });
		 
		/* End Pagination 
		------------------------------------------- */	
		
		 function loadTestimonials(e){
		
			if(e == '+'){
				currentTPos= currentTPos + maxtestimonialsPerPage;
				if(currentTPos >= totaltestimonials)
					currentTPos = 0;
			}else if(e == '-'){
				currentTPos= currentTPos - maxtestimonialsPerPage;
				if(currentTPos < 0)
					currentTPos = totalTestimonials - maxtestimonialsPerPage;
			}else{
				currentTPos= (e-1) * maxtestimonialsPerPage;
			}
			
			$(".testimonialsNav .middle a").each(function(index) {
				$(this).removeClass('selected');
			});
			
			$(".testimonialsNav .middle a:eq("+currentTPos/4+")").addClass('selected');
			
			var p = $(".testimonialsRow:eq("+currentTPos+")");
			var position = p.position();
			
			
			$('#testimonialsScroll').animate({
			
			top: -position.top,
			
			}, 600, function() {
			// Animation complete.
			});
				
		}
		 
	}
	
	/* End
	-------------------------------------------------------------------------------------------------------------------------- */	
});





/* Sub Functions
-------------------------------------------------------------------------------------------------------------------------- */

function incCounter(mcId, begin, limit) {
	
	if(limit < 500000) {
	begin = begin+1317;
	}else if(limit > 1000000){
	begin = begin+1002000;
	}else{
	begin = begin+59310;
	} 
    $("#"+mcId).html(FormatNumberBy3(begin));
    if (begin < limit) {
        setTimeout('incCounter("'+ mcId +'", '+ begin +', '+ limit +')', 50);
    }else{
		$("#"+mcId).html(FormatNumberBy3(limit));
	}
	
}

var testimonialsIndex;
var	totalTestimonials;

function testimonials(){
			 
	$(".testimonial_item:eq("+testimonialsIndex+")").fadeOut(300);
	testimonialsIndex++;
	if(testimonialsIndex >= totalTestimonials) {
		testimonialsIndex = 0;
	}
	$(".testimonial_item:eq("+testimonialsIndex+")").delay(400).fadeIn(300);
	
 } 
 
 
 function FormatNumberBy3(num, decpoint, sep) {
  // check for missing parameters and use defaults if so
  if (arguments.length == 2) {
    sep = ",";
  }
  if (arguments.length == 1) {
    sep = ",";
    decpoint = ".";
  }
  // need a string for operations
  num = num.toString();
  // separate the whole number and the fraction if possible
  a = num.split(decpoint);
  x = a[0]; // decimal
  y = a[1]; // fraction
  z = "";


  if (typeof(x) != "undefined") {
    // reverse the digits. regexp works from left to right.
    for (i=x.length-1;i>=0;i--)
      z += x.charAt(i);
    // add seperators. but undo the trailing one, if there
    z = z.replace(/(\d{3})/g, "$1" + sep);
    if (z.slice(-sep.length) == sep)
      z = z.slice(0, -sep.length);
    x = "";
    // reverse again to get back the number
    for (i=z.length-1;i>=0;i--)
      x += z.charAt(i);
    // add the fraction back in, if it was there
    if (typeof(y) != "undefined" && y.length > 0)
      x += decpoint + y;
  }
  return x;
}

