
// Site Variables
var bookmark_url = 'http://www.mia-movies.com';
var tracker_url = '/cgi-bin/swiftclicks/out.cgi';
var thumb_page_url = 'http://www.amazingcum.com/cgi-bin/page_counter/count.cgi';


// Dynamic Page Width
window.onresize = dynamicWidth;
function dynamicWidth()
{
    var width = 0;

    if(window.innerWidth != null) width = window.innerWidth;
    if(document.body.clientWidth != null) width = document.body.clientWidth;

    if( width )
    {
        if( width >= 1364 )
        {
            if( document.getElementById('wrapper').style.width != "1304px" ) document.getElementById('wrapper').style.width = "1304px";
            if( document.getElementById('cats-table').style.height != "4076px" ) document.getElementById('cats-table').style.height = "4076px";
            if( document.getElementById('main-head').style.width != "1204px" ) document.getElementById('main-head').style.width = "1204px";
            if( document.getElementById('main-head').style.backgroundImage != "url('/images/cloud_head_1204x40.gif')" ) document.getElementById('main-head').style.backgroundImage = "url('/images/cloud_head_1204x40.gif')";
            if( document.getElementById('main-foot').style.width != "1204px" ) document.getElementById('main-foot').style.width = "1204px";
            if( document.getElementById('main-foot').style.backgroundImage != "url('/images/cloud_foot_1204x45.gif')" ) document.getElementById('main-foot').style.backgroundImage = "url('/images/cloud_foot_1204x45.gif')";
        }
        else if( width >= 1192 )
        {
            if( document.getElementById('wrapper') && document.getElementById('wrapper').style.width != "1132px" ) document.getElementById('wrapper').style.width = "1132px";
            if( document.getElementById('cats-table') && document.getElementById('cats-table').style.height != "4756px" ) document.getElementById('cats-table').style.height = "4756px";
            if( document.getElementById('main-head') && document.getElementById('main-head').style.width != "1032px" ) document.getElementById('main-head').style.width = "1032px";
            if( document.getElementById('main-head') && document.getElementById('main-head').style.backgroundImage != "url('/images/cloud_head_1032x40.gif')" ) document.getElementById('main-head').style.backgroundImage = "url('/images/cloud_head_1032x40.gif')";
            if( document.getElementById('main-foot') && document.getElementById('main-foot').style.width != "1032px" ) document.getElementById('main-foot').style.width = "1032px";
            if( document.getElementById('main-foot') && document.getElementById('main-foot').style.backgroundImage != "url('/images/cloud_foot_1032x45.gif')" ) document.getElementById('main-foot').style.backgroundImage = "url('/images/cloud_foot_1032x45.gif')";
        }
        else
        {
            if( document.getElementById('wrapper') && document.getElementById('wrapper').style.width != "960px" ) document.getElementById('wrapper').style.width = "960px";
            if( document.getElementById('cats-table') && document.getElementById('cats-table').style.height != "5708px" ) document.getElementById('cats-table').style.height = "5708px";
            if( document.getElementById('main-head') && document.getElementById('main-head').style.width != "860px" ) document.getElementById('main-head').style.width = "860px";
            if( document.getElementById('main-head') && document.getElementById('main-head').style.backgroundImage != "url('/images/cloud_head_860x40.gif')" ) document.getElementById('main-head').style.backgroundImage = "url('/images/cloud_head_860x40.gif')";
            if( document.getElementById('main-foot') && document.getElementById('main-foot').style.width != "860px" ) document.getElementById('main-foot').style.width = "860px";
            if( document.getElementById('main-foot') && document.getElementById('main-foot').style.backgroundImage != "url('/images/cloud_foot_860x45.gif')" ) document.getElementById('main-foot').style.backgroundImage = "url('/images/cloud_foot_860x45.gif')";
        }
    }
}


// Quick Bookmark Function
function addBookmark()
{
    var bookmark_title = document.title; 

    // Firefox Bookmark
    if (window.sidebar)
    {
        window.sidebar.addPanel(bookmark_title, bookmark_url, '');
    }
    // IE Favorite
    else if( window.external )
    {
        window.external.AddFavorite(bookmark_url, bookmark_title);
    }
    // Opera Hotlist
    else if(window.opera && window.print)
    {
        return true;
    }
}


// Convert UTC time to client local time
function toLocalTime(year,month,day,hours,minutes)
{
    month--;
    var d = (new Date(Date.UTC(year,month,day,hours,minutes))).toLocaleString();

    var myregexp = /^(\w+),\s(\w+)\s(\d+),\s(\d+)\s(\d+):(\d+):(\d+)\s(\w+)/i;
    var mymatch = myregexp.exec(d);

    var my_month  = mymatch[2].substring(0,3);
    var my_day    = mymatch[3];
    var my_year   = mymatch[4];
    var my_hour   = mymatch[5];
    var my_minute = mymatch[6];
    var my_am_pm  = mymatch[8];

    return my_month + ' ' + my_day + ', ' + my_year + ', ' + my_hour + ':' + my_minute + my_am_pm;
}


// Setup Tracking
function trackThis(obj, args)
{
    if(obj.href.indexOf(tracker_url) == -1)
    {
        var gallery_url = obj.href;
        obj.href = tracker_url + args + obj.href;
        setTimeout(function(){obj.href = gallery_url;}, 5000);
    }
    return true;
}


// Series Rollover Functions
var t;
function startSeriesRoll(img_obj)
{
    var myregexp = /(.*\d\d)\w\.jpg$/i;
    var mymatch = myregexp.exec(img_obj.src);
    var thumb_url = mymatch[1];

    var letter_jpgs = ['a.jpg','b.jpg','c.jpg'];

    for(i=0; i<3; i++)
    {
        if(img_obj.src == thumb_url + letter_jpgs[i]) break;
    }

    if(i < 2) img_obj.src = thumb_url + letter_jpgs[i+1];
    else img_obj.src = thumb_url + letter_jpgs[0];

    t = setTimeout(function(){startSeriesRoll(img_obj);}, 800);
}


function stopSeriesRoll(img_obj)
{
    clearTimeout(t);

    var myregexp = /(.*\d\d)\w\.jpg$/i;
    var mymatch = myregexp.exec(img_obj.src);
    var thumb_url = mymatch[1];

    img_obj.src = thumb_url + 'a.jpg';
}


// Thumb Rollover Functions
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] = getSubdirs(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];

    getSubdirs(preview_id);

    img_obj.src = thumb_url + getSubdirs(preview_id);
}


function getSubdirs(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;
}


// Bandwith Tracking Counter
function ThumbPageCounter(x)
{
    imageObj = new Image();
    imageObj.src = thumb_page_url + "?x=" + x;
}

