$.extend({
  getUrlVars: function(){
	// Get object of URL parameters
	//var allVars = $.getUrlVars();
	// Getting URL var by its nam
	//var byName = $.getUrlVar('name');
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
      hash = hashes[i].split('=');
      vars.push(hash[0]);
      vars[hash[0]] = hash[1];
    }
    return vars;
  },
  getUrlVar: function(name){
    return $.getUrlVars()[name];
  }
});
var revealContent = function(elem_name){
			$(elem_name).fadeIn(500, function() {
			});
}
// BEGIN: added by Magpie 8-17-10 - might want to refactor other popups to use this function
var launchPopup = function(src, thisWidth, thisHeight, name){
	var window_top = getModalTop(thisHeight);
	if(top.parent.frames['carousel_iframe'])
		top.parent.frames['carousel_iframe'].location=src;
	$.modal('<iframe src="' + src + '" width="'+thisWidth+'" height="'+thisHeight+'" frameborder="0" allowtransparency="true" style="border:0" name="'+name+'" id="'+name+'">', {
		closeHTML:"",
		opacity:70,
		overlayCss: {backgroundColor:"#c2c2c2"},
		position: [window_top+'px',],
		containerCss:{
			width:thisWidth,
			height:thisHeight,
			padding:0
		},
		dataCss:{
			width:thisWidth,
			height:thisHeight,
			padding:0
		},
		minHeight:thisHeight,
		overlayClose:true,
		onClose: function (dialog) {
			dialog.data.fadeOut('fast', function () {
				dialog.container.hide('fast', function () {
					dialog.overlay.fadeOut('fast', function () {
						$.modal.close();
					});
				});
			});
		}

	});
	return false;
}
// END: added by Magpie 8-17-10
// BEGIN: added by Magpie 8-30-10
var launchDivPopup = function(divId, thisWidth, thisHeight){
	var window_top = getModalTop(thisHeight);
	if(top.parent.frames['carousel_iframe'])
		top.parent.frames['carousel_iframe'].location=src;
	$("#" + divId).modal( {
		closeHTML:"",
		opacity:70,
		overlayCss: {backgroundColor:"#c2c2c2"},
		position: [window_top+'px',],
		containerCss:{
			width:thisWidth,
			height:thisHeight,
			padding:0
		},
		dataCss:{
			width:thisWidth,
			height:thisHeight,
			padding:0
		},
		minHeight:thisHeight,
		overlayClose:true,
		onClose: function (dialog) {
			dialog.data.fadeOut('fast', function () {
				dialog.container.hide('fast', function () {
					dialog.overlay.fadeOut('fast', function () {
						$.modal.close();
					});
				});
			});
		}

	});
	return false;
}

var closeDivPopup = function(){
	$.modal.close();
}
// END: added by Magpie 8-30-10

var setUpProgramDetailModal = function(){
//function setUpProgramDetailModal(){
	$('.modal_program_detail').click(function() {
		//alert($(this).attr('rel'));
		//Read REL attribute from div that was clicked - then pass to modal iframe URL.
		var rel_val = $(this).attr('rel');
		// Display an external page using an iframe
		var src = "/modal_product_detail?program="+rel_val;
		//var src = rel_val;
		var thisWidth=522;
		var thisHeight=633;
		var window_top = getModalTop(thisHeight);
		$.modal('<iframe src="' + src + '" width="'+thisWidth+'" height="'+thisHeight+'" frameborder="0" allowtransparency="true" style="border:0" name="modal_program_detail" id="modal_program_detail">', {
			closeHTML:"",
			opacity:70,
			overlayCss:  {
				backgroundColor:"#c2c2c2"
			},
			position: [window_top+'px',],
			containerCss:{
				width:thisWidth,
				height:thisHeight,
				padding:0
			},
			minHeight:thisHeight,
			overlayClose:true,
			onClose: function (dialog) {
				dialog.data.fadeOut('fast', function () {
					dialog.container.hide('fast', function () {
						dialog.overlay.fadeOut('fast', function () {
							$.modal.close();
						});
					});
				});
			}

		});
		return false;
	});
}
var getModalTop = function(thisHeight){
	var window_top = 0;
	var scroll_top = $("html").scrollTop();
	var window_height = $(document).height();
	var window_top = 0;//(scroll_top + (window_height - thisHeight) / 2);
	var window_visibleheight = $(window).height();
	//		alert('scroll_top:'+scroll_top + ' ,window_height:'+window_height + ' ,thisHeight:'+thisHeight + ' ,window_visibleheight:' + window_visibleheight);
	if (thisHeight > window_height){
		window_top = 0;//Modal bigger than scrollable window size, place at top of window (best we can do)
	}else if (window_visibleheight > thisHeight){
		window_top = scroll_top + ((window_visibleheight-thisHeight)/2);//Modal will fit - position in center of visible window
	}else if ((scroll_top+thisHeight) < window_height){
		window_top = scroll_top;//Modal is bigger than visible window, but will fit without overflowing document
	}else if ((scroll_top+thisHeight) > window_height){
		window_top = window_height - thisHeight;//Align to bottom of scrollable window if too long for page
	}
	window_top = Math.floor(window_top);//round down
	//alert('window_top:'+window_top);
	if($.browser.msie && parseFloat($.browser.version) < 7){//IE6 DETECTION	
		//IE6 Fix  
		window_top = window_top-scroll_top;
	}
	return window_top;
}

