/*!
 * Title:	Product Selector Widget
 * Author: 	Ryan Crosby (tweaked by Kaena Miller)
 * Date:	Jan 6 2011
 *
 * Uses JQuery javascript library
 *
 *
 * 
 * 
 *
 */

var debug = false;

var selPIM = '';		// Holds the currently selected PIM
var selPDA = '';		// Holds the currently selected PDA	

$(document).ready(function(){
						   
	$('.sub-menu').hide(); 								//Hides the PIM/PDA submenus on page load
	$('#GO').hide(); 									//Hides the GO button on page load
	$('#pdaList-google a').html('<img src="/graphics/pda-google-32.png">Sync with Google');									//Sets the list item for PDA-Google to have custom text
	$('#pdaList-windowslive a').html('<img src="/graphics/pda-windowslive-32.png">Sync with Windows Live/Hotmail');			//Sets the list item for PDA-Google to have custom text
	$('#pimList-palmdesktop a').html('<img src="/graphics/pim-palm-32.png">Palm Desktop');									//Sets the list item for PIM-PDT to have custom text

	$('#PIM').click(
				function() {							// On click, expand the PIM list
		$('#PIM ul.sub-menu').slideToggle(250);
		//alert('PIM clicked');
				}
	);

	$('#PDA').click(
				function() {							// On click, expand the PDA list
		$('#PDA ul.sub-menu').slideToggle(250);
		//alert('PIM clicked');
				}
	);

	// This handles the user clicking on an item in the PIM list.
	$('#PIM ul.sub-menu li').click(function() {
		selPIM = $(this).attr('target');				// Set the currently selected PIM
		
		var selPIMName = $(this).text();				// This is the long name of the PIM
		
		$('#pimTitle').html(selPIMName);				// This will change the title of the drop down button
		$('#pimTitle').addClass('activeTitle');			// Sets active style
		$('#PIM .drop-menu').addClass('selected orangewash-button');
		$('#PIM ul.sub-menu').slideToggle(250); 		// Closes the list upon selection

		if ( selPIM != '' && selPDA != '' ) {			// If both PIM and PDA have been selected, draw the GO button
			$('#GO').fadeIn(700);
			$('#advisor span').fadeIn(2000);
		}
		
		if (debug)
			alert( 'clicked on specific PIM ' + selPIM );
			
		return false;									// return false, stops the chain of event handlers, 
														// so the #PIM event handler does not get called
	});
	
	// This handles the user clicking on an item in the PDA list.
	$('#PDA ul.sub-menu li').click(function() {
		selPDA = $(this).attr('target');				// Set the currently selected PDA
		
		var selPDAName = $(this).text();				// Passes the html content of the list item to a temp var
		
		$('#pdaTitle').html(selPDAName);				// sets selected pda as active
		$('#pdaTitle').addClass('activeTitle');			// Sets active style
		$('#PDA .drop-menu').addClass('selected bluewash-button');
		$('#PDA ul.sub-menu').slideToggle(250); 		// Closes the list upon selection
		
		if ( selPIM != '' && selPDA != '' ) {			// If both PIM and PDA have been selected, draw the GO button
			$('#GO').fadeIn(700);
			$('#advisor span').fadeIn(2000);
			//$('#GOholder span').css({'visibility': 'normal', 'left': '100px'});
		}
		
		if ( selPDA == 'google' ) {
		$('#pdaTitle').html('Google');
		}
		if ( selPDA == 'windowslive' ) {
		$('#pdaTitle').html('Windows Live');
		}
		if ( selPDA == 'PCsync-OL' ) {
		$('#pdaTitle').html('Sync to Outlook');
		}
		if ( selPDA == 'PCsync-PDT' ) {
		$('#pdaTitle').html('Sync to Palm Desktop');
		}
		if ( selPDA == 'PCsync-LN' ) {
		$('#pdaTitle').html('Sync to Lotus Notes');
		}

		if (debug)
			alert( 'clicked on specific PDA ' + selPDA );
			
		return false;									// return false, stops the chain of event handlers, 
														// so the #PDA event handler does not get called
	});

	// Displays error message when user has not selected PIM/PDA
	$('#grayGO').click(function() {
		var msgOptions = '';
		
		// Make sure a PIM/PDA are selected
		// use the msgOptions to check later.
		if ( selPIM == '' ) {
			msgOptions = 'PC database';
		}
		if ( selPDA == '' ) {
			if ( msgOptions != '' ) {
				msgOptions += ' and ';
			}
			msgOptions += 'device';
		}

		alert('Please select a ' + msgOptions + '.'); 	// Alerts user they need to choose options
		return false;

	});
		
	$('#GO').click(function() {			//Sends the user to the correct module tabs page when they click the GO button

		if ( selPIM == 'wiseagent' ) {
			$(window.location).attr('href','http://www.companionlink.com/wiseagent/');	
		}
		else if ( selPIM == 'infusionsoft' ) {
			$(window.location).attr('href','http://www.companionlink.com/infusionsoft/');	
		}
		else if ( selPIM == 'salesjunction' ) {
			$(window.location).attr('href','http://www.companionlink.com/salesjunction/');	
		}
		else if ( selPIM == 'respond' ) {
			$(window.location).attr('href','http://www.companionlink.com/respond/');	
		}
		else if ( selPDA == 'PCsync-OL' || selPDA == 'PCsync-PDT' || selPDA == 'PCsync-LN' ) {
			$(window.location).attr('href','http://www.companionlink.com/pimtopim/');	
		}
		else {
			$(window.location).attr('href','http://www.companionlink.com/' + selPDA + '/' + selPIM + '/');
		}
		return true;
	});
		

});
