// JavaScript Document


incomment=false;
insearch=false;
onimage=false;

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_showHideLayers() { //v6.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
		//obj.visibility=v;
		if (v=='visible') {
		   obj.display='block';
		}
		else
		{
		   obj.display='none';
		}
	}
}

function MM_openCloseLayers() { //v6.0
 
  var i,p,v,obj,args=MM_openCloseLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) 
  if ((obj=MM_findObj(args[i]))!=null) { 
    v=args[i+2];

    if (obj.style) {
		if (v=='show') {
			obj.style.display='block';
		}
		else
		{
			obj.style.display='none';
		}
	}
 
  }
}

function changeSrc(imagepos,newimage)
{
   document.getElementById(imagepos).src = newimage;
}

function changeTitle(imagepos,title)
{
   document.getElementById(imagepos).title = title;
   document.getElementById(imagepos).alt = title;
}


function changeInnerHTML(imagepos,newhtml)
{
   document.getElementById(imagepos).innerHTML = newhtml;
}

function changeURL(imagepos,newurl)
{
   document.getElementById(imagepos).parentNode.href = newurl;
  // alert(document.getElementById(imagepos).parentNode.href);
   //alert(newurl);
}


function showmessagebox(message, title)
{
   if (document.getElementById('messagebody')) {
      document.getElementById('messagebody').innerHTML = message;
      document.getElementById('messagetitle').innerHTML = title;
      MM_showHideLayers('messagebox','','show');
   }
   else
   {
	   alert (message);
   }
}

function showcopyright()
{
   message='© M. Ruffe 2009. The content/design of this site may not be reproduced in any form without permission';
   showmessagebox(message, 'Copyright notice');
}

function showcommentapproval()
{
   message='Thankyou for your comment. It is now waiting for approval';
   showmessagebox(message, 'Comment submission');
}

function closecomments ()
{
	MM_showHideLayers('comments','','hide');incomment=false;
}

function closeaddcomment ()
{
MM_showHideLayers('commentform','','hide');MM_showHideLayers('thumbs','','show');MM_showHideLayers('previewnext','','show');MM_showHideLayers('previewaddcomment','','show');MM_showHideLayers('closeimage','','show');MM_showHideLayers('enlargeimage','','show');
}

function addcomment ()
{
MM_showHideLayers('previewnext','','hide');MM_showHideLayers('previewaddcomment','','hide');MM_showHideLayers('thumbs','','hide');MM_showHideLayers('closeimage','','hide');MM_showHideLayers('enlargeimage','','hide');MM_showHideLayers('commentform','','show');
}

function viewcomments ()
{
if (incomment==false) { MM_showHideLayers('comments','','show')} else { MM_showHideLayers('comments','','hide');MM_showHideLayers('previewnext','','show');MM_showHideLayers('previewaddcomment','','show');MM_showHideLayers('thumbs','','show');MM_showHideLayers('closeimage','','show');MM_showHideLayers('enlargeimage','','show');MM_showHideLayers('commentform','','hide');} ; incomment=!incomment;
}

function viewfullsize ()
{
MM_showHideLayers('imagepreview','','hide');MM_showHideLayers('thumbs','','hide');MM_showHideLayers('largeimage','','show');
MM_showHideLayers('paging','','hide');
MM_showHideLayers('thumbgrid','','show');
MM_showHideLayers('nextpage','','hide');
MM_showHideLayers('closeimage','','hide');
MM_showHideLayers('enlargeimage','','hide');
MM_showHideLayers('previewnext','','hide');

}

function closepreview ()
{
MM_showHideLayers('imagepreview','','hide');MM_showHideLayers('previewnext','','hide');MM_showHideLayers('previewaddcomment','','hide');MM_showHideLayers('closeimage','','hide');MM_showHideLayers('enlargeimage','','hide');	
}

