/* The my lab pages

 * Last Update: September 08, 2008(Alex)

 * Description:
   The "Add to custom workout" links trigger the popup IFRAMES for workout add, select, edit, save.
   A dynamic IFRAME for the Workout Exercise List is created if a workoutId param is found in the query params or cookie.

 * Dependencies:
   - Requires: jquery.js, jquery.cookie.js, iframe_utils.js , common.js, my_lab_common.js
*/
 
// the popups are associated to the window object so they can be manipulated from inside the IFRAMES
jQuery.extend(window, {customPopups: {}});

// runs on document load
// checks the query params, if 
jQuery(function()
		{
			var qParams = getMyLabQueryParams(); // get query params
			var isExerciseDetailsPage = (jQuery('#detail a.addToWorkoutD').size() > 0)?true:false; // is exercise details page flag
			
			// save cookies
			if (qParams != null) // if query params are set
			{
				clearCookies(myLabCookies, {path: '/'}); // delete any  cookies previously set for the lab pages
				saveMyLabCookies(qParams['params'], {expires: 7, path:'/'}); // save the query params as cookies
			}
			
			var updatedParams = getMyLabCookies();
			if (updatedParams)
				var updatedParamsString = getSerializedSimpleObject(updatedParams);
			
			// update the popup trigger links, and build Workout Exercise List IFRAME
			jQuery('#detail a.addToWorkout, #detail a.addToWorkoutD') // update popup trigger links, add all params
				.each(function()
					  {
						  var triggerLink = jQuery(this);
						  var triggerLinkHref = new String(triggerLink.attr('href'));
						  var exerciseParams = triggerLinkHref.match(/exerciseId=\d+/i);
						  var filteredHref = triggerLinkHref.replace(triggerLinkHref.match(/\?.*/), '');
						  if (exerciseParams != null)
						  {
							filteredHref += '?' + exerciseParams;
							if (updatedParams)
								filteredHref += '&' + updatedParamsString;
						  }
						  else
						  {
							if (updatedParams)
								filteredHref += '?' + updatedParamsString;
						  }
							
						  triggerLink.attr('href', filteredHref);
					  });
			
			if (updatedParams)
			{
				// build the IFRAME object
				var workoutDetailsIframe = jQuery('<iframe name="WorkoutExerciseList" id="WorkoutExerciseList" marginheight="0" marginwidth="0" frameborder="0" allowtransparency="true" scrolling="no"></iframe>');
				
				// load the page inside the IFRAME
				workoutDetailsIframe.attr('src', 'workoutexerciselist.aspx' + '?' + updatedParamsString)
									.css({
											width: '100%',
											height: '1px', // initial height(will be changed from within the IFRAME to fit the IFRAME content)
											marginBottom: '10px',
											visibility: 'hidden' // hidden until loaded
										});
			}
			
			// init the popups for the details page and prepend Workout Exercise List IFRAME to the exercise right column
			if(isExerciseDetailsPage) // exercise detail page
			{
				var addToWorkoutDetailsLink = jQuery('#detail a.addToWorkoutD'); // get the popup trigger link
				var popupWrapper = jQuery('#container, #mainContainer').filter(':first');
				
				//init popup
				window.customPopups['ExerciseDetailsPopup'] = new dc_Popup(addToWorkoutDetailsLink, {
																popupIframeName: 'ExerciseDetailsPopup',
																popupWidth: 414, 
																popupHeight: 424, 
																popupLeft:  addToWorkoutDetailsLink.offset().left - 10 + 'px', 
																popupTop: addToWorkoutDetailsLink.offset().top + addToWorkoutDetailsLink.height() + 9 + 'px',
																popupParentNode: popupWrapper
														});
														
				if (workoutDetailsIframe)
				{
					workoutDetailsIframe.prependTo(jQuery('.video-right'));// prepend to right column, next to exercise video
				}
			}
			else
			{
				// init the popups for the list page and prepend Workout Exercise List IFRAME to the left column, if workoutId is defined
				var addToWorkoutListLinks = jQuery('#detail a.addToWorkout');
				addToWorkoutListLinks
					.each(function(index)
						{
							var ind = index;
							var triggerLink = jQuery(this);
							var popupWrapper = jQuery('#container, #mainContainer').filter(':first');
							window.customPopups['ExerciseListPopup' + ind] = new dc_Popup(triggerLink, {
																					popupIframeName: 'ExerciseListPopup' + ind,
																					popupWidth: 414, 
																					popupHeight: 424, 
																					popupLeft:  triggerLink.offset().left - 268 + 'px', 
																					popupTop: triggerLink.offset().top + triggerLink.height() + 9 + 'px',
																					popupParentNode: popupWrapper
																				});
						});
						
				if (workoutDetailsIframe)
				{
					workoutDetailsIframe.prependTo(jQuery('#results'));// prepend to left column, above search results
				}
			}
			
		})