var slideWidth = 990;
var slideId = 0;
var whatwedo_fade = false;
var whatwedo_timer;

$(document).ready(function(){

	whatwedo_random();
	whatwedo_timer = setInterval(function() {
		whatwedo_next();
	}, 3000);
	
	$('[id^=slideText]').each(function() {
        $(this).hide();
    });

    if ($('#slideText_1').children('.hideinfo').size() == 1){
        $('.infoButton').hide();
    }else{
		$('.infoButton').animate({"right": "0px"}, {duration: "fast"});;
        $('.infoButton').show();

    	$('#slideText_1').show();
		$('.slideHolder .infoText').hide();
		$('.slideHolder .infoText').slideUp();
	    $('.slideHolder .infoText').animate({"height": "show"}, {duration: "fast"});
    }
	
    // image slider functionality
	$('#prev').click(function(){
        changeSlide(slideId);
    });
    $('#next').click(function(){
        changeSlide(slideId+2);
    });

    $('.slideHolder .infoButton div').hoverIntent(function(){

        $('[id^=slideText]').each(function() {
            $(this).hide();
        });
        var slideNum = slideId+1;

        $('#slideText_'+slideNum).show();
        $('.slideHolder .infoText').animate({"height": "show"}, {duration: "fast"});
        
    },function(){
        var slideNum = slideId+1;
        $('.slideHolder .infoText').animate({"height": "hide"}, {duration: "fast"});
    });
    
    $('.slideHolder .infoButton div').click(function(){
        var slideNum = slideId+1;
        $('.slideHolder .infoText').animate({"height": "toggle"}, {duration: "fast"}); 
    });    
    
    $('.slideHolder .previous').hoverIntent(function(){
        $('.slideHolder .previousBg').animate({"width": "toggle"}, {duration: "fast"});
    },function(){
        $('.slideHolder .previousBg').animate({"width": "toggle"}, {duration: "fast"});
    });

	$('.slideHolder .next img').hover(function(){
		$('.slideHolder .next img').attr('src','themes/inoob/images/navi_right_over.png');
	},function(){
		$('.slideHolder .next img').attr('src','themes/inoob/images/navi_right.png');
	});
	
	$('.slideHolder .previous img').hover(function(){
		$('.slideHolder .previous img').attr('src','themes/inoob/images/navi_left_over.png');
	},function(){
		$('.slideHolder .previous img').attr('src','themes/inoob/images/navi_left.png');
	});
    
    $('.slideHolder .next').hoverIntent(function(){
        $('.slideHolder .nextBg').animate({"width": "toggle"}, {duration: "fast"});
    },function(){
        $('.slideHolder .nextBg').animate({"width": "toggle"}, {duration: "fast"});
    });
    
    $('.servItemHovering').hover(function () {
        $(this).children('.example').show();
    }, function(){
        $(this).children('.example').hide();
    });

    // press releases hover/click
    $('.pressrelease').hover(function(){
        var url = $(this).children('.presstopic').children('a').attr('href');
        if(url)        
            $(this).css('cursor', 'pointer');
    });
    $('.pressrelease').click(function(){        
        var url = $(this).children('.presstopic').children('a').attr('href');
        if(url)
            window.location = url;
    });
    
    // jobs hover/click
    $('.job').hover(function(){
        $(this).css('cursor', 'pointer');
    });
    $('.job').click(function(){
        var url = $(this).children('a').attr('href');
        window.location = url;
    });

    // bottom arrow hover
    $('#BottomArrow').hover(function(){
        $(this).addClass('hover');
        $('#Bottom a').addClass('hover');
    },function(){
        $('#Bottom a').removeClass('hover');
        $(this).removeClass('hover');
    });
    $('#Bottom a').hover(function(){
        $('#BottomArrow').addClass('hover');
    },function(){
        $('#BottomArrow').removeClass('hover');
    });
    
    // smooth scroll to top
    $('a.top').click(function(event){
        event.preventDefault();
        $('html, body').animate({scrollTop: '0px'}, 300);
        return false;
    });

    // ipad etc. wipe
    if (!$.browser.msie){
        $('.slide').touchwipe({
            wipeLeft: function(){
                $('.slideHolder .infoText').animate({"height": "hide"}, {duration: "fast"});
                changeSlide(slideId+2);
            },
            wipeRight: function(){
                $('.slideHolder .infoText').animate({"height": "hide"}, {duration: "fast"});
                changeSlide(slideId);
            }
        });
    }
    
    // subscribe
    $("#Form_SubscribeForm").submit(function(){
         $('#result').load('SubscribeFormController', {values: $(this).serialize()}, function(){
            if ($('#result').html() != 'false'){
                $('#wait').hide();
                $('.subscribeForm').removeClass('error');
                $('.subscribeForm').hide();
                $('#success').show('fast');
            }else{
                $('.subscribeForm').addClass('error');
                $('#wait').hide();
                $('#newsletter').show();
            }
         });
         return false;
      });
    $("#Form_SubscribeForm2").submit(function(){
         $('#result2').load('SubscribeFormController', {values: $(this).serialize()}, function(){
            if ($('#result2').html() != 'false'){
                $('.subscribeForm2').removeClass('error');
                $('.subscribeForm2').hide();
                $('#success2').show('fast');
            }else{
                $('.subscribeForm2').addClass('error');
            }
         });
         return false;
      });
});