function closemainimageview ()
{
MM_showHideLayers('imagepreview','','show');MM_showHideLayers('thumbs','','show');MM_showHideLayers('largeimage','','hide');
MM_showHideLayers('paging','','show');
MM_showHideLayers('thumbgrid','','hide');
MM_showHideLayers('nextpage','','show');
MM_showHideLayers('closeimage','','show');
MM_showHideLayers('enlargeimage','','show');
MM_showHideLayers('previewnext','','show');
}

function closesearchbox ()
{
	//alert('closing search');
	MM_showHideLayers('searchbox','','hide');insearch=false;
}

function closemessage ()
{
	MM_showHideLayers('messagebox','','hide');
}


function showsubsections ()
{
	MM_showHideLayers('subsections1','','show');
	MM_showHideLayers('subsections2','','show');
}



function showabout ()
{
	MM_showHideLayers('smalltextbox','','show');MM_showHideLayers('mrpicdiv','','show');

}

function hideabout ()
{
	MM_showHideLayers('smalltextbox','','hide');MM_showHideLayers('mrpicdiv','','hide');
}

function showsearch ()
{
if (!insearch) {view='show'} else {view='hide'}; insearch=!insearch;MM_showHideLayers('searchbox','',view);
}

function settopmargin()
{
        msie = (/MSIE (\d+\.\d+);/.test(navigator.userAgent)); 

        if (msie) {
           scrh = document.documentElement.clientHeight;
           topmarg = ((scrh - 520)/2); // 214 for toolbars, 544 for website
           }
        else
           {
           scrh = document.documentElement.clientHeight-30;
           topmarg = ((scrh - 520)/2); // 214 for toolbars, 544 for website
           }

	if (topmarg<0) {
	   topmarg = 0;
	}
	ms = '' + topmarg + 'px';
//	alert("setting top margin : " + ms + ", " + scrh + ", " + topmarg);
	document.getElementById('standard').style.paddingTop=ms;
	document.getElementById('standard').style.paddingBottom=ms;
}

function miniImageLoaded()
{
  /* idminidiv=document.getElementById('miniImageDiv');  
   idmini=document.getElementById('miniImage');  
   if ((onimage==true) && (idmini.src.indexOf('whiteblob')<0)) {
  	    idminidiv.style.display = 'block';
   }*/
}

function thumb_mouseout(y,x)
{
/*	idminidiv=document.getElementById('miniImageDiv');  
	idminidiv.style.display = 'none';
    changeSrc('miniImage','/nav_images/whiteblob.gif');*/
	onimage=false;
}

