var short_desc_default = '<p>DXY builds mobile software. We create innovative, powerful solutions for companies, organizations and for the people that use mobile devices.</p>';

function show_current_page(menu_ul) {
    var current_page =$(menu_ul).data('current_page');
    if(current_page == undefined) {
	    current_page = 1;
	}
	var first_index = ((current_page - 1) * 7);
	var last_index = ((current_page) * 7);
	var total_items = $(menu_ul).children('li').length;
	if(last_index >= total_items) {
		last_index = total_items;
		$(menu_ul).siblings('.next').fadeOut();
	} else {
	    $(menu_ul).siblings('.next').fadeIn();
	    $(menu_ul).siblings('.next').attr('href',$.address.path() + '?page=' + (current_page + 1));
	    $(menu_ul).siblings('.next').address();
	}
	$(menu_ul).siblings('.pagenumber').fadeIn();
	$(menu_ul).siblings('.pagenumber').html(current_page);
	if(current_page != 1) {
	    $(menu_ul).siblings('.previous').fadeIn();
	    $(menu_ul).siblings('.previous').attr('href',$.address.path() + '?page=' + (current_page - 1));
	    $(menu_ul).siblings('.previous').address();
	    
	    $(menu_ul)
	        .children('li:gt(' + (last_index - 1) + '),li:lt(' + first_index + ')').slideUp()
	        .end()
    		.children('li:lt(' + last_index + '):gt('+ (first_index - 1) + ')').slideDown()
    		.end();
	} else {
	    $(menu_ul).siblings('.previous').fadeOut();
	    $(menu_ul)
	        .children('li:gt(' + (last_index - 1) + ')').slideUp()
	        .end()
    		.children('li:lt(' + last_index + ')').slideDown()
    		.end()
	}
}


function changeColor(next_path) {
    if(next_path.length == 0 || next_path.indexOf('search') == 0) {
        var toColor = '#333';
    } else {
        var toColor = '#' + $('li.' + next_path[0] + ' a').css('color');
    }
	$('#logo img').animate({backgroundColor: toColor}, 250);
	$('#footer').animate({borderTopColor: toColor}, 250);
	$('#header').animate({borderBottomColor: toColor}, 250);
}

var current_path;
var rotate_timer;
$(document).ready(function() {
    //     setTimeout('window.scrollTo(0, 10)',500);
    // setTimeout('animateIn()', 1000);
	$('#searchform').ajaxForm(function(data) {
		$("#content").fadeOut();
		$('ul.rootmenu').slideUp();
		$('div#searchresults').html(data);
		$("#menu ul").css('font-size','35px');
		$("#menu ul.submenu > li").css('display', 'block');
		$('ul.searchmenu').slideDown();
		window.location.hash = '#!/search/' + $("#search-input").val();
		show_current_page($('ul.searchmenu')[0]);
	});
	
	$("#search #search-input").focus(function() {
        if($(this).val()=="SEARCH DXY") {
            $(this).val('');
        }
    });
    
    $("#search #search-input").blur(function() {
        if($(this).val()=='') {
            $(this).val("SEARCH DXY");
        }
    });
    $.address.init(function(event) {
        current_path = [];
    })
    .change(function(event) {  
        // do something depending on the event.value property, e.g.
        // $('#content').load(event.value + '.xml');
        var next_path = event.pathNames;
        if(next_path.length == 1 && next_path[0] == "speak-to-dxy") {
            Dajaxice.dxysolutions.fetch_speak_to_dxy('Dajax.process');
        } else {
            navigate(current_path,next_path);
        }
        current_path = next_path;
    });
});

function rotate(fadeOut, spans) {
    if(fadeOut != null) {
        $(fadeOut).fadeOut('slow', function() {
            $(spans[0]).fadeIn('slow', function() {
                if(spans.length > 1) {
                    rotate_timer = window.setTimeout(function(){rotate(spans[0], spans.slice(1))}, 2000);
                }
            });
        });
    } else {
        $('#menu li.mobile span:visible').hide();
        $(spans[0]).fadeIn('slow', function() {
            if(spans.length > 1) {
                rotate_timer = window.setTimeout(function(){rotate(spans[0], spans.slice(1))}, 2000);
            }
        });
    }
}