function changeSlide(id){
	
    // display i-button    
//    if ($('#slideText_'+id).children('.hideinfo').html() == 1){

    if ($('#slideText_'+id).children('.hideinfo').size() == 1){
        $('.infoButton').animate({"right": "-60px"}, {duration: "fast"});;
    }else{
        $('.infoButton').animate({"right": "0px"}, {duration: "fast"});;
		$('.infoButton').show();
    }

    // var cls = '#slideText_' + id;
    //  // alert($(cls + ' .agency').size());
    // if ($(cls + ' .agency').size() == 0)
    //     $('.infobutton').hide();

    // hide/display video
    $('.youtube').hide();
    $('.slide'+id).children('.youtube').show();
    
	id--;
    //make sure the id is within the allowed limits
    var max = $('.slide').size()-1;
    if (id < 0)id=0;
    if (id > max) id = max;
    slideId = id;
    
    // hide next button
    if (id == max) {
        $('.next').hide();
    }else{
        $('.next').show();
    }
    
    if (id == 0){
        $('.previous').hide();
    }else{
        $('.previous').show();
    }

    //animate
    $('.slides').animate({
        left: -1*id*slideWidth
    }, 1000);
    
    $('[id^=slideText]').each(function() {
        $(this).hide();
    });
    var slideNum = slideId+1;

    $('#slideText_'+slideNum).show();
	$('.slideHolder .infoText').hide();
	$('.slideHolder .infoText').slideUp();
    $('.slideHolder .infoText').animate({"height": "show"}, {duration: "fast"});

    // 44 = base length, 45 = length of one unit forward
    var length = 25+id*28;
    $('#numbersSelectedBack').animate({"width": length}, {duration: "fast"});
    
    //change the underlined number
    $('.pages > a').removeClass('selected');
    $('.pages > a').each(function(){
        if ($(this).html() == id+1) $(this).addClass('selected');
    });
    
}

function whatwedo(e, id) {
	if($(e).hasClass("selected")) return false;
	clearTimeout(whatwedo_timer);
	
	$("#whatwedo_titles div.selected").removeClass("selected");
	$(e).addClass('selected');
	
	if(!whatwedo_fade) {
		whatwedo_fade = true;
		$("#whatwedo_content_wrapper div:visible h2").animate({ top: '-100px'}, 300, function() {});
		$("#whatwedo_content_wrapper div:visible p").animate({ top: '220px'}, 300, function() { $(this).parent().hide(); });
		setTimeout(function() {
			$("#whatwedo_content_"+id+" h2").css("top","20px");
			$("#whatwedo_content_"+id+" p").css("top","110px");
			$("#whatwedo_content_"+id).fadeIn();
			whatwedo_fade = false;
		}, 100);
	}
}

function whatwedo_next() {
	$("#whatwedo_content_wrapper div:visible h2").animate({ top: '-100px'}, 300, function() {});
	$("#whatwedo_content_wrapper div:visible p").animate({ top: '220px'}, 300, function() {
		$(this).parent().hide();
		$(this).parent().children('h2').css("top","20px");
		$(this).css("top","110px");
		
		if($("#whatwedo_titles div.selected").next().length > 0) $("#whatwedo_titles div.selected").removeClass("selected").next().addClass("selected");
		else {
			$("#whatwedo_titles div.selected").removeClass("selected");
			$("#whatwedo_titles div:first").addClass("selected");
		}
		
	});
	setTimeout(function() {
		if($("#whatwedo_content_wrapper div:visible").next().length > 0) $("#whatwedo_content_wrapper div:visible").next().fadeIn();
		else $("#whatwedo_content_wrapper div:first").fadeIn();

	}, 100);
}

function whatwedo_random() {
	var count = $("#whatwedo_titles div").length;
	var rand = random(1,count);
	
	$("#whatwedo_title_"+rand).addClass("selected");
	$("#whatwedo_content_"+rand).show();
}

function random(from, to){
	return Math.floor(Math.random() * (to - from + 1) + from);
}
