$(document).ready(function(){
	
	// Fix IE
	//$(document).pngFix(); 

	// Preload all rollovers
	
	$("#nav img").each(function(){
		// Set the original src
		rollsrc = $(this).attr("src");
		rollON = rollsrc.replace(/.gif$/ig,"_on.gif");
		$("<img>").attr("src", rollON);
	});
	
	// Navigation rollovers
	$("#nav a").mouseover(function(){
		imgsrc = $(this).children("img").attr("src");
		matches = imgsrc.match(/_on/);
		// don't do the rollover if state is already ON
		if (!matches) {
		imgsrcON = imgsrc.replace(/.gif$/ig,"_on.gif"); // strip off extension
		$(this).children("img").attr("src", imgsrcON);
		}
		
	});
	$("#nav a").mouseout(function(){
		$(this).children("img").attr("src", imgsrc);
	});
	
	
	// Tab Navigation
	$("#tabs a").click(function(e) {
		e.preventDefault();
		// hide the old content
		$("#tab_content div.content").each(function(){ $(this).hide(); });
		// deselect all tabs
		$("#tabs img").each(function(){
			new_src = $(this).attr("src").replace(/_on.gif$/ig,".gif");
			$(this).attr("src", new_src);
		});
		// swap the image
		img = $(this).children("img").eq(0);
		new_src = img.attr("src").replace(/.gif$/ig,"_on.gif");
		img.attr("src", new_src);
		// show the new content
		div = $(this).attr("href");
		$(div).show();
	});
	// Show the first tab
	$("#tabs a:first").trigger("click");
	
	// Warn users about external links
	$("ol.references a").click(function(){
		alert('You are now leaving the mederma.com website.');
	});
	
	// More detailed warning
	$("#store_list a").click(function(){
		alert('Note: You are leaving Mederma.com. Merz Pharmaceuticals, LLC does not have control over this web site and does not make any representations or warranties. Merz has received no compensation for providing this link.');
	});
	
	$("a.external_link").click(function(){
		alert('Note: You are leaving Mederma.com. Merz Pharmaceuticals, LLC does not have control over this web site and does not make any representations or warranties. Merz has received no compensation for providing this link.');
	});
	
});

function show_video(the_vid) {
	
	$('#video_container').show();
	$('#video_container').append('<div id="video_close"><a href="javascript:hide_video();"><img src="/images/closebox.png" /></a></div><div id="video"></div>');
	var params = { 	
		autoplay: "true",
		allowscriptaccess: "always", 
		allowfullscreen: "true",
		flashvars: "file=/swf/" + the_vid + ".flv"
	}
	swfobject.embedSWF("/swf/player.swf", "video", "328", "200", "8", true, false, params);
}

function hide_video() {
	$('#video_container').empty().hide();
}

function show_video_story(the_vid, vid) {
	$('#video' + vid).empty().hide();	
	$('#video' + vid).show();
	$('#video' + vid).css("float","none");
	$('#video' + vid).append('<p><div id="video_story" style="float: left;"></div><div style="float: left;"><a href="javascript:hide_video_story(\'' + the_vid + '\', ' + vid + ');"><img src="/images/closebox.png" /></a></div></p>');
	var params = { 	
		autoplay: "true",
		allowscriptaccess: "always", 
		allowfullscreen: "true",
		flashvars: "file=/Videos/" + the_vid + ".flv"
	}
	swfobject.embedSWF("/swf/player.swf", "video_story", "288", "160", "8", true, false, params);
}

function hide_video_story(the_vid, vid) {
	$('#video' + vid).empty().hide();
	$('#video' + vid).show();
	$('#video' + vid).css("float","left");
	$('#video' + vid).append('<p style="width:120px;"><a href="" onclick="show_video_story(\'' + the_vid + '\', ' + vid + '); return false;"><img src="/Videos/' + the_vid + '11.jpg" width="97" style="padding:0;"></a><br /><br /><br /><br /><img src="/images/play_video.jpg" border="0" style="padding:0;"></p>');
}

function show_scars(sid, before, after) {
	$('#scars' + sid).empty();
	$('#scars' + sid).show();
	$('#scars' + sid).append('<p style="height:150px;"><a href="" onclick="hide_scars('+ sid +' , \''+ before +'\', \''+ after +'\'); return false;"><img src="/images/scars/large_' + before + '" style="padding:0;"><img src="images/scars/large_' + after + '" style="padding:0;"></a></p>');
}

function hide_scars(sid,before,after) {
	$('#scars' + sid).empty();
	$('#scars' + sid).show();
	$('#scars' + sid).append('<p style="width:120px; height:100px;"><a href="" onclick="show_scars('+ sid +' , \''+ before +'\', \''+ after +'\'); return false;"><img src="/images/scars/' + before + '" style="padding:0;"><img src="images/scars/' + after + '" style="padding:0;"><br /><br /><br /><br /><img src="/images/scars_beforeafter.gif" border="0" style="padding:0;"></a></p>');
}

function submitForm() {
	var checkVideo = true;
	var sAlert;
	
	if (document.forms['stories_form'].story.value == '') {
		checkVideo = false;
		sAlert = 'Your story is a required field.\n';
	}
	if (document.forms['stories_form'].afterfile.value != '') {
		if (document.forms['stories_form'].after1_weeks.value == '') {
			checkVideo = false;
			sAlert += 'You must select the duration of your After 1 photo.\n';
			document.forms['stories_form'].after1_error.value = 'true';
		}
	}
	if (document.forms['stories_form'].afterfile2.value != '') {
		if (document.forms['stories_form'].after2_weeks.value == '') {
			checkVideo = false;
			sAlert += 'You must select the duration of your After 2 photo.\n';
			document.forms['stories_form'].after2_error.value = 'true';
		}
	}
	if (!document.forms['stories_form'].agree.checked) {
		checkVideo = false;
		sAlert += 'You must agree to the Terms & Conditions.\n';
	}
	
	if (!checkVideo) {
		//alert(sAlert);
		document.forms['stories_form'].ShareAPhoto.value = '';
		document.forms['stories_form'].Video.value = '';
		
		document.forms['stories_form'].beforefile.value = '';
		document.forms['stories_form'].afterfile.value = '';
		document.forms['stories_form'].afterfile2.value = '';
	}
	
	document.forms['stories_form'].submit();
}