function thumb_mouseover(x,y)
{
   /* idmini=document.getElementById('miniImage');  
	idminidiv=document.getElementById('miniImageDiv');  
   	idminidiv.style.display = 'none';*/
    // get on with the shuffling
	id=document.getElementById('thumbimg_'+x+'_'+y);
	if(id.src.indexOf('imageplacer')>=0){
		yminus1=y-1;
		if(y-1<1){yminus1=5;}
		xminus1=x-1;
		if(x-1<1){xminus1=5;} //10
		yplus1=y-1+2;
		if(yplus1>5){yplus1=1;}
		xplus1=x-1+2;
		if(xplus1>5){xplus1=1;}//10
		options='';
		c=0;
		direction='';
		if(document.getElementById('thumbimg_'+xminus1+'_'+y).src.indexOf('imageplacer')<0){
			options=options+'thumbimg_'+xminus1+'_'+y+',';
 		    direction=direction+'R,';
			c=c+1;
			}
		if(document.getElementById('thumbimg_'+xplus1+'_'+y).src.indexOf('imageplacer')<0){
			options=options+'thumbimg_'+xplus1+'_'+y+',';
 		    direction=direction+'L,';
			c=c+1;
		}
		if(document.getElementById('thumbimg_'+x+'_'+yminus1).src.indexOf('imageplacer')<0){
			options=options+'thumbimg_'+x+'_'+yminus1+',';
 		    direction=direction+'D,';
			c=c+1;
		}
		if(document.getElementById('thumbimg_'+x+'_'+yplus1).src.indexOf('imageplacer')<0){
			options=options+'thumbimg_'+x +'_'+yplus1+',';
 		    direction=direction+'U,';
			c=c+1;
		}
	//	alert (direction + ":" + c);
		if(c>0){
			var r=1+Math.floor(c*Math.random());
	//	alert (r);
	//	alert(options);
			places=options.split(',');
			arrowpos=direction.split(',');
			switchpos=places[r-1];
			arrowtype=arrowpos[r-1];
//alert(arrowtype + ":" + switchpos);
			swid=document.getElementById(switchpos);
			pswid=swid.parentNode;
			pid=id.parentNode;
			
			saver=id.src;
			id.src=swid.src;
//			swid.src=saver;
			swid.src='/portfolio/navimages/imageplacer' + arrowtype + '.gif';
			
			saver=id.alt;
			id.alt=swid.alt;
			swid.alt=saver;

			saver=id.title;
			id.title=swid.title;
			swid.title=saver;

			saver=pid.href;
			pid.href=pswid.href;
			pswid.href=saver;
		}
	}

    // show mini image
	if(document.getElementById('thumbimg_'+x+'_'+y).src.indexOf('imageplacer')<0){
		newsrc = document.getElementById('thumbimg_'+x+'_'+y).src.replace('thumb_4t80_','thumb_b70_70_');
		
		//"thumb_b" + thumbnailwidth + "_" + thumbnailheight + "_" + uploadfilename;
  	    //idminidiv.style.display = 'none';
       // changeSrc('miniImage',newsrc);
		onimage=true;

  	  //  idminidiv.style.display = 'block'; // put back in as onload on image proprietry

    }
	else
	{
		thumb_mouseout(y,x);
	}
	

}

function setOpacity(testObj, value) {
	testObj.style.opacity = value/10;
	testObj.style.filter = 'alpha(opacity=' + value*10 + ')';
}

var fading_in = false;
var opacity = 0;

function fade_in_subsections () {
	
    subsection1 = document.getElementById('subsections1');
    subsection2 = document.getElementById('subsections2');
	
	if (subsection1.visibility=='show') {
		opacity = 10; // already showing
		fading_in = true;
	}
	
	if (fading_in == false) {
	   fading_in = true;
	   setOpacity(subsection1, 0);
 	   setOpacity(subsection2, 0);
       showsubsections ();	

       setTimeout ("fade_in_subsections()", 200); 
	}
	else
	{
	   if (opacity<10) {
           opacity = opacity + 1;  		   
		   setOpacity(subsection1, opacity);
		   setOpacity(subsection2, opacity);
		   setTimeout ("fade_in_subsections()", 200); 
	   }
	}
}

function openmenusection (section) {
    if (section!='') {
       MM_openCloseLayers('menuthumblab_1_0','','hide');
	}
	MM_openCloseLayers('menuthumblab_1_1','','hide');
	MM_openCloseLayers('menuthumblab_1_2','','hide');
	MM_openCloseLayers('menuthumblab_1_3','','hide');
    MM_openCloseLayers('menuthumblab_1_4','','hide');
	MM_openCloseLayers(section,'','show');
}

function openmenusection_jquery (section) {
	// doesn't work perfectly so removed.
    if (section!='') {
       MM_openCloseLayers_jquery('menuthumblab_1_0','','hide');
	}
	MM_openCloseLayers_jquery('menuthumblab_1_1','','hide');
	MM_openCloseLayers_jquery('menuthumblab_1_2','','hide');
	MM_openCloseLayers_jquery('menuthumblab_1_3','','hide');
	if ('menuthumblab_1_4'!=section) {
  	   MM_openCloseLayers_jquery('menuthumblab_1_4','','hide', section);
	}
	else
	{
  	   MM_openCloseLayers_jquery('menuthumblab_1_4','','show');
    }
//	MM_openCloseLayers_jquery(section,'','show');
}