var launchTourDelayed = function(rel_val,delay){
	setTimeout("launchTour('"+rel_val+"');",delay);
}
var launchTour = function(rel_val){
	//This standalone function allows another modal to be closed and this to be opened with a single click in another modal.
	// Display an external page using an iframe
	//var src = "modal_tour_a.html?program="+rel_val;
	//alert('ABC:'+rel_val);
	var src = rel_val;
	var thisWidth=525;
	var thisHeight=633;
	var window_top = getModalTop(thisHeight);
	if(top.parent.frames['carousel_iframe'])
		top.parent.frames['carousel_iframe'].location=src;
	$.modal('<iframe src="' + src + '" width="'+thisWidth+'" height="'+thisHeight+'" frameborder="0" allowtransparency="true" style="border:0" name="modal_tour" id="modal_tour">', {
		closeHTML:"",
		opacity:70,
		overlayCss: {backgroundColor:"#c2c2c2"},
		position: [window_top+'px',],
		containerCss:{
			width:thisWidth,
			height:thisHeight,
			padding:0
		},
		dataCss:{
			width:thisWidth,
			height:thisHeight,
			padding:0
		},
		minHeight:thisHeight,
		overlayClose:true,
		onClose: function (dialog) {
			dialog.data.fadeOut('fast', function () {
				dialog.container.hide('fast', function () {
					dialog.overlay.fadeOut('fast', function () {
						$.modal.close();
					});
				});
			});
		}

	});
	return false;
}
var launchTools = function(rel_val){
	//This standalone function allows another modal to be closed and this to be opened with a single click in another modal.
	// Display an external page using an iframe
	//var src = "modal_tour_a.html?program="+rel_val;
	//alert('ABC:'+rel_val);
	var src = "/modal_tools_sample_plan?program="+rel_val;
	var thisWidth=915;
	var thisHeight=710;
	var window_top = getModalTop(thisHeight);
	if(top.parent.frames['carousel_iframe'])
		top.parent.frames['carousel_iframe'].location=src;
	$.modal('<iframe src="' + src + '" width="'+thisWidth+'" height="'+thisHeight+'" frameborder="0" allowtransparency="true" style="border:0" name="modal_tools" id="modal_tools">', {
		closeHTML:"",
		opacity:70,
		overlayCss: {backgroundColor:"#c2c2c2"},
		position: [window_top+'px',],
		containerCss:{
			width:thisWidth,
			height:thisHeight,
			padding:0
		},
		dataCss:{
			width:thisWidth,
			height:thisHeight,
			padding:0
		},
		minHeight:thisHeight,
		overlayClose:true,
		onClose: function (dialog) {
			dialog.data.fadeOut('fast', function () {
				dialog.container.hide('fast', function () {
					dialog.overlay.fadeOut('fast', function () {
						$.modal.close();
					});
				});
			});
		}

	});
	return false;
}


