function getIEVersionNumber( ) {
    var ua = navigator.userAgent;
    var MSIEOffset = ua.indexOf("MSIE ");
    if (MSIEOffset == -1) {
        return 0;
    } else {
        return parseFloat(ua.substring(MSIEOffset + 5, ua.indexOf(";", MSIEOffset)));
    }
}

$(document).ready(function(){
	initTabs();
	initReadMoreTop();
	initReadMoreBottom();
	
	// Check the screen resolution and the User browser version
	if(
        screen.width > 1024 
        &&
        // Firefox 2.x 
        navigator.userAgent.indexOf("Firefox/2") == -1 
        &&
        // Opera 10 has the user agent Opera/9.80 (X11; Linux i686; U; en) Presto/2.2.15 Version/10.10 
        // Older Operas like 9.x does not have "Version" text
        (navigator.userAgent.indexOf("Opera/9") == -1 || navigator.userAgent.indexOf("Version") > 0) 
        &&
        (getIEVersionNumber() == 0 || getIEVersionNumber() >= 8)
    ) {
		// Find every <li> tag in #dock list
		$("#dock li").each(function(index) {
			// Get the future image description from the <li> inner text
			var altText = $(this).text();
			// Get the anchor jQuery object
			var anchorTag = $(this).find("a");
			// Create image file name for the future image
			var imageName = "images/" + anchorTag.attr("class")+".png";
			// Create new ID for the image, so we can easily replace image source when needed (for example when showing current web page)
			var imageId = anchorTag.attr("class");
			// Remove class attribute from the anchor object, and then replace existing content with the new image
			anchorTag.removeAttr("class").html("<img src=\""+imageName+"\" alt=\""+altText+"\" id=\""+imageId+"\"/>");
		});
					
		// Call dock plugin on a <div> containing <img>'s 100dla 1024 i ie7
		$('#dock').dock({ zoomFactor : 1.25385, zoomWidth : 150, timeOut : 3000 });
		
		//-----------------------------------------
		// Below we try to highlight apriopriate navigation button so it will always show us the page on which we are now...
		// Find the current page name
		var pageId = $("body").attr("id");
		
		// Do the basic page name validation, every body ID should begin with "page" prefix, so the "page" text must be positioned at the ID strings' 0 index
		if(pageId.indexOf("page") == 0) {
			// Get the real page name, fetching every letter except the first four (prefix string)
			var pageName = pageId.substring(4);
			// Create new image source for the current web page
			var hoverImageUrl = "images/btn" + pageName + "Hover.png";
			// Find the button linking to the current web page and replace its image source so it will be selected
			$("#btn" + pageName).attr("src", hoverImageUrl);
		}		
	} else {
		// This will run for IE 4/5/6/7, Firefox 2.x, Opera 9.x or resolutions smaller than 1024px
		var styleSheets = $("head").html();
		styleSheets += "\n<link href=\"styles/hackStyles.css\" rel=\"stylesheet\" type=\"text/css\" />\n";
		
		// This will run only when screen width is less than 1024px
		if (screen.width <= 1024) {
			styleSheets += "\n<link href=\"styles/resolution1024Styles.css\" rel=\"stylesheet\" type=\"text/css\" />\n";
		}

		$("head").html(styleSheets);	
	}
});


function initTabs() {

	// Does element exist?
	if (!$('ul.tabs').length) {
		// If not, exit.
		return; }
		
	// Reveal initial content area(s).
	$('div.tabContentWrapper').each(function() {
		$(this).find('div.tabContent:first').show().siblings().hide(); 
	});

	// Listen for click on tabs.
	$('ul.tabs a').click(function() {

		// If not current tab.
		if (!$(this).hasClass('current')) {

			// Change the current indicator.
			$(this).addClass('current').parent('li').siblings('li').find('a.current').removeClass('current');

			// Show target, hide others.
			$($(this).attr('href')).show().siblings('div.tabContent').hide();
		}

		// Nofollow.
		this.blur();
		return false;
	});
}

function initReadMoreBottom() {

	$('a.readMoreBottom').each(function(i) {
		this.textStr = $(this).text();
		this.textArr = this.textStr.split('|');
		this.text1 = this.textArr[0];
		this.text2 = this.textArr[1];
		//alert('text1: ' + this.text1 + ' text2:' + this.text2);
		$(this).parent().prev().hide();
		//$(this).bind('click', {text1: this.text1 }, toggleText);
		$(this).text(this.text1);
		$(this).bind('click', {text1: this.text1, text2: this.text2 }, function(event) {
  			//alert(event.data.text2);
			$(this).parent().prev().toggle();
			$(this).toggleClass('highlighted');
			if($(this).text() == event.data.text1) {
				$(this).text(event.data.text2);
			} else {
				$(this).text(event.data.text1); 
			}
			return false;
		});
	});
}

function initReadMoreTop() {

	$('.readMoreTop').each(function(i) {
		$(this).parent().parent().next().hide();		
		
		$(this).bind('click', function(event) {
			$(this).parent().parent().next().toggle();
		});
	});
}