function detectBrowser()
{
   var badBrowser = (/MSIE ((5\.5)|6)/.test(navigator.userAgent) && navigator.platform == "Win32");
   if (badBrowser) {
//	  alert("sigh!");
//	  window.location = "http://bracketsfox.redbubble.com/";
  }
}

detectBrowser(); // to use onload event with behaviour separation

/* jquery */

function MM_openCloseLayers_jquery ()
{
 
  var i,p,v,obj,args=MM_openCloseLayers_jquery.arguments;
  for (i=0; i<(args.length-2); i+=3) 
  //i=0;
  if ((obj=MM_findObj(args[i]))!=null) { 
    //obj is id of item to change
	//v is whether to open or close
    v=args[i+2];
	callbackshow = args[i+3];

    if (obj.style) {
		if (v=='show') {
			//obj.style.display='block';
			$("#"+args[i]).slideDown("slow"); // jquery]
		//	alert("opening" + args[i]);


		}
		else
		{
			//obj.style.display='none';
//			obj.style.display='block'; // make sure turned on
//			$(args[i]).hide();
            if ($("#"+args[i]).is(":hidden")) {
               if (callbackshow) {
				   MM_openCloseLayers_jquery(callbackshow,'','show');
			   }
		    }
			else
			{
  			   if (callbackshow) {
			      $("#"+args[i]).slideUp("slow", 
					MM_openCloseLayers_jquery(callbackshow,'','show')); 
				    
			   }
			   else
			   {
			      $("#"+args[i]).slideUp("slow"); // jquery
			   } 
            }
		}
	}
 
  }
}

function setCookie(c_name,value,expiredays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate()+expiredays);
document.cookie=c_name+ "=" +escape(value)+
((expiredays==null) ? "" : ";expires="+exdate.toGMTString()+";path=/");
}

function getCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1)
    {
    c_start=c_start + c_name.length+1;
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length;
    return unescape(document.cookie.substring(c_start,c_end));
    }
  }
return "";
}

function changebackground(backgroundcolour)
{
	setCookie('backgroundtype',backgroundcolour,200);
	document.location = 'http://www.martinruffe.co.uk/portfolio/pm_cat_Menu/index.aspx?backgroundtype=' +backgroundcolour ;
	
}

function showwhitebackground ()
{
	var background = ''+gup('backgroundtype'); // use to force background type
	if (background != '')
	   {
  	   setCookie('backgroundtype',background,200);
	   }
	
	usewhite = getCookie('backgroundtype')=='white' 
	if (usewhite) {
		document.write('<link title="standard stylesheet" href="http://www.martinruffe.co.uk/portfolio/css/whiteandpurple.css" rel="stylesheet" type="text/css" media="screen,projection" />');
	}
	return usewhite;
}

function gup( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}

function addLoadEvent (func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload=func;
	} else 
	{
		window.onload=function(){
			oldonload();
			func();
		}
	}
}

function setbloglink () {
		currentcolour = getCookie('backgroundtype');
	
        var arr = new Array();
        arr = document.getElementsByTagName("a");
        for(var i = 0; i < arr.length; i++)
        {
            var obj = document.getElementsByTagName("a").item(i);
			if (obj.className == 'bloglink') {
				obj.href = obj.href + '?backgroundtype=' + currentcolour;
			}
									 
        }
}



//================================
// bookmarks

function buildhtml (sn, i, url)
{
root = 	'http://www.martinruffe.co.uk/nav_images/socialnetwork/'; // for site

add = 'Add to: ' + sn;	
i = '<img alt="' + add + '" title="' + add + 
    '" src="' + root + i + '" />';
imageandurl = '<a target="_blank" href="' + url + '">' + i + '</a>';
return imageandurl;
}



function get_del (url, title, text)
{
sn = 'Delicious';
i = 'delicious_16.png';
url = 'http://delicious.com/post?url=' + url + '&title='  + title + '&notes=' + text
;
return buildhtml (sn, i, url);
}

