var currentPage = "home";
var scrollActive = true;
var current = 0;
var pages = new Array();
pages = ["home","portfolio","service","about","blog","contact"];
$(document).ready(function(){
   
	$("#sections").queryLoader2({
		onComplete: function () {},
        barColor: "#09C8FF",
        backgroundColor: "#000000",
        percentage: false,
        completeAnimation: "grow"
    });
	Modernizr&&(!Modernizr.canvas||!Modernizr.csstransitions||!Modernizr.opacity||!Modernizr.rgba)&&$(".browser-alt").removeClass("hidden");
	Modernizr&&(!Modernizr.canvas||!Modernizr.csstransitions||!Modernizr.opacity||!Modernizr.rgba)&&(ie=true);
	 
	 $("body").touchwipe({
		 wipeRight: function() {  
		 	gotoPrev();
		 
		 },
		 wipeLeft: function() { 
		 	gotoNext();
		 
		  },
		 wipeUp: function() {  location.hash =  getPrevPage(); },
		 wipeDown: function() { location.hash =  getNextPage(); },
		 min_move_x: 20,
		 min_move_y: 20,
		 preventDefaultEvents: true
	});
	
	if(swipetouch){
		$("#footer .info").css("background","transparent url('images/swipenavi.gif') no-repeat right");	
		$("#footer .info").html("Swipe Navigation aktiv");	
	}
	
	buildGallery();
	$('input:not(:submit,:checkbox)').bind('focus blur', change);
	$('textarea').bind('focus blur', change);
	$("#gesendet").fadeOut(10);
	
	
	$("#close-btn").bind({click: function() {
		if(ie)
		$("#service-mehr").animate({opacity:0,marginLeft:"1500px"},500);
		else
		$("#service-mehr").removeClass("fade-in2");
  	}});
	
	$("#mehr-btn").bind({click: function() {
		if(ie)
		$("#service-mehr").animate({opacity:1,marginLeft:"420px"},500);
		else
		$("#service-mehr").addClass("fade-in2");
  	}});
	
	/* attach a submit handler to the form */
  $("#contactform").submit(function(event) {

    /* stop form from submitting normally */
    event.preventDefault(); 
	
	if($("#fname").val() != "Name" &&  $("#fname").val().length>3){
		nameok = true; 
		$("#fname").removeClass("error");
	}else{
		nameok = false; 
		$("#fname").addClass("error");
	}
	if($("#fsubject").val() != "Betreff" &&  $("#fsubject").val().length>3){
		subok = true; 
		$("#fsubject").removeClass("error");
	}else{
		subok = false; 
		$("#fsubject").addClass("error");
	}
	if($("#femail").val() != "E-Mail" &&  $("#femail").val().length>3 && looksLikeMail($("#femail").val())){
		emailok = true; 
		$("#femail").removeClass("error");
	}else{
		emailok = false; 
		$("#femail").addClass("error");
		
	}
	if($("#fmessage").val() != "Ihre Nachricht" &&  $("#fmessage").val().length>3){
		mesok = true; 
		$("#fmessage").removeClass("error");
	}else{
		mesok = false; 
		$("#fmessage").addClass("error");
	}
    /* get some values from elements on the page: */
    var $form = $( this ),
        url = $form.attr( 'action' );

    /* Send the data using post and put the results in a div */
	if(nameok&&subok&&emailok&&mesok){
    	$.post( url, { fname: $("#fname").val() , fsubject: $("#fsubject").val(),femail: $("#femail").val(),fmessage: $("#fmessage").val()},
      	function( data ) {
			if(data.answer=="ok"){
          		 $("#gesendet").fadeIn(500);
				 $("#fname").val("Name");
				 $("#fsubject").val("Betreff");
				 $("#femail").val("E-Mail");
				 $("#fmessage").val("Ihre Nachricht");
			}
     	 }, "json");
	}
	});
	
	$('#header-logo').keyframeclip({
			imagepath: 'images/ani/',
			imagename: 'contrast____000',
			images:61,
			loop: false,
			touchcontrol:false,
			autoplay : true
		  });
	var keyframeclip1 = $('#header-logo').data('keyframeclip');
	keyframeclip1.init(); // prints "publicMethod() called!" to console
	$('#header').bind('mouseover',function(e){ 
		if(keyframeclip1.currentFrame() == 0)
		keyframeclip1.playtoAndStop(60,100,callBackF);
	});
	$('#header').bind('mouseout',function(e){ 
		if(keyframeclip1.currentFrame() == 60)
		keyframeclip1.gotoAndStop(0);
	});
	function callBackF(){
		
		keyframeclip1.gotoAndStop(0);
	}
	$("#sectionMenu a").each(function(index){
		$(this).bind({click: function() {
    		if(keyframeclip1.currentFrame() == 0)
			keyframeclip1.playtoAndStop(60,100,callBackF);
  		}});

	});
	
});

