
$(document).ready(function(){
videoTranscriptButton();
showhideTopics();
expandablePanels();
navHover();
surveyWidget();
surveyResults();
hoverFunctions();
languageSelect();
tabbedTables();
//setupHCPOverlay();
videoSelect();

if($('a.editorGlossLink') != 0){
	$('a.editorGlossLink').each(function(){
		var word = $(this).html().toLowerCase().split(' ').join('_');
		var newHref = $(this).attr('href')+"#"+word;
		$(this).attr('href',newHref);
	});
}

});


function tabbedTables(){
	$('.tabbedTable').tabs();	
};
function videoTranscriptButton(){
$('span.transcriptLink a').click(function(){
     $('div.transcript').css({"right":"5px"});
     return false;
});
$('div.transcript a').click(function(){
     $('div.transcript').css({"right":"999em"});
     return false;
});
};

//Comparison Widget
function showhideTopics(){

	$(".topic").each(function(){
		if($(this).is(".open")){}else{
			$(this).addClass("closed");	
		}
	})
		
	$('div.type .showhideHeader').click(function(){
		dcsMultiTrack('DCS.dcsuri', '/en/public/contraceptive_choices/your%20contraceptive%20choices.nopage', 'DCS.dcssip', 'www. mirena.com', 'WT.ti', 'Button: Your%20contraceptive%20choices%20table','WT.cusAccordion',	
'Your%20contraceptive%20choices','WT.cusAccordionStatus','1','WT.cusEngage','Engagement');
		$(this).parents('div.type').next().slideToggle('slow');	
		if($(this).parents('div.type').is(".open")){
			$(this).parents('div.type').removeClass("open");
			$(this).parents('div.type').addClass("close");	
		}
		else{
			$(this).parents('div.type').removeClass("close");
			$(this).parents('div.type').addClass("open");
		}			
	});
	$(".comparisonWidget .moreInformation .compContent").hover(
  		function () {$(this).css("background-color","#ededed")
  		},
  		function () {$(this).css("background-color","#ffffff")	
	});	
}

//ExpandablePanels
function expandablePanels(){
	
	$(".revealContent").each(function(){
		if($(this).is(".open")){}else{
			$(this).addClass("close");	
		}
	})
	$('.revealHeader').click(function(){
		$(this).siblings(".revealContent").slideToggle("fast");
		if($(this).is(".open")){
			$(this).removeClass("open");
			$(this).addClass("closed");	
		}
		else{	
			$(this).removeClass("closed");
			$(this).addClass("open");
		}	
	});
	
	$(".panelContent").each(function(){
		if($(this).is(".open")){}else{
			$(this).addClass("close");	
		}
	})
	$('.expandablePanel .panelHeader').click(function(){
		$(this).siblings(".panelContent").slideToggle("slow");
		if($(this).is(".open")){
			$(this).removeClass("open");
			$(this).addClass("closed");	
		}
		else{	
			$(this).removeClass("closed");
			$(this).addClass("open");
		}	
	});
	$('.refsPanel .refsHeader').addClass('closed');
	$('.refsPanel ol').hide();
	$('.refsPanel .refsHeader').click(function(){
		if($(this).is(".open")){
			$(this).removeClass("open").addClass("closed").next("ol").slideUp();	
		}
		else{	
$(this).removeClass("closed").addClass("open").next("ol").slideDown();	
		}	
	});
	
	$('a.reference').click(function(){
			$('.refsPanel .refsHeader').addClass("open").next("ol").slideDown();
	});
}

//Nav Hover

function navHover(){
     $('li.hoverNav').hover(function(){
          $(this).children('.topNavImage').show();
          $(this).children('.topNavTwoCol').show();

     },function(){
          $(this).children('.topNavImage').hide();
          $(this).children('.topNavTwoCol').hide();
     });
}

//Language Select

function languageSelect(){
	$('.languageSelect').addClass('close');
	$('.languageSelect').click(function(){
		$(this).children('ul').slideToggle();
	});
}

//Hover functions

function hoverFunctions(){
    hoverToggle('.larcOption','no');
    hoverToggle('li.region','no');
    hoverToggle('li.hoverNav','yes');
    hoverToggle('.topNavImage li','yes');
    hoverToggle('.topNavTwoCol li','yes');
    hoverToggle('#searchResults li.searchListing','yes');
}

function hoverToggle(selector,remain){
	$(selector).hover(function(){
		$(selector).removeClass('selected');
		$(this).addClass('selected');
	},function(){
		if(remain == 'yes'){
			$(selector).removeClass('selected');
		}	
    });
}

//Survey Widget

function surveyWidget(){

	$("div.survey").show();
	if($("div.surveyPanel").length > 0 ){
		jQuery('#mycarousel').jcarousel({
			scroll: 1,
			itemVisibleInCallback: {
  				onAfterAnimation: showSubmit
			}
    	});
    	replaceRadioButtons();
	}	
	submitSurvey()
}

