
/**
 * Run slideshows in sequence.
 * @author Alek - Zone1Media
 */
(function( $ ){

  var defaults = {
    timeout:    1000
  };
  var settings = {};
  var elements;
  var cycleInterval = null;
  var cycleTimeout = [];
  var qty = 0; // nr of elements

  function cycleInit() {
	  elements.cycle({ 
		  cleartype :  1,
		  speed     : 1000,
		  timeout   : 0 // do not start
	  });
  }

  function cycleNext(i) {
	  elements.eq(i).cycle('next');
  }

  function cycleNextTimeout(i) {
	  var j = i;
	  cycleTimeout[j] = setTimeout(function(){cycleNext(j);}, settings.timeout * ((j + 1) / qty));
  }

  function multiCycleNext() {
	  for (var i = 0; i < qty; i++) {
		cycleNextTimeout(i);
	  }
  }

  function multiCycle() {
	  if ( ! cycleInterval) {
		  cycleInterval = setInterval(function(){multiCycleNext();},settings.timeout);
	  }
  }

  var methods = {
    init : function( options ) { 
      settings = $.extend(defaults, options);
      //window.console.log(settings.timeout);
 
      elements = $(this).find('ul');
      if ( ! elements.length) {
        return this;
      }
	  qty = elements.length;

      cycleInit();
      multiCycleNext();
      multiCycle();
    },
    stop : function( ) {
      if (cycleInterval) {
        clearInterval(cycleInterval);
        cycleInterval = null;
		for (var i = 0; i < cycleTimeout.length; i++) {
			clearTimeout(cycleTimeout[i]);
			cycleTimeout[i] = null;
		}
      }
    },
    restart : function( ) {
      if ( ! cycleInterval) {
		  multiCycleNext();
		  multiCycle();
      }
    }
  };

  $.fn.homeCycle = function( method ) {
    
    if ( methods[method] ) {
      return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
    } else if ( typeof method === 'object' || ! method ) {
      return methods.init.apply( this, arguments );
    } else {
      $.error( 'Method ' +  method + ' does not exist on jQuery.tooltip' );
    }    
  
  };

})( jQuery );

/**
 * @author adam
 */
Cufon.replace('.font', { fontFamily: 'helvetica-neue' });

var pagePos = new Array("home", "andreetienne", "products", "yourchoice", "fittingservice", "contact", "message");
var colors = new Array("AF9C70", "B4A76E", "C7BD9E", "BFC1C7", "B2A9A7", "9EA6D1", "9EA6D1");

$(document).ready(function() {

	
	$('.blank').blankTarget();
	
	$('#testimonial').cycle({ 
	    cleartype :  1, // enable cleartype corrections 
	    speed     : 1000,
		timeout   : 6000
	});

  $('#page_home').homeCycle({
    timeout:  8800
  });
	
	$('#dialog').dialog({
		width : 500,
		modal : true,
		buttons: {
			"Close": function(){
				$(this).dialog("close");
			}
		},
		close : function() {
			window.location = REL_ROOT;
		}
	});
	
	$('body.all_in_one #menu li a, body.all_in_one #message, body.all_in_one #contact_back, body.all_in_one #products').click(function() {
		
		var id = ($(this).attr('id') == "contact_back") ? "contact" : $(this).attr('id');
		var position = 0;
		var color = "";
		var i = 0;
		
		for(i = 0; i < pagePos.length; i++) {
			if(pagePos[i] == id) {
				position = i * 1000;
				color = "#"+colors[i];
			}	
		}
		$('#middle').animate({
			scrollLeft : position+"px"
		}, {
			duration: 1000,
			easing: "easeOutCirc"
		});
		
		if ($(this).attr('id') !== "message" && $(this).attr('id') !== "contact_back") {
			$('#menu li a').css('color', '#fff');
			$(this).css('color', color);
		}
		// do not animate when homepage is hidden
		if(pagePos[0] == id) {
		  $('#page_home').homeCycle('restart');
		} else {
		  $('#page_home').homeCycle('stop');
		}
		return false;
	});
	
	$('.page, .first_page').not('#page_message, #page_contact, #page_manufacturers').click(function() {
		
		var id = $(this).attr('id');
		var position = 0;
		var i = 0;
		for(i = 0; i < 5; i++) {
			if("page_"+pagePos[i] == id) {
				position = i * 1000;
			}	
		}
		$('#middle').animate({
			scrollLeft : position+"px"
		}, {
			duration: 1000,
			easing: "easeOutCirc"
		});
		return false;
	});
	
	$('.fancy').fancybox();
	$('.winter_sale').fancybox({
		'frameHeight' : 430,
		'frameWidth'  : 500
	});
	
});

function startPage(page) {
	if(page !== "") {
		var position = 0;
		var color = "";
		var i = 0;
		
		for(i = 0; i < pagePos.length; i++) {
			if(pagePos[i] == page) {
				position = i * 1000;
				color = "#"+colors[i];
			}	
		}
		$('#middle').animate({
			scrollLeft : position+"px"
		}, {
			duration: 1000,
			easing: "easeOutCirc"
		});
	}
}

