var prev_active = false ;
var fadeVal=0;

function hidediv(id)
{
	if (document.getElementById)
	{
		try{document.getElementById(id).style.display = 'none';}
		catch(e){ return false;	/*alert(e.description);*/ }
	}
	else
	{
		if(document.layers){ try{document.id.display = 'none';} catch(e){return false;} }
		else{ try{document.all.id.style.display = 'none';} catch(e){return false;}}
	}
}

function showdiv(id)
{
	if (document.getElementById)
	{
		try{document.getElementById(id).style.display = 'block';} catch(e){ return false; }
	}
	else
	{
		if(document.layers){ try{document.id.display = 'block';} catch(e){return false;} }
		else{ try{document.all.id.style.display = 'block';} catch(e){return false;}}
	}
}

function showhidediv(id)
{
	if (document.getElementById)
	{
		if(document.getElementById(id).style.display == 'block')
		{
			document.getElementById(id).style.display = 'none';
		}
		else if(document.getElementById(id).style.display == 'none')
		{
			document.getElementById(id).style.display = 'block';
		}
	}
	else
	{
		if (document.layers)
		{
			if(document.id.display == 'block')
			{
				document.id.display = 'none';
			}
			else if(document.id.display == 'none')
			{
				document.id.display = 'block';
			}
		}
		else
		{
			if(document.all.id.style.display == 'block')
			{
				document.all.id.style.display = 'none';
			}
			else if(document.all.id.style.display == 'none')
			{
				document.all.id.style.display = 'block';
			}
		}
	}
}

function slide_advert(){ window.setTimeout(function() {showdiv("wall_advert")},3500); }

function roundNumber(num, dec) {
	var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
	return result;
}

function resPrint()
{
	var scrWidth = screen.width ;
	var scrHeight = screen.height ;	
	var scrRatio = roundNumber(scrWidth / scrHeight,2) ;
	
	var resPrint = "<span id='respri';>Your display features</span><span>Aspect Ratio: <strong>";
	if(scrRatio == 1.25 || scrRatio == 1.33)
	{
		resPrint += "Standard";
	}			
	else if(scrRatio == 1.6 || scrRatio == 1.67)
	{
		resPrint += "Wide";
	}
	else if(scrRatio == 1.78)
	{
		resPrint += "HD";
	}
	else if(scrRatio == 1.76)
	{
		resPrint += "Mobile PSP";
	}
	else if(scrRatio == 0.75 || (scrRatio == 1.33 && scrWidth <= 640))
	{
		resPrint += "Mobile VGA";
	}
	else if(scrRatio == 0.6 || (scrRatio == 1.67 && scrWidth <= 800))
	{
		resPrint += "Mobile WVGA";
	}
	else if(scrRatio == 0.67 || (scrRatio == 1.5 && scrWidth == 480))
	{
		resPrint += "Mobile iPhone";
	}		
	else if(scrRatio == 0.8)
	{
		resPrint += "Mobile Phone";
	}			
	else
	{
		resPrint = "Unknown";
	}
	resPrint += "</strong></span><span>Resolution: <strong>"+scrWidth+" x "+scrHeight+"</strong></span>";
	document.write(resPrint);
}

function linkHighlight()
{
	var scrWidth = screen.width ;
	var scrHeight = screen.height ;
	if (document.getElementById)
	{
		dwlinks = document.getElementById("wallpaper-resolutions");
		dwlinks = dwlinks.getElementsByTagName("a");
		for (i=0; i<dwlinks.length; i++) 
		{
			if(dwlinks[i].href.search('/download/')>0 && dwlinks[i].href.search(scrWidth+'x'+scrHeight)>0)
			{
			dwlinks[i].style.backgroundColor="#BBBBBB";
			dwlinks[i].style.MozBorderRadius="6px";
			dwlinks[i].style.webkitBorderRadius="6px";
			dwlinks[i].style.paddingLeft="5px";
			dwlinks[i].style.paddingRight="5px";
			}
		}		
	}
}

function prevframe_show(wurl)
{
	if (document.getElementById)
	{
		document.getElementById("notifyFrame").src = wurl;
	}
	else
	{
		if (document.layers)
		{
			document.notifyFrame.src = wurl;
		}
		else
		{
			document.all.notifyFrame.src = wurl;
		}
	}
	prev_active = true ;
	frameLoad();
	showdiv("notifyBox2");
	showdiv("notifyBox3");
}

function prevframe_close()
{
	if (document.getElementById)
	{
		document.getElementById("notifyFrame").src = "";
	}
	else
	{
		if (document.layers)
		{
			document.notifyFrame.src = "";
		}
		else
		{
			document.all.notifyFrame.src = "";
		}
	}
	prev_active = false ;
	hidediv("notifyBox2");
	hidediv("notifyBox3");
}

function winHeight(frameHeight) {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }

  var margingHeight = Math.round((myHeight-frameHeight)/2);
  if (margingHeight<0) { margingHeight = 0 ;}
	if (document.getElementById)
	{
		document.getElementById('notifyBox3').style.marginTop = margingHeight+"px";
	}
	else
	{
		if (document.layers)
		{
			document.notifyBox3.style.marginTop = margingHeight+"px";
		}
		else
		{
			document.all.notifyBox3.style.marginTop = margingHeight+"px";
		}
	}
	frameLoaded();
}