function navigate(current_path, next_path) {
    changeColor(next_path);
    update_short_description(next_path);
    if(next_path.length==0) {
        rotate_timer = window.setTimeout(function(){rotate(null, $('#menu li.mobile span'))}, 1500);
    } else {
        window.clearTimeout(rotate_timer);
    }
    
    if(next_path.length==0) {
        navigate_home(current_path, next_path);
    } else if (next_path.indexOf('search') == 0) {
        navigate_search(current_path, next_path);
    } else if(current_path.length == 0 && next_path.length == 1) {
        navigate_select(current_path, next_path);
    } else if(current_path[0] == next_path[0]) {
        if(current_path.length > next_path.length) {
            navigate_deslect(current_path, next_path);
        } else {
            navigate_select(current_path, next_path);
        }
    } else {
        navigate_jump(current_path, next_path);
    }
}

function update_short_description(next_path) {
    if(next_path.length==0) {
        var content_desc = short_desc_default;
    } else {
        var next_link = $('.' + next_path[next_path.length - 1] + ' > a.menulink')[0];
    	if ($(next_link).attr('title') != undefined) {
            var content_desc = '<p>' + $(next_link).attr('title') + '</p>';
        } else {
            var content_desc = short_desc_default;
        } 
    }
    $("#content-description").html(content_desc);
}

	
function navigate_select(current_path, next_path) {
    var fontSizeInt = parseInt($("#menu ul").css('font-size').substr(0,2));
    var next_li = $('li.' + next_path[next_path.length - 1]);
    var next_link = $('.' + next_path[next_path.length - 1] + ' > a.menulink')[0];
    
    $(next_link).addClass('selected');
    $(next_li).siblings('li').slideUp();
    
    if($.address.parameter('page') != undefined) {
        var current_page = parseInt($.address.parameter('page'));
        $(next_li).children('ul.submenu').data('current_page',current_page);
    }
    if($(next_li).children('ul.submenu').length  == 1) {
        //We're selecting into another set of menus
        var toFontSize = 35;
		$("#menu ul").animate({fontSize: toFontSize + "px"},250);
		$('#mobilepicture').fadeOut();
		
        show_current_page($(next_li).children('ul.submenu')[0]);
    } else {
        //We're selecting into content
        var slug = $(next_li).attr('class');
        Dajaxice.dxysolutions.fetch_content('Dajax.process', {'slug': slug});
        $('.next, .previous, .pagenumber').fadeOut();
    }
}

function navigate_deslect(current_path, next_path) {
    var current_li = $('li.' + current_path[current_path.length - 1]);
    var next_li = $('li.' + next_path[next_path.length - 1]);
    
    if($(current_li).children('ul.submenu').length  == 1) {
        //We're just at a menu
        
    } else {
        //We're currently looking at content 
        $("ul.rootmenu").css('border','none');
        $('#content').fadeOut();
		$('#mobilepicture').fadeOut();
        show_current_page($(next_li).children('ul.submenu')[0]); 
    }
}

function navigate_jump(current_path, next_path) {
    $('a.selected').removeClass('selected');
    
    if(current_path.length != 0) {
        navigate_home();
    }
    
    var temp_prev_path = [];
    
    for(var i=1;i<=next_path.length;i++) {
        var current_li = $('li.' + current_path[current_path.length - 1]);
        var next_li = $('li.' + next_path[next_path.length - 1]);
        setTimeout('navigate_select(' + JSON.stringify(temp_prev_path) + ',' + JSON.stringify(next_path.slice(0,i)) + ')', (i + 1) * 500);
        temp_prev_path = next_path.slice(0,i);
    }
    
    var current_li = $('li.' + current_path[current_path.length - 1]);
    var next_li = $('li.' + next_path[next_path.length - 1]);
    
    if($(current_li).children('ul.submenu').length  == 1) {
        //We're just at a menu
        
    } else {
        //We're currently looking at content 
        $("ul.rootmenu").css('border','none');
        $('#content').fadeOut();
		$('#mobilepicture').fadeOut();
        show_current_page($(next_li).children('ul.submenu')[0]); 
    }
}

function navigate_search(current_path, next_path) {
    if($.address.parameter('page') != undefined) {
        var current_page = parseInt($.address.parameter('page'));
        $('ul.searchmenu').data('current_page',current_page);
    }
    show_current_page($('ul.searchmenu')[0]);
}

function navigate_home(current_path, next_path) {
    $("#content").fadeOut();
	$('.next, .previous, .pagenumber').fadeOut();
	
	$('ul.searchmenu').slideUp();
	$('ul.searchmenu').html('');
	
	$("ul.rootmenu")
	    .css('border','none')
	    .find('.selected').removeClass('selected')
	    .end()
	    .find('ul.submenu > li').slideUp()
	    .end()
	    .slideDown()
	    .find('> li').delay(250).slideDown()
	    .end()
	    .delay(450).animate({fontSize:"50px"},500);	
	
	$('#mobilepicture').fadeIn();

}
