/******************************************************
 * HARDWARE OVERVIEW CODE
 *****************************************************/
var Hardware = new Object();

Hardware.Initialize = function () {

    // Get the hardware thumbnail list
    var thumbs = $( 'ul.prodThumbs li a' );
    if( thumbs.length > 0 ) {
	
	// Setup on clicks
	thumbs.each( function (idx) {
		if( !thumbs[idx].initialized ) {
		    thumbs.eq( idx ).click( function() {
			    return Hardware.ActivateThumbnail( $( this ), idx );
		    } );
		    thumbs[idx].initialized = true;
		}
	} ); // each thumbnail
    } // if thumbnails

    // Get callouts
    var callouts = $( 'ul.callouts li a' );
    if( callouts.length > 0 ) {
	// Setup mouse over/out
	callouts.each( function (idx) {
		if( !callouts[idx].initialized ) {
		    callouts.eq( idx ).mouseover( function() {
			    return Hardware.ShowCalloutContent( $( this ), idx );
		    } );
		    callouts.eq( idx ).mouseout( function() {
			    return Hardware.HideCalloutContent( $( this ), idx );
		    } );
		    callouts.eq( idx ).click( function() { return false; } );
		    callouts[idx].initialized = true;
		}
	} ); // each callout
    } // if callouts

    return;
} // Initialize

Hardware.ActivateThumbnail = function (activeThumb, idx) {

    // Change selected thumb
    var thumbs = $( 'ul.prodThumbs li a' );
    thumbs.removeClass( 'selected' );
    activeThumb.addClass( 'selected' );

    // Get content
    $.ajax( {
	    url      : '/en/services/hardware/get-highlight' + (idx+1) + '-content.html', // Todo: Move this url out of JS
	    dataType : 'html',
	    error    : function () { Hardware.OnError( activeThumb ) },
	    timeout  : 5000, // trigger error after 5 seconds
	    success  : function (html) { Hardware.OnSuccess( html ) }
    } );
    
    // Return false so browser does not go to link
    return false;
} // ActivateThumbnail

Hardware.OnSuccess = function ( content ) {
    
    // Replace content on page with content from ajax request
    $( '.prodHighlights' ).html( content );
    Hardware.Initialize();

} // OnSuccess

Hardware.OnError = function (activeThumb) {
    
    // Retrieve link and go to it
    var link = activeThumb[0].tagName.toUpperCase() == 'A' ? activeThumb[0]
             :                                             $( 'a', activeThumb )[0]
             ;
    

    if( link ) {
	location.href = link.href;
    }
    else {
	// Todo: Show error ?
    }
} // OnError

Hardware.ShowCalloutContent = function(callout, idx) {
    $( '.highlightsContent div' ).hide();
    
    var cssClass = callout[0].parentNode.id + 'Content';
    
    $( '.highlightsContent div.' + cssClass ).show();
} // ShowCalloutContent

Hardware.HideCalloutContent = function(callout, idx) {
    $( '.highlightsContent div' ).hide();
    $( '.highlightsContent div.defaultContent' ).show();
} // HideCalloutContent

/******************************************************
 * SOFTWARE ACTIVITIES CODE
 *****************************************************/
var Software = new Object();

Software.Initialize = function () {
    
    // Get the thumbnail list
    var thumbs = $( 'ul.activityIcons li a' );
   
    
    if( thumbs.length > 0 ) {
	
	// Setup on clicks
	thumbs.each( function (idx) {
		if( !thumbs[idx].initialized ) {
		    thumbs.eq( idx ).click( function() {
			    return Software.ActivateThumbnail( $( this ), idx );
		    } );
		    thumbs[idx].initialized = true;
		}
	} ); // each thumbnail
    } // if thumbnails

    return;
} // Initialize