var launchToolsWide = function(rel_val){
	//This standalone function allows another modal to be closed and this to be opened with a single click in another modal.
	// Display an external page using an iframe
	//var src = "modal_tour_a.html?program="+rel_val;
	//alert('ABC:'+rel_val);
	var src = "/modal_tools_sample_plan?program="+rel_val;
	var thisWidth=920;
	var thisHeight=740;
	var window_top = getModalTop(thisHeight);
	if(top.parent.frames['carousel_iframe'])
		top.parent.frames['carousel_iframe'].location=src;
	$.modal('<iframe src="' + src + '" width="'+thisWidth+'" height="'+thisHeight+'" frameborder="0" allowtransparency="true" style="border:0" name="modal_tools_wide" id="modal_tools_wide">', {
		closeHTML:"",
		opacity:70,
		overlayCss: {backgroundColor:"#c2c2c2"},
		position: [window_top+'px',],
		containerCss:{
			width:thisWidth,
			height:thisHeight,
			padding:0
		},
		dataCss:{
			width:thisWidth,
			height:thisHeight,
			padding:0
		},
		minHeight:thisHeight,
		overlayClose:true,
		onClose: function (dialog) {
			dialog.data.fadeOut('fast', function () {
				dialog.container.hide('fast', function () {
					dialog.overlay.fadeOut('fast', function () {
						$.modal.close();
					});
				});
			});
		}

	});
	return false;
}

