/////////////////////////////////////////////
////     JAVASCRIPT FUNCTIONS FILE       ////
////   Copyright Biard Productions Inc   ////
/////////////////////////////////////////////

// Site Variables
var bookmark_title = document.title; 
var bookmark_url = 'http://www.smut-house.com';
var script_url = '/cgi-bin/swiftclicks/out.cgi';
var thumb_page_url = 'http://www.smut-house.com/cgi-bin/page_counter/count.cgi';

function Init()
{
	var len = window.frames.length;
	
	if (window.sidebar)
	{ // Firefox iframe caching fix
		for(var x=0;x<len;x++)
			window.frames[0].src = window.frames[0].src;
	}
}
window.onload = Init;

// Quick Bookmark Function
function addbookmark()
{
    if (window.sidebar)
    { // Mozilla Firefox Bookmark
        window.sidebar.addPanel(bookmark_title, bookmark_url,"");
    }
    else if( window.external )
    { // IE Favorite
        window.external.AddFavorite(bookmark_url, bookmark_title);
    }
    else if(window.opera && window.print)
    { // Opera Hotlist
        return true;
    }
}

//Dynamic Width
function DynamicWidth()
{
   var larger_width = 920;
  
   if(window.innerWidth != null) width = window.innerWidth;
   if(document.body.clientWidth != null) width = document.body.clientWidth;
   
   if(width >= larger_width + 40)
      document.getElementById('wrapper').style.width = larger_width + "px";
}
//window.onresize = DynamicWidth;

// Setup Tracking
function trackThis(event, obj, args)
{
   if(obj.href.indexOf(script_url) == -1)
   {
      var gallery_url = obj.href;
      obj.href = script_url + args + obj.href;
      setTimeout(function(){obj.href = gallery_url;}, 5000);
   }
   
   return true;
}


// Thumb Rollover Functions
var t;
function StartRoll(img_obj, preview_ids)
{
   var len = preview_ids.length;
   
   if( len > 1 )
   {
      var myregexp = /http.*\/\d{2,3}x\d{2,3}\//i;
      var mymatch = myregexp.exec(img_obj.src);
      var thumb_url = mymatch[0];

      var subdirs = [];
      var i;
      for(i=0; i<len; i++)
         subdirs[i] = buildThumbURL(preview_ids[i]);
      
      for(i=0; i<len; i++)
         if(img_obj.src == thumb_url + subdirs[i])
		    break;
      
      if(i < len-1) img_obj.src = thumb_url + subdirs[i+1];
      else img_obj.src = thumb_url + subdirs[0];

      t = setTimeout(function(){StartRoll(img_obj, preview_ids);}, 800);
   }
}

function StopRoll(img_obj, preview_id)
{
   clearTimeout(t);

   var myregexp = /http.*\/\d{2,3}x\d{2,3}\//i;
   var mymatch = myregexp.exec(img_obj.src);
   var thumb_url = mymatch[0];

   buildThumbURL(preview_id);

   img_obj.src = thumb_url + buildThumbURL(preview_id);
}

function buildThumbURL(preview_id)
{
   var subdir1 = zeroPad( (Math.floor(preview_id/1000000) ), 3);
   var subdir2 = zeroPad( (Math.floor(preview_id/10000)   ), 3);
   var subdir3 = zeroPad( (Math.floor(preview_id/100)     ), 3);
   
   return subdir1 + '/' + subdir2 + '/' + subdir3 + '/' + preview_id + '.jpg';
}

function zeroPad(num,count)
{
   var numZeropad = num + '';
   while(numZeropad.length < count)
   {
      numZeropad = "0" + numZeropad;
   }
   return numZeropad;
}

// Pop-up Window Opener Funtion
function openWindow(url, width, height, scroll)
{
    var ns4 = (document.layers) ? true : false;           //NS 4
    var ie4 = (document.all) ? true : false;              //IE 4
    var dom = (document.getElementById) ? true : false;   //DOM

    if( ie4 || dom )
    {
        xMax = screen.availWidth;
        yMax = screen.availHeight;

        if( xMax <= width )
        {
            width = screen.availWidth;
        }

        if( yMax <= height )
        {
            height = screen.availHeight;
        }
    }

    else if( ns4 )
    {
        xMax = window.outerWidth;
        yMax = window.outerHeight;

        if( xMax <= width )
        {
            width = window.outerWidth;
        }

        if( yMax <= height )
        {
            height = window.outerHeight;
        }
    }

    else
    {
        xMax = 640;
        yMax = 480;
    }


    var xOffset = (xMax - width)/2;
    var yOffset = (yMax - height)/2;


    window.open(url, '_blank', 'menubar=yes,scrollbars='+scroll+',resizable=yes,status=yes,height='+height+',width='+width+',screenX='+xOffset+',screenY='+yOffset+',top='+yOffset+',left='+xOffset+'');

    return false;
}

function ThumbPageCounter(x){
	imageObj = new Image();
	imageObj.src = thumb_page_url + "?x=" + x;
} 