function get_digg (url, title, text)
{
sn = 'Digg';
i='digg_alt_16.png';
url = 'http://digg.com/submit?phase=2&url=' + url + '&title='  + title + '&bodytext=' + text;
return buildhtml (sn, i, url);
}

function get_dzone (url, title, text)
{
sn = 'Dzone';
i='';//no image
url = 'http://www.dzone.com/links/add.html?url=' + url + '&title='  + title;
return buildhtml (sn, i, url);
}

function get_face (url, title, text)
{
sn = 'Facebook';
i='facebook_16.png';
url = 'http://www.facebook.com/share.php?u=' + url + '&t='  + title;
return buildhtml (sn, i, url);
}

function get_my (url, title, text)
{
sn = 'MySpace';
i='myspace_16.png';
url = 'http://www.myspace.com/Modules/PostTo/Pages/?u=' + url + '&t='  + title;
return buildhtml (sn, i, url);
}

function get_stumble (url, title, text)
{
sn = 'StumbleUpon';
i='stumbleupon_16.png';
url = 'http://www.stumbleupon.com/submit?url=' + url + '&title='  + title;
return buildhtml (sn, i, url);
}

function get_tech (url, title, text)
{
sn = 'Technorati';
i='technorati_16.png';
url = 'http://technorati.com/faves?add=' + url;
return buildhtml (sn, i, url);
}

function get_twit (url, title, text)
{
sn = 'Twitter';
i='twitter_16.png';
url = 'http://twitter.com/home?status='  + title + ' - ' + url;
return buildhtml (sn, i, url);
}

function buildstrip (locationid)
{
// naughty use of innerHTML 
locationid = 'social';

url = ''+document.URL;
if (url == '')
   {
	   url = 'http://www.martinruffe.co.uk';
   }
hashpos = url.indexOf("#");   
if (hashpos>0) {
	url = url.substring(0,hashpos);
}

ptitle = ''+document.title;
if (ptitle.indexOf('Martin Ruffe')>0) {
   ptitle = ptitle.replace('Martin Ruffe','');
   ptitle = ptitle.replace('|','-');
   ptitle = ptitle.replace('|','-');
   ptitle = ptitle.replace('|','-');
   ptitle = ptitle.replace('|','-');
   ptitle = ptitle.replace('|','-');
   ptitle = ptitle.replace('- -','-');
   ptitle = ptitle.replace('- -','-');
   ptitle = ptitle.replace('- -','-');
   ptitle = ptitle.replace('- -','-');
   ptitle = ptitle.replace('-  -','-');
   ptitle = ptitle.replace('-  -','-');
   ptitle = ptitle.replace('-  -','-');
   ptitle = ptitle.replace('-  -','-');
   ptitle = ptitle.replace('  ',' ');
   ptitle = ptitle.replace('  ',' ');
   ptitle = ptitle.replace('  ',' ');
}

if (ptitle == '')
   {
	   ptitle = 'Martin Ruffe. Developer, Photographer';
   }
ptext = '';
s='';	
s=s+get_twit (url, ptitle, ptext);
s=s+get_face (url, ptitle, ptext);
s=s+get_del (url, ptitle, ptext);
s=s+get_digg (url, ptitle, ptext);
s=s+get_stumble (url, ptitle, ptext);
s=s+get_my (url, ptitle, ptext);
s=s+get_tech (url, ptitle, ptext);

id = document.getElementById(locationid);
//alert(locationid);
id.innerHTML = s;


}

// ===========================

// onload events and setup - keep at end of file

function setupbloglink ()
{
    addLoadEvent (setbloglink);
	//addLoadEvent (buildstrip ('social'));
}

function onloadmenusection () {
    addLoadEvent (setbloglink);
	addLoadEvent (openmenusection (''));
	//addLoadEvent (buildstrip ('social'));
}

x=setupbloglink ();

// onload events and setup end 