$(document).ready(function() {
	
	var select_slide_show_with_animation = function(button){
		var slidenum = $(button).attr('rel');
		if(is_ie_6()){
			$('#content_upper_photo1').css('background-image', 'url(media/images/photos/photo'+slidenum+'.jpg)');		
			$('#content_upper_photo2').hide();
			change_selected_slide_show_button();
			$('.content_upper_text').fadeOut(500, function() {
			});
			setTimeout("$('#content_upper_text_"+slidenum+"').fadeIn('slow', function() {});",500);
		}else{
			change_selected_slide_show_button(button);
			//Fade in new image (two layers, #1 on top)
			//Step 1. Update layer #2 with new image
			//Step 2. Fade out layer #1
			//Step 3. When fade complete, make layer #1 visible again
			$('.content_upper_text').fadeOut(500, function() {
			});
			setTimeout("$('#content_upper_text_"+slidenum+"').fadeIn('slow', function() {});",500);
	
			$('#content_upper_photo2').attr('class', 'photo'+slidenum);
			$('#content_upper_photo1').fadeOut('slow', function() {
				$('#content_upper_photo1').attr('class', 'photo'+slidenum);
				$('#content_upper_photo1').show();
	  		});
		} 
	};
	
	var start_random_slide_show = function(){
		last_selected_slide = 0;
		var button = get_slide_show_button_by_index(last_selected_slide);
		var slidenum = $(button).attr('rel');
		if(is_ie_6()){
			
		}else{
			$('#content_upper_photo2').attr('class', 'photo'+slidenum);
			$('#content_upper_photo1').attr('class', 'photo'+slidenum);
			$('#content_upper_photo1').show();
			$('#content_upper_text_'+slidenum).show();
			change_selected_slide_show_button(button);
		} 
	};
	
	var change_selected_slide_show_button = function(button){
		if(is_ie_6()){
			$('.slideshow_button').css('background-image','url(media/images/slideshow_off.gif)');
			$('#slideshow_'+slidenum).css('background-image','url(media/images/slideshow_on.gif)');
		}else{
			$('.slideshow_on').addClass('slideshow_off');
			$('.slideshow_on').removeClass('slideshow_on');
			$(button).addClass('slideshow_on');
		}
	};

	var get_slide_show_button_by_index = function(index){
		return $('.slideshow_button')[2 - index]; //we index the array in reverse to get the correct look on screen
	};
	
	var is_ie_6 = function(){
		return $.browser.msie && parseFloat($.browser.version) < 7;
	};
	
	var select_next_slide_show_button= function(){
		var next_slide = (last_selected_slide + 1) % 3;
		last_selected_slide = next_slide;
		return get_slide_show_button_by_index(next_slide);
	};
	
	var random_slide_show_interval = setInterval(function(){
		select_slide_show_with_animation(select_next_slide_show_button());
	},10000);
	
	var last_selected_slide = 0;
	
	$('.slideshow_button').click(function() {
		clearInterval(random_slide_show_interval);
		select_slide_show_with_animation(this);
	});
	
	start_random_slide_show();
	
	
	$('.modal_tools_wide').click(function() {
		rel_val = $(this).attr('rel');
		launchToolsWide(rel_val);
	});
	$('.modal_tour').click(function() {
		var src ="/modal_tour_1?program="+ $(this).attr('rel');
		launchTour(src);
	});
	$('.modal_tools').click(function() {
		rel_val = $(this).attr('rel');
		launchTools(rel_val);
		if ($(this).attr('changelet') != 'changelet') {
			$(this).removeClass('gray');
			$(this).addClass('babyblue');
			$(this).parent().siblings().children().removeClass('white');
			$(this).parent().siblings().children().addClass('babyblue');
		}
	});
	
	$('.modal_habitdetector').click(function() {
		//alert($(this).attr('rel'));
		//Read REL attribute from div that was clicked - then pass to modal iframe URL.
		var rel_val = $(this).attr('rel');
		// Display an external page using an iframe
		var src = "/modal_habitdetector_1?habit="+rel_val;
		//var src = rel_val;
		var thisWidth=843;
		var thisHeight=472;
		var window_top = getModalTop(thisHeight);
		$.modal('<iframe src="' + src + '" width="'+thisWidth+'" height="'+thisHeight+'" frameborder="0" allowtransparency="true" style="border:0" id="habitdetector" name="habitdetector">', {
			closeHTML:"",
			opacity:70,
			overlayCss: {backgroundColor:"#c2c2c2"},
			position: [window_top+'px',],
			containerCss:{
				width:thisWidth,
				height:thisHeight,
				padding:0
			},
			minHeight:thisHeight,
			overlayClose:true,
			onClose: function (dialog) {
				dialog.data.fadeOut('fast', function () {
					dialog.container.hide('fast', function () {
						dialog.overlay.fadeOut('fast', function () {
							$.modal.close();
						});
					});
				});
			}

		});
			$('#content_hdbox_dropdown').fadeOut('fast', function() {});

		return false;
	});
	$('.modal_feedback').click(function() {
		//alert($(this).attr('rel'));
		//Read REL attribute from div that was clicked - then pass to modal iframe URL.
		var rel_val = $(this).attr('rel');
		// Display an external page using an iframe
		var src = "/feedback";
		//var src = rel_val;
		var thisWidth=522;
		var thisHeight=633;
		var window_top = getModalTop(thisHeight);
		$.modal('<iframe src="' + src + '" width="'+thisWidth+'" height="'+thisHeight+'" frameborder="0" allowtransparency="true" style="border:0" id="modal_feedback" name="modal_feedback">', {
			closeHTML:"",
			opacity:70,
			overlayCss: {backgroundColor:"#c2c2c2"},
			position: [window_top+'px',],
			containerCss:{
				width:thisWidth,
				height:thisHeight,
				padding:0
			},
			minHeight:thisHeight,
			overlayClose:true,
			onClose: function (dialog) {
				dialog.data.fadeOut('fast', function () {
					dialog.container.hide('fast', function () {
						dialog.overlay.fadeOut('fast', function () {
							$.modal.close();
						});
					});
				});
			}

		});
		return false;
	});
	// BEGIN: added by Magpie 8-17-10
	$('.modal_passwordrecovery').click(function() {
		var rel_val = $(this).attr('rel');
		var src = "sendpasswordrecovery";
		var thisWidth=522;
		var thisHeight=633;
		launchPopup(src, thisWidth, thisHeight, "modal_passwordrecovery");		
	});
	// END: added by Magpie 8-17-10
	// BEGIN: added by Magpie 8-30-10
	$('.modal_whytext').click(function() {
		var rel_val = $(this).attr('rel');
		var src = "/whytext";
		var thisWidth=522;
		var thisHeight=633;
		launchPopup(src, thisWidth, thisHeight, "modal_whytext");		
	});
	// END: added by Magpie 8-30-10

	

	setUpProgramDetailModal();
});