Software.ActivateThumbnail = function (activeThumb, idx) {

    // Change selected thumb
    var thumbs = $( 'ul.activityIcons li a' );
    thumbs.removeClass( 'selected' );
    activeThumb.addClass( 'selected' );
     

    // Get content
    $.ajax( {
	    url      : '/v2/laptop-php/services/software/get-activity' + (idx+1) + '-content.html', // Todo: Move this url out of JS
	    dataType : 'html',
	    error    : function () { Software.OnError( activeThumb ) },
	    timeout  : 5000, // trigger error after 5 seconds
	    success  : function (html) { Software.OnSuccess( html ) }
    } );
    
    // Return false so browser does not go to link
    return false;
} // ActivateThumbnail

Software.OnSuccess = function ( content ) {
    
    // Replace content on page with content from ajax request
    $( '.activities' ).html( content );

} // OnSuccess

Hardware.OnError = function (activeThumb) {
    
    // Retrieve link and go to it
    var link = activeThumb[0].tagName.toUpperCase() == 'A' ? activeThumb[0]
             :                                             $( 'a', activeThumb )[0]
             ;
    

    if( link ) {
	location.href = link.href;
    }
    else {
	// Todo: Show error ?
    }
} // OnError


/******************************************************
 * NEWSLINKS CODE
 *****************************************************/

var News = new Object();

News.Initialize = function () {

  var newslinks = $( 'dl#newsblurbs a' );

  // want newslinks to open in a new window

  newslinks.each(function(idx) {
    var a = newslinks[idx];
    a.onclick = target_blank;
    a.onkeypress = target_blank;
  });
}

/******************************************************
 * BODY.ONLOAD CODE
 *****************************************************/
$(document).ready( function () {

    //Navigation.Initialize();
    
    Hardware.Initialize();

    Software.Initialize();
   
    //News.Initialize();
	
    return true;
} );


/******************************************************
 * Email subscribe form
 *****************************************************/
function validateSubscribeForm( frm ) {
    var email = frm.ea;
    var AtPos = email.value.indexOf( "@" );
    var StopPos = email.value.lastIndexOf( "." );
    var Message = true;
    
    if( AtPos == -1 || StopPos == -1 ) {
	Message = false;
    }
    if( StopPos < AtPos ) {
	Message = false;
    }
    if( StopPos-AtPos == 1 ) {
	Message = false;
    }
    if( Message == false ) {
	alert( "Please enter a valid email" );
	frm.ea.focus();
    }

    return Message;
}

// Portable "open in new window" --
//   bind to onclick and onkeypress

function target_blank() {
  window.open( this.href );
  return false;
}

// ------[ Generic Popup Window ]------------------------------------------------- //
function popWin(url,w,h,scroll,tools,name,center) {
	var str = "height=" + h + ",innerHeight=" + h;
	str += ",width=" + w + ",innerWidth=" + w;
	if(!center) var center = false;
	if(!scroll) scroll = 0;
	if(!tools) tools = 0;
	if(!name) name = "pop";

	if((window.screen) && (center)) {
		var ah = screen.availHeight - 30;
		var aw = screen.availWidth - 10;

		var xc = (aw - w) / 2;
		var yc = (ah - h) / 2;

		str += ",left=" + xc + ",screenX=" + xc;
		str += ",top=" + yc + ",screenY=" + yc;
		}
		
	pop = window.open(url,name,'toolbar=' + tools + ',location=0,directories=0,status=0,menubar=0,scrollbars=' + scroll + ',resizable=1,' + str).focus();
	}

	// SEARCH BOX FUNCTION
	function handleSearchInput( el, str, bRestore )
	{
	    if( bRestore ) { // restore string
		if( el.value == "" ) {
		    el.value = str;
		}
	    } else { // clear string
		if( el.value == str ) {
		    el.value = "";
		}
	    }
	} // handleSearchInput
	
	
//-- Show or hide layers --//
function MM_showHideLayers() { //v9.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) 
  with (document) if (getElementById && ((obj=getElementById(args[i]))!=null)) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
    obj.visibility=v; }
}