function sortNumber(a, b) {
    return a - b;
}
function dateSort(imgObj) {
    var sortedAry = new Array();
    var dateAry = new Array();
    var orderedAry = new Array();
    for(var i=0; i<collections.length; i++) {
        sortedAry.push(collections[i][0]);
        dateAry[collections[i][0]] = i;
    }
    for(var j=0; j<sortedAry.length; j++) {
        orderedAry.push(dateAry[sortedAry[j]]);
    }
    flipSort(imgObj, orderedAry);
}
function titleSort(imgObj) {
    var sortedAry = new Array();
    var titleAry = new Array();
    var orderedAry = new Array();
    for(var i=0; i<collections.length; i++) {
        sortedAry.push(collections[i][1]);
        titleAry[collections[i][1]] = i;
    }
    sortedAry.sort();
    for(var j=0; j<sortedAry.length; j++) {
        orderedAry.push(titleAry[sortedAry[j]]);
    }
    flipSort(imgObj, orderedAry);
}
function photoSort(imgObj) {
    var sortedAry = new Array();
    var photosAry = new Array();
    var orderedAry = new Array();
    for(var i=0; i<collections.length; i++) {
        sortedAry.push([collections[i][2]]);
        if(!(photosAry["p" + collections[i][2]] instanceof Array)) 
            photosAry["p" + collections[i][2]] = new Array();
        photosAry["p" + collections[i][2]].push(i);
    }
    sortedAry.sort(sortNumber);
    for(var j=0; j<sortedAry.length; j++) {
        orderedAry[j] = photosAry["p" + sortedAry[j]][0];
        if(photosAry["p" + sortedAry[j]].length > 1)
            photosAry["p" + sortedAry[j]].splice(0,1);
    }
    flipSort(imgObj, orderedAry.reverse());
}
function ratingSort(imgObj) {
    var sortedAry = new Array();
    var rateAry = new Array();
    var orderedAry = new Array();
    for(var i=0; i<collections.length; i++) {
        sortedAry.push([collections[i][7]]);
        if(!(rateAry["p" + collections[i][7]] instanceof Array)) 
            rateAry["p" + collections[i][7]] = new Array();
        rateAry["p" + collections[i][7]].push(i);
    }
    sortedAry.sort(sortNumber);
    for(var j=0; j<sortedAry.length; j++) {
        orderedAry[j] = rateAry["p" + sortedAry[j]][0];
        if(rateAry["p" + sortedAry[j]].length > 1)
            rateAry["p" + sortedAry[j]].splice(0,1);
    }
    flipSort(imgObj, orderedAry.reverse());
}
function flipSort(imgObj, orderedAry) {
    if(imgObj.src.indexOf("up") > 0) {
        imgObj.src = imgObj.src.replace(/up/,"down");
        printCollections(orderedAry);
    } else {
        imgObj.src = imgObj.src.replace(/down/,"up");
        printCollections(orderedAry.reverse());
    }
}
function printCollections(orderedAry) {
    var collectionData = document.getElementById("collectionTable");
    collectionData.innerHTML=null;
    var total = orderedAry.length;
    var rows = Math.ceil(total/3);
    var html = '<table width=405 border=0 cellspacing=0 cellpadding=0>'; 
    var count = 0;
    for(var row=0; row<rows; row++) {
        html += '<tr>'; 
        for(var cell=0; cell<3; cell++) {
            if(count < total) {
                var ele = orderedAry[count];
                html += '<td class="bx-collection-bio02"><a href="http://www.mystique-magazine.com/bin/feature?id=' + collections[ele][4] + '"><img src="http://media.mystique-magazine.com/uobf.jpg?' + collections[ele][5] + '" width=100 height=150 border=0 class="thumb"></a><br>' + collections[ele][1] + '<br>';
                var nvids = (collections[ele][3] <= 1) ? ' video' : ' videos';
                html += '<span class="txt10grey">' + collections[ele][2] +
                    ' photos &amp; ' + collections[ele][3] + nvids + '</span><br><br>';
                if(collections[ele][6] < 0)
                    html += '<img src="/images/common/bio-coming-soon.gif"><br>';
            } else {
                html += '<td height=190 width=116>&nbsp;</td>';
            }
            if(cell < 2)
                html += '<td>&nbsp;</td>';
            count++;
        }
        html += '</tr><tr><td colspan=5>&nbsp;</td></tr>';
    }
    collectionData.innerHTML = html + '</table>';
}
function bigPic(img,title,ptp) {
    PopUp('/big.htm','big','xga',1);
    popupHandle['big'].ptpTitle = title;
    popupHandle['big'].ptpImg = ptp;
    popupHandle['big'].imgSrc = img;
}
function printClips(num,vid) {
    var clipData = document.getElementById("clipTable");
    clipData.innerHTML=null;
    var html = "<table width=232 border=0 cellspacing=0 cellpadding=0>";
    if(num == 0) {
        html += "<tr><td width=63><img src=\"/images/common/b-video01-off.gif\" width=63 height=17></td>";
    } else {
        html += "<tr><td width=63><a href=\"javascript: printClips(0)\"><img src=\"/images/common/b-video01.gif\" width=63 height=17 border=0></a></td>";
    }
    if(clips.length > 1) {
        if(num == 1) {
           html += "<td width=56><img src=\"/images/common/b-video02-off.gif\" width=56 height=17></td>";
        } else {
           html += "<td width=56><a href=\"javascript: printClips(1)\"><img src=\"/images/common/b-video02.gif\" width=56 height=17 border=0></a></td>";
        }
    }
    html += "<td width=113 align=\"right\">";
    if(clips[num]['hd'] == "T") {
        html += "<img src=\"/images/common/icon-hd-available.gif\" width=60 height=10></td></tr>";
    } else {
        html += "&nbsp;</td></tr>";
    }
    html += "<tr><td colspan=3>";
    html += '<embed src="/images/common/player240.swf" width="240" height="' + clips[num]['ht'] + '" allowfullscreen="false" flashvars="&displayheight=240&file=' + clips[num]['flv'] + '&height=' + clips[num]['ht'] + '&image=' + clips[num]['img'] + '&width=240&backcolor=0xFFFFFFF"/>';
    html += "<tr><td colspan=3 align=\"center\"><img src=\"/images/common/spacer.gif\" width=10 height=10><span class=\"txt11grey\">" + clips[num]['title'] + " (" + clips[num]['rt'] + ")<br>Note: actual video is larger, longer &amp; clearer</span></div></td></tr>";
    html += "<tr align=\"center\"><td colspan=3 class=\"txt14white\"><b><br>Full Screen High Quality Video </b></td></tr>";
    html += "<tr align=\"center\"><td colspan=3>";
    for (var f in clips[num]['avail']) {
        html += (vid > 0) ? 
            "<a href=\"/members/bin/watchvideo?id=" + vid + "\"><img src=\"/images/common/" + clips[num]['avail'][f] + "\" border=0 hspace=5 vspace=5></a>" : 
            "<img src=\"/images/common/" + clips[num]['avail'][f] + "\" hspace=5 vspace=5>"; 
    }
    html += "</td></tr></table>";
    clipData.innerHTML=html;
}
function toFavorites(action,type,id,cid,postCallback) {
    if(auth == null) {
        disableFn();
        return;
    }
    if(action == -1) {
        if(!confirm("Are you sure you want to delete this " + type + "?"))
            return;
    }
    var fav = new XHRequest(favCallback,postCallback);
    var param = "action=" + action + "&fav=" + type + "&id=" + id + "&cid=" + cid;
    fav.connect("POST", "/members/bin/favorite", param, true);
}
function favCallback(txtObj,xmlObj) {
    var msg = xmlObj.getElementsByTagName("msg").item(0);
    alert(msg.firstChild.data);
    var status = xmlObj.getElementsByTagName("status").item(0);
    return (status.firstChild.data == 1) ? true : false;
}
function flipCollection() {
    var favText = (document.getElementById("cFav").innerHTML);
    var favLink = (document.getElementById("cFav").href);
    var favImg = document.images.iFav;
    if(favText.indexOf("add") >= 0) {
        favText = favText.replace(/add collection to/,"remove collection from");
        favLink = favLink.replace("(1","(-1");
        favImg.src = "/images/common/b-del.gif";
    } else {
        favText = favText.replace(/remove collection from/,"add collection to");
        favLink = favLink.replace("(-1","(1");
        favImg.src = "/images/common/icon-check.gif";
    }
    document.getElementById("cFav").innerHTML = favText;
    document.getElementById("cFav").href = favLink;
}
function navHtml(total,from,limit,fn) {
    var totalPages = Math.ceil(total/limit);
    var currPage = Math.ceil((from+limit)/limit);
    var disp = 4;
    var lo = 1;
    var hi = 10;
    var dots = ' ... ';
    var nhtml = "&laquo;&laquo;";
    nhtml += (from == 0) ? 'pg1 ' : '<a href="javascript: ' + fn + '(0)">pg1</a> ';
    if(currPage > disp) {
        lo = (currPage - disp);
        nhtml += dots;
    } 
    if((currPage + disp) >= totalPages) {
        lo = totalPages - disp * 2;
    }
    hi = lo + disp * 2;
    if(lo < 0) lo = 1;
    if(hi >= totalPages) hi = totalPages;
    var i = 0;
    for(i=(lo+1); i<=hi; i++) {
        var pg = 'pg' + i;
        var sp = (i == totalPages) ? '' : ' ';
        nhtml += (i == currPage) ?
            pg + sp:
            '<a href="javascript: ' + fn + '(' + (limit*(i-1)) + ')">' + pg + '</a>' + sp;
        if((totalPages > hi) && (i%(hi) == 0))
            nhtml += dots;
    }
    if(totalPages > hi)
        nhtml += '<a href="javascript: ' + fn + '(' + (limit*(totalPages - 1)) + ')">pg' + totalPages + '</a>';
    return nhtml + "&raquo;&raquo;";
}
function viewLarge(size,pid,id,title,len,mem) {
    var url = mem + "/bin/big?size=" + size + "&pid=" + pid + "&id=" + id + "&title=" + escape(title) + "&l=" + len;
    PopUp(url,'big','xga',1);
}
function goPtp(title,img) {
    var provider = 'mystique';
    var newWindow = true;
    var loc = "http://pictopia.com/perl/ptp/" + provider
            + "?title=" + escape(title)
            + "&photo_name=" + img;
    if(img.indexOf("0741" == 0))
        loc += "&pps=BTS";

    if (newWindow) {
        var  win = window.open(loc, 'ptp');
        win.focus();
    } else {
        location.href = loc;
    }
} 
function zipDown() {
    if(zips.length == 0) {
        disableFn();
        return;
    }
    var size = getSize();
    document.getElementById('zipit').href = media + "/uobf.zip?" + zips[size];
}
function disableFn() {
    alert("Sorry, this function is disabled in the free monthly preview area");
}
function freeMonthly() {
    var fr = new XHRequest(freeCallback);
    fr.connect("GET", "/includes/free-monthly.htm", "", true);
}
function freeCallback(txtObj, xmlObj) {
    document.getElementById("free-monthly").innerHTML = txtObj;
}
function cartTotal(prices) {
    var total = 0.00;
    var disc = 0.00;
    var ret = new Object();
    for(var i=0; i<prices.length; i++) {
        total += parseFloat(prices[i]);
    }
    if((prices.length >= 5) && (prices.length <= 9))
        disc = total * 0.10;
    else if(prices.length >= 10)
        disc = total * 0.15;
    ret.disc = disc;
    ret.total = total - disc;
    return ret;
}
function credTotal(price,num) {
    var total = price * num;
    var disc = 0.00;
    var ret = new Object();
    if((num >= 50) && (num <= 74))
        disc = total * 0.10;
    else if(num >= 75)
        disc = total * 0.20;
    ret.disc = disc;
    ret.total = total - disc;
    return ret;
}
function redeem(id) {
    if(credits < required) {
        alert("Sorry, you do not have enough credits available.\nPlease click BUY CREDITS button at top of page, or wait until next monthly re-up");
        return;
    } else {
        var proceed = confirm("You are about to redeem " + required + " credits\nto activate this collection on your account.\nThis is permanent and cannot be undone.\nAre you sure you wish to proceed?");
        if(proceed) {
            post("/members/bin/redeem?id=" + id, "", redeemCallback, refresh);
        }
    }
}
function redeemCallback(txtObj,xmlObj) {
    var msg = xmlObj.getElementsByTagName("msg").item(0);
    alert(msg.firstChild.data);
    window.location.reload();
    var status = xmlObj.getElementsByTagName("status").item(0);
    return (status.firstChild.data == 1) ? true : false;
}
function refresh () {
    window.location.reload();
}

