window.onresize=align; //align the window after resizing
jQuery.noConflict();
var $j = jQuery;

var goodieType;
var selectedImagePath;
var finalUrl;
var showDDMenu = true; // Show only one drop down at a time

//preloades images
function mmPreloadImages() { //v3.0
	var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
	var i,j=d.MM_p.length,a=mmPreloadImages.arguments; for(i=0; i<a.length; i++)
	if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

//calls dcsMultiTrack for web tracking
function goodieDownload() { 
	dcsMultiTrack("DCS.dcsuri","/goodie_download.html", "WT.ti","goodie download : "+goodieType+"", "WT.pi", "goodie download : "+	goodieType+"", "WT.cg_n", "goodies", "WT.cg_s", "downloads", "DCSext.w_locale", "en_AU");
}

function shareThis(){ 
	dcsMultiTrack("DCS.dcsuri","/goodie_share.html", "WT.ti","Goodie Share : "+goodieType+" : "+shareValue+" ", "WT.pi","Goodie Share : "+goodieType+" : "+shareValue+" ", "WT.cg_n", "goodies", "WT.cg_s", "shares", "DCSext.w_locale", "en_AU");
}


$j(function(){
	$j().not(".btn_holder_bg").pngFix(); // png fix jquery plugin for IE 6
	align();                  //sub_events js
	positionArrow();	      //sub_events js
	//requestInit();
	$j('.displayNone:first').show();
	
	/* Check on http://keith-wood.name/bookmark.html for more detail about bookmark plugin and configuration*/
	$j('.share_this').bookmark({sites: bookmarks_dropdown});
	if($j("div").hasClass("dgv_format") || $j("div").hasClass("dgv_format_scr") ){	
		$j('.dgv_format_container_share div').bind("click",function(event){
		event.stopPropagation();
		$j('.bookmark_list li a').click(function(){
			shareValue= $j(this).text();
			shareThis();
		})
		if($j('.popup_container').css('display') != 'block') {
			if($j('.bookmark_list a').hasClass('btn_anchor')){
				$j('.bookmark_list a').removeClass('btn_anchor')
			}
			if ($j('.share_this').is(':hidden')) {
				$j('.share_this').show();
				$j('.dgv_format_options').hide();
			}
		}
		});
		$j().click(function(){
			$j('.share_this').hide();
			$j('.dgv_format_options').hide();
		})
	}
})

//Show selected flavor preview image on click of thumbnail image
function showFlavorContainer(selectedTitle){
	refreshButton(selectedTitle);
	$j('.dgv_format_disable').removeClass().addClass("dgv_format");
	$j('.displayNone' ).hide();
	$j('#'+selectedTitle).show();
	$j('#'+selectedTitle+' div:first').hide();
}

// Show download popup message
 function showPopupDivMessage(selectedTitle, selectedImgPath) {
	if(goodieType != "wallpaper")
	{
		$j('.btn_anchor').removeClass().addClass('btn_anchor_disable');
		$j('.dgv_format_scr').removeClass().addClass('dgv_format_scr_disable');
	}
	$j('#popup_'+selectedTitle).show();
	selectedImagePath = selectedImgPath;
}
		
// Hide popup on click of continue for wallpapers and screensver
function hidePopupDivMessage(selectedTitle) {
	if(goodieType == "wallpaper"){
		$j('.dgv_format_disable').removeClass().addClass("dgv_format");
		refreshButton(selectedTitle);
	} else{
		$j('.btn_anchor_disable').removeClass().addClass('btn_anchor');
		$j('.dgv_format_scr_disable').removeClass().addClass('dgv_format_scr');
	}
	if ($j('#'+selectedTitle+' div:first').is(':visible')) {
		$j('#'+selectedTitle+' div:first').hide();
		$j('.continue').bind('click',function(){
			mailerServletCall(); //Calls Mailer servlet for download asset
		})
	}
	showDDMenu = true;
}

//Calls Mailer servlet for download asset
function mailerServletCall()
{
	var protocol = String(location.protocol);
	var host = String(location.host);
	//var finalUrl=appurl+'?downloadpath='+protocol+'//'+host+selectedImgPath;
	finalUrl=protocol+'//'+host+selectedImagePath;
	if(goodieType=='other')
	{
	window.open ('index_en_AU.html');
	}else{
	window.open(finalUrl);
	}
	$j('.continue').unbind('click');
}
		
//Common method for stopping bubble/propogation in diff browsers
function stopBubble(e) {
	// If an event object is provided, then this is a non-IE browser
	if ( e && e.stopPropagation )
	{
	// and therefore it supports the W3C stopPropagation() method
	e.stopPropagation();
	}
	else
	// Otherwise, we need to use the Internet Explorer
	// way of cancelling event bubbling
	window.event.cancelBubble = true;
}

//Open the drop down on Click of button
function selectOpenDD(selectedFlavorTitle, selectedGoodie, event){	
	if(showDDMenu != false) {
		stopBubble(event);
		$j('.dgv_format_options').hide();
		var hiddenDiv = $j("#"+"dgv_format_options_"+selectedGoodie+"_"+selectedFlavorTitle);
		if(hiddenDiv.is(':hidden')){
			hiddenDiv.show();
			$j('.share_this').hide();
		}
	}
}

//Select option from drop down and display on the button
function selectDDValue(selectedOptValue, selectedFlavorTitle, selectedImagePath, selectedGoodie){
	showDDMenu = false;
	var selectedFlavorConatiner = "#dgv_format_container_"+selectedGoodie+"_"+selectedFlavorTitle;
	var selectedIndTitle = "#dgv_format_"+selectedGoodie+"_"+selectedFlavorTitle;
	$j(selectedFlavorConatiner+" option")[selectedOptValue].selected = true;
	$j(selectedIndTitle+" div").html($j(selectedFlavorConatiner+" option")[selectedOptValue].innerHTML);
	showPopupDivMessage(selectedFlavorTitle,selectedImagePath);
	$j('.dgv_format').removeClass().addClass("dgv_format_disable");
}

//Refresh dropdown text
function refreshButton(selectedTitle) {
	if(goodieType == "wallpaper"){
		$j('.share_this').hide();
		$j('.dgv_format_options').hide();
		$j('#dgv_format_desktop_'+selectedTitle+' div').html("desktop");
		$j('#dgv_format_mobile_'+selectedTitle+' div').html("mobile");
		$j('#share_this_'+selectedTitle+' div').html("share");
		if(showDDMenu == false) {
		showDDMenu = true;
		}
	}
}