function showSubmit(jcarousel_object, li_object, index, state){

	if(index==$('#mycarousel li').length){
		$('.jcarousel-next-horizontal').hide();
		$('#surveySubmit').show();
	}else{
		$('.jcarousel-next-horizontal').show();
		$('#surveySubmit').hide();
	}
}

function replaceRadioButtons() {
	jQuery("input.radio").each(function(index){
		$(this).attr('name','result'+Math.floor(index/2)).hide();
		var l_radio = '<div class="RadioReplace"><!-- open --></div>';
		$(this).before(l_radio);
		$("div.RadioReplace").click(function() {
			$(this).siblings("input.radio").get(0).checked = true;
			$(this).parent().siblings().children("div.RadioReplace").removeClass("Selected");
			$(this).addClass("Selected");
		});
		
		$("label.answer").click(function() {
			$(this).siblings("input.radio").get(0).checked = true;
			$(this).parent().siblings().children("div.RadioReplace").removeClass("Selected");
			$(this).siblings("div.RadioReplace").addClass("Selected");
		});
	});
}

function submitSurvey(){

	$('#surveySubmit').click(function(){
		
		if($('input:radio.radio:checked').length == $('input:radio.radio').length/2){
    		return true;  		
		}
		else{
			$("span.error").text('Please complete all questions');
			return false;
		}					
	});
	
	$('.RadioReplace').click(function(){
		
		if($('input:radio.radio:checked').length == $('input:radio.radio').length/2){
    		$('.jcarousel-next-horizontal').hide();
			$('#surveySubmit').show();
		}	
	});
}


//SurveyResults

function surveyResults(){
	
	if($('.surveyResults').length != 0){
		$(".result").each(function(index){
			$(this).addClass('result'+index);
		});		
		var allVars = $.getUrlVars();
		
		$.each(allVars, function(key, value) { 	 
  			 var answer = $.getUrlVars()[value];
  			 
  			 if(answer=='no'){	 	
  			 	$('.'+value+' .answerYes').hide();	
  			 	if($('.'+value+' .answerNo').text() == ""){
  			 		$('.'+value).hide();
  			 	} 	
  			 }
  			 else{ 
  			 	$('.'+value+' .answerNo').hide();
  			 } 
		});
	}	
}
/*
function setupHCPOverlay(){

	$('.hcpSite .bayerLogo').click(function(){
		$('#wrapper').prepend('<div id="overlay"><div id="mask"></div><div id="viewWindow"><div class="viewWrapper"><div class="viewContent"><div class="panelHeaderGrey"><h2>For Healthcare Professionals only</h2></div><p>To access the professional site for information on the Mirena intrauterine system please confirm that you are a healthcare professional.</p><p><em class="editor">You will only be prompted once per visit to the website</em></p><a class="button128" href="long-acting-contraceptives.html">Go to public site</a><a class="button87" href="#">Confirm</a></div></div><div class="panelBase"><!-- base --></div></div></div>')
		return false;
	});	
	
	$('#overlay .button87').live('click',function(){
		$('#overlay').hide();
		return false;
	});
}
*/
function videoSelect(){
	
	$('ul.vidQuestions li:eq(0)').addClass('selected');
	videoPlace('ul.vidQuestions li:eq(0)',false);

	$('ul.vidQuestions li').click(function(){
		$('ul.vidQuestions li').removeClass('selected')
		$(this).addClass('selected');
		var thisLi = $(this);
		videoPlace(thisLi,true);
	});
	
	$('ul.vidQuestions li').hover(function(){
		$(this).addClass('hover');
	},function(){
		$(this).removeClass('hover');
	});
}

function videoPlace(el,playvar){

	vidURL=$(el).children(".vidURL").text();
	vidID=$(el).children(".vidID").text();
	
	vidURL  = $.trim(vidURL);
	vidID	= $.trim(vidID);
	
	 var params = { allowScriptAccess: "sameDomain",  movie: "/html/flash/MirenaVideplayer_V3_458.swf", 
		wmode: "window", quality: "high", bgcolor: "#ffffff", play: "true", loop: "true" , scale: "showall", wmode: "opaque",
		menu: "true", devicefont: value= "false", FlashVars: "videopath="+vidURL+"&startimage=/html/images/diana-vid-V2_410.jpg&autoplay="+playvar+""};
	
	$('.vidPlacement').empty().append("<div>video loading</div>");
	$('.vidPlacement').children().attr("id",vidID);
	swfobject.embedSWF("/html/flash/MirenaVideplayer_V3_458.swf", vidID, "453", "278", "8", null, null, params); 

}

$.extend({
  getUrlVars: function(){
    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];
  }
});
	