function looksLikeMail(str) {
    var lastAtPos = str.lastIndexOf('@');
    var lastDotPos = str.lastIndexOf('.');
    return (lastAtPos < lastDotPos && lastAtPos > 0 && str.indexOf('@@') == -1 && lastDotPos > 2 && (str.length - lastDotPos) > 2);
}


function change(e) {
	var y = $(this); var D = this.defaultValue; var V = $.trim(y.val()); var t = e.type;
	switch (t) {
	case 'focus': if(D==V&&D=='http://'){y.select();}else if(D==V){y.val('');}else{y.val(V); y.select();};y.removeClass("error"); break;
	case 'blur': if(V==''){y.val(D);}; break;
	}
}


jQuery(function($) {
	$('#sections')
        .bind('mousewheel', function(event, delta) {
            var dir = delta > 0 ? 'Up' : 'Down',
                vel = Math.abs(delta);
			if(scrollActive){
				if(dir=="Up"){
					location.hash =  getPrevPage();
				}else{
					location.hash =  getNextPage();
				}
				scrollActive = false;
			}
            return false;
        });
});


$(document).keydown(function (event) {

    // handle cursor keys
    if (event.keyCode == 40) {
      // go left
	  if(getNextPage() == "blog")
	  location.href =  "http://blog.contrast-concepts.net/";
	  else
      location.hash =  getNextPage();
    } else if (event.keyCode == 38) {
      // go right
	  if(getPrevPage() == "blog")
	  location.href =  "http://blog.contrast-concepts.net/";
	  else
      location.hash =  getPrevPage();
    }
	// handle cursor keys
    if (event.keyCode == 37) {
     	gotoPrev();
    } else if (event.keyCode == 39) {
		gotoNext();

    }

  });

function getNextPage(){
	for(i=0;i<pages.length;i++){
		if(currentPage == pages[i] && i<pages.length-1)
			return pages[i+1];
		else if(i == pages.length-1)
			return currentPage;
	}
}
function getPrevPage(){
	for(i=0;i<pages.length;i++){
		if(currentPage == pages[i] && i >0)
			return pages[i-1];
		else if(currentPage == pages[0])
			return currentPage;
	}
}

$(window).resize(function() { 
	$("#sections").scrollTo("#_"+currentPage,10);
	positionGallery();
});
$(window.onorientationchange = function() { 
	$("#sections").scrollTo("#_"+currentPage,10);
 	positionGallery()
});

