var agent = navigator.userAgent.toLowerCase();
// We need to eliminate Symbian, Series 60, Windows Mobile and Blackberry
// browsers for this quick and dirty check. This can be done with the user agent.
var otherBrowser = (agent.indexOf("series60") != -1) || (agent.indexOf("symbian") != -1) || (agent.indexOf("windows ce") != -1) || (agent.indexOf("blackberry") != -1);
// If the screen orientation is defined we are in a modern mobile OS
var mobileOS = typeof orientation != 'undefined' ? true : false;
// If touch events are defined we are in a modern touch screen OS
var touchOS = ('ontouchstart' in document.documentElement) ? true : false;
// iPhone and iPad can be reliably identified with the navigator.platform
// string, which is currently only available on these devices.
var iOS = (navigator.platform.indexOf("iPhone") != -1) ||
    (navigator.platform.indexOf("iPad") != -1) ? true : false;
// If the user agent string contains "android" then it's Android. If it
// doesn't but it's not another browser, not an iOS device and we're in
// a mobile and touch OS then we can be 99% certain that it's Android.
var android = (agent.indexOf("android") != -1) || (!iOS && !otherBrowser && touchOS && mobileOS) ? true : false;
var touch = (touchOS || iOS || android);


function show_touch_menu(caller)
{
    if (window.innerWidth <=480)
    {
	var menu = $('body').find("#menu-container")[0];
	if ($(menu).hasClass("show-block"))
	{
	    $(menu).hide(400);
	    $(menu).removeClass("show-block");
	    $('#main').show();
	    $('#footer').show();
	    $('.buttons').show();
	    $('#close-touchscreen-menu').hide();
	}
	else
	{
	    $(menu).show(400);
            $(menu).addClass("show-block");
	    $('#main').hide();
            $('#footer').hide();
	    $('.buttons').hide();
	    $('#close-touchscreen-menu').show();
	}
    }
    
}

function fix_rotate_on_menu()
{
    var menu = $('body').find("#menu-container")[0];
    if (window.innerWidth >=480 && $('#main').filter(":hidden").length > 0)
    {
        $(menu).removeClass("show-block");
        $('#main').show();
        $('#footer').show();
        $('.buttons').show();
        $('#close-touchscreen-menu').hide();
    }
}

function show_touch_sub_menu(caller)
{
    if (window.innerWidth <=480)
    {
	if ($(caller).parents(".sub-menu").length > 0){
	    var subMenus = ($(caller).parents(".sub-menu").find(".sub-menu"));
			   }
	    else {
	var subMenus = $('body').find(".sub-menu");
	    }
	var menu = $(caller.parentNode).find(".sub-menu")[0];
	var isSelected = $(menu.parentNode).hasClass('selected');
	subMenus.each(function() {
	    $(this).hide(400);
	    $(this.parentNode).removeClass('selected');
	});
        menu = $(caller.parentNode).find(".sub-menu")[0];
	if (!isSelected) {
            $(menu).show(400);
	    $(menu.parentNode).addClass('selected');
	}
    }
}

function add_onclick_to_menu()
{
    if (touch) 
    {
	$('body').find("#wpadminbar").hide();
	var menuItems = $('body').find(".menu-item");
	menuItems.each(function() {
	    var subMenu = $(this).children('ul');
	    if ($(subMenu).length > 0) {
		var link = $(this).children('a')[0];
		var newlink = $(link.parentNode).clone();
		if ($(newlink).children('a')[0].innerText){
		    $(newlink).children('a')[0].innerText = $(newlink).children('a')[0].innerText + " Home";
		}
		else {
		    $(newlink).children('a')[0].textContent = $(newlink).children('a')[0].textContent + " Home"
		}
		$(newlink).children('.sub-menu').remove();
		newlink.prependTo(subMenu[0]);
		$(link).attr('onClick', 'show_touch_sub_menu(this)');
		$(link).attr('href', 'javascript: void(0)');
		$(this).addClass('has-sub-menu');
	    }
	});
    }
}

function remove_release_note_headings()
{
    var table_rows = $('table').find('tr');
    if (window.innerWidth <=480)
    {
	table_rows.each(function() {
	    var row = this;
	    if ($(this).children("td:contains('Trac ID')").length > 0) $(this).addClass("hidden");
	});
	    }
    else
    {
            table_rows.each(function() {
            var row = this;
            if ($(this).children("td:contains('Trac ID')").length > 0) $(this).removeClass("hidden");
        });
    }

}

function change_home_icon() 
{
    var image = $("#site-title").find("img")[0];
    if (window.innerWidth <= 750 && window.innerWidth >=480 )
    {
	$(image).attr("src", "/wp-content/uploads/images/single/globeSmall49.png");
    }
    else
    {
	$(image).attr("src", "/wp-content/themes/twentytenmoddedfortest/images/eproLogo.png");
    }
}

$(add_onclick_to_menu);
$(window).resize(remove_release_note_headings);
$(window).resize(change_home_icon);
$(window).resize(fix_rotate_on_menu);
$(change_home_icon);
$(remove_release_note_headings);