function frameHeight()
{
	if (prev_active)
	{
		if (document.getElementById)
		{
			//document.getElementById('notifyFrame').height=20;
			var the_height=document.getElementById('notifyFrame').contentWindow.document.body.scrollHeight;
			document.getElementById('notifyFrame').height= the_height;
		}
		else
		{
			if (document.layers)
			{
				//document.notifyFrame.height=20;
				var the_height=document.notifyFrame.contentWindow.document.body.scrollHeight;
				document.notifyFrame.height= the_height;
			}
			else
			{
				//document.all.notifyFrame.height=20;
				var the_height=document.all.notifyFrame.contentWindow.document.body.scrollHeight;
				document.all.notifyFrame.height= the_height;
			}
		}
		winHeight(the_height);
	}
}

function frameLoad()
{
	if (document.getElementById)
	{
		document.getElementById('notifyBox3').style.opacity = 0;
		document.getElementById('notifyBox3').style.filter = "alpha(opacity=0)";
	}
	else
	{
		if (document.layers)
		{
			document.notifyBox3.style.opacity = 0;
			document.notifyBox3.style.filter = "alpha(opacity=0)";
		}
		else
		{
			document.all.notifyBox3.style.opacity = 0;
			document.all.notifyBox3.style.filter = "alpha(opacity=0)";
		}
	}
	showdiv("notifyBoxLoad");
}

function frameLoaded()
{
	hidediv("notifyBoxLoad");
	
	fadeVal=0;
	frameFadeIn();	
}

function frameFadeIn()
{
	fadeVal+=100;
	if (document.getElementById)
	{
		document.getElementById('notifyBox3').style.opacity = fadeVal/100;
		document.getElementById('notifyBox3').style.filter = "alpha(opacity="+fadeVal+")";
	}
	else
	{
		if (document.layers)
		{
			document.notifyBox3.style.opacity = fadeVal/100;
			document.notifyBox3.style.filter = "alpha(opacity="+fadeVal+")";
		}
		else
		{
			document.all.notifyBox3.style.opacity = fadeVal/100;
			document.all.notifyBox3.style.filter = "alpha(opacity="+fadeVal+")";
		}
	}
	if(fadeVal < 100) setTimeout("frameFadeIn()",50);
}

// preview functions begin

function showres(id)
{

	hidediv('dlw_block_standard');
	hidediv('dlw_block_wide');
	hidediv('dlw_block_hd');
	hidediv('dlw_block_mobile');
	hidediv('dlw_block_dual');
	showdiv(id);
	res_sel_highest(id);
}

function res_sel_highest(id)
{
	try{ document.getElementById(id).selectedIndex = document.getElementById(id).options.length-1; }
	catch(e){ return false; }
}

function selectOptionByValue(selObj, val)
{
	try
	{
		var sel= selObj.options;
		var len = sel.length;
		for (var x = 0; x < len; x++)
		{
			if (sel[x].value==val) { selObj.selectedIndex=x; return true; }
		}
		return false;
	}
	catch(e){return false;}
}

function res_download()
{
	try
	{ 
	var ratio = document.getElementById('dw_ratio').value ;
	var resol = document.getElementById('dlw_block_'+ratio).value ;
	var dwpath = "http://wallpaperswide.com/" + document.getElementById('dw_url').value+resol+".html";
	document.getElementById('dw_form').action = dwpath;
	document.getElementById('dw_form').submit();
	}
	catch(e){ return false; }
}

function res_select()
{
	var scrWidth = screen.width ;
	var scrHeight = screen.height ;
	var scrRatioInt = roundNumber(scrWidth / scrHeight,2);
	var scrRatio = "other" ;

	if(scrRatioInt == 1.25 || scrRatioInt == 1.33)
	{
		scrRatio = "standard";
	}			
	else if(scrRatioInt == 1.6 || scrRatioInt == 1.67)
	{
		scrRatio = "wide";
	}
	else if(scrRatioInt == 1.78)
	{
		scrRatio = "hd";
	}
	else if(scrRatioInt == 1.76)
	{
		scrRatio = "mobile";
	}
	else if(scrRatioInt == 0.75 || (scrRatioInt == 1.33 && scrWidth <= 640))
	{
		scrRatio = "mobile";
	}
	else if(scrRatioInt == 0.6 || (scrRatioInt == 1.67 && scrWidth <= 800))
	{
		scrRatio = "mobile";
	}
	else if(scrRatioInt == 0.67 || (scrRatioInt == 1.5 && scrWidth == 480))
	{
		scrRatio = "mobile";
	}		
	else if(scrRatioInt == 0.8)
	{
		scrRatio = "mobile";
	}
	
	if(selectOptionByValue(document.getElementById('dw_ratio'), scrRatio))
	{
		showres('dlw_block_'+scrRatio);
		selectOptionByValue(document.getElementById('dlw_block_'+scrRatio), scrWidth+'x'+scrHeight);
	}
	else
	{
		document.getElementById('dw_ratio').options.selectedIndex = 0;
		scrRatio = document.getElementById('dw_ratio').options[0].value;
		showres('dlw_block_'+scrRatio);
	}
}

// preview functions end