function handleChange(evt){
	_gaq.push(['_trackPageview', "/"+evt.path]);
   if(evt.path.length>3){
   lastPage = currentPage;
   currentPage = evt.path;
   if(currentPage.substring(0,1) == "/")
   currentPage = currentPage.substring(1,currentPage.length);
	$("#sections").scrollTo("#_"+currentPage,600, function(){
		
		if(ie){
		if(lastPage == "service")
			$("#service-mehr").animate({opacity:0,marginLeft:"1500px"},0);
		$("#_"+lastPage+" article .hgroup span").animate({opacity:0,marginLeft:"100px"},0);
		$("#_"+lastPage+" article .hgroup div.home-text").animate({opacity:0,marginLeft:"100px"},0);
		$("#_"+lastPage+" article #figure").animate({opacity:0,marginLeft:"100px"},0);
		$("#_"+lastPage+" div .fade").animate({opacity:0,marginLeft:"100px"},0);
		$("#_"+currentPage+" article .hgroup span").animate({opacity:0.9,marginLeft:"0px"},500);
		$("#_"+currentPage+" article .hgroup div.home-text").animate({opacity:0.9,marginLeft:"0px"},500);
		$("#_"+currentPage+" article #figure").animate({opacity:0.9,marginLeft:"0px"},500);
		$("#_"+currentPage+" div .fade").animate({opacity:0.9,marginLeft:"0px"},500);
		}else{
		if(lastPage == "service")
			$("#service-mehr").removeClass("fade-in2");
		$("#_"+lastPage+" article .hgroup span").removeClass("fade-in");
		$("#_"+lastPage+" article .hgroup div.home-text").removeClass("fade-in");
		$("#_"+lastPage+" article #figure").removeClass("fade-in");
		$("#_"+lastPage+" div .fade").removeClass("fade-in");
		$("#_"+currentPage+" article .hgroup span").addClass("fade-in");
		$("#_"+currentPage+" article .hgroup div.home-text").addClass("fade-in");
		$("#_"+currentPage+" article #figure").addClass("fade-in");
		$("#_"+currentPage+" div .fade").addClass("fade-in");	
		}
		scrollActive = true;
		
	});
	updateMenu();
   }
	

}

SWFAddress.addEventListener(SWFAddressEvent.CHANGE, handleChange);




function buildGallery(){
	positionGallery();
	updateDots();
	$("#prev-btn").css("opacity",0);
	$("#next-btn").bind({click: function() {
		 gotoNext();
  	}});
	$("#prev-btn").bind({click: function() {
    	gotoPrev();
  	}});
	$("#dots img").each(function(index) {
		$(this).bind({click: function() {
    		gotoNum(index);
  		}});
    });

}

function gotoNext(){
	if(current < anzahl && currentPage == "portfolio"){
			current++;
			var xPos = 0-current*$(document).width();
			if(ie)
				$("#container").animate({marginLeft:xPos+"px"},700);
			else
				$("#container").css("margin-left",xPos+"px");
			if(current == anzahl)
				$("#next-btn").css("opacity",0);
			$("#prev-btn").css("opacity",1);
			updateDots();
			_gaq.push(['_trackPageview', "/portfolio/"+current]);
		}
}

function gotoPrev(){
	 if(current > 0 && currentPage == "portfolio"){
			current--;
			var xPos = 0-current*$(document).width();
			if(ie)
				$("#container").animate({marginLeft:xPos+"px"},700);
			else
				$("#container").css("margin-left",xPos+"px");
			if(current==0)
				$("#prev-btn").css("opacity",0);
			$("#next-btn").css("opacity",1);
			updateDots();
			_gaq.push(['_trackPageview', "/portfolio/"+current]);
		}
}
function gotoNum(num){
	 if( currentPage == "portfolio"){
			current  = num;
			var xPos = 0-current*$(document).width();
			if(ie)
				$("#container").animate({marginLeft:xPos+"px"},700);
			else
				$("#container").css("margin-left",xPos+"px");
			if(current==0)
				$("#prev-btn").css("opacity",0);
			else
				$("#prev-btn").css("opacity",1);
			if(current == anzahl)
				$("#next-btn").css("opacity",0);
			else
				$("#next-btn").css("opacity",1);
			updateDots();
			_gaq.push(['_trackPageview', "/portfolio/"+current]);
		}
}

function positionGallery(){
	$("#container div.item").each(function(index) {
		var xPos = (index*$(document).width())-430;
		$(this).css("margin-left",xPos+"px");
		anzahl = index;
    });
	var xPos = 0-current*$(document).width();
	$("#container").css("margin-left",xPos+"px");
}

function updateDots(){
	$("#dots img").each(function(index) {
		if(index == current)
			$(this).attr("src","images/dot.png");
		else
			$(this).attr("src","images/dot2.png");
    });	
}

function updateMenu(){
	$("#sectionMenu a").each(function(index) {
		if($(this).attr("href") == "#"+currentPage)
			$(this).addClass("active");
		else
			$(this).removeClass("active");
    });	
}
	


	
