var mashup = {width:137, height:87};
var v1, v2, v3, v4, v5;

function createCookie(name, value, days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
    if (BrowserDetect.browser=='Explorer' && BrowserDetect.version < 7) {
        parent.document[name] = value;
    }
}

function readCookie(name) {
    if (BrowserDetect.browser=='Explorer' && BrowserDetect.version < 7) {
        return parent.document[name];
    } 
    
    var nameE = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameE) == 0) {
            return c.substring(nameE.length,c.length);
        }
    }
    return null;
}

function eraseCookie(name) {
    createCookie(name,"",-1);
    if (BrowserDetect.browser=='Explorer' && BrowserDetect.version < 7) {
        parent.document[name] = null;
    }
}

function refreshVotes() {
    v1 = readCookie('vote1');
    v2 = readCookie('vote2');
    v3 = readCookie('vote3');
    v4 = readCookie('vote4');
    v5 = readCookie('vote5');
    return [v1, v2, v3, v4, v5];
}

function checkVotes() {
    refreshVotes();
    if (!v1 || !v2 || !v3 || !v4 || !v5) {
       var body = document.getElementById('body');
       body.innerHTML =
          "<h3>Please complete your votes</h2>" +
          "<ul>" +
          (v1 ? "<li>" + v1 + "</li>" : "") +
          (v2 ? "<li>" + v2 + "</li>" : "") +
          (v3 ? "<li>" + v3 + "</li>" : "") +
          (v4 ? "<li>" + v4 + "</li>" : "") +
          (v5 ? "<li>" + v5 + "</li>" : "") +
          "</ul>";

    } else {
        noMoreVoting(parent.document);
    }
}

function getCodes() {
    refreshVotes();
	
	theCode = '<h3 style="margin-left: 7px;">Copy the code below</h3>';
	theCode = theCode + '<form><textarea name="" cols="" rows="20" onclick="this.select();" style="width: 280px; margin-left: 7px; margin-top: 10px;">';
//	theCode = theCode + v1;
//	theCode = theCode + v2;
//	theCode = theCode + v3;
//	theCode = theCode + v4;
//	theCode = theCode + v5;

v1 = v1.substr(1,2);
if(v1.substr(1,1) == "]") v1 = v1.substr(0,1);
v2 = v2.substr(1,2);
if(v2.substr(1,1) == "]") v2 = v2.substr(0,1);
v3 = v3.substr(1,2);
if(v3.substr(1,1) == "]") v3 = v3.substr(0,1);
v4 = v4.substr(1,2);
if(v4.substr(1,1) == "]") v4 = v4.substr(0,1);
v5 = v5.substr(1,2);
if(v5.substr(1,1) == "]") v5 = v5.substr(0,1);
	
//vid_id_string = dragImage.src;
//vid_id_str_len = dragImage.src.length;
//vid_id_use = dragImage.src.substr((vid_id_str_len-6), 2)
	
	theCode = theCode + '<p width="100%" align="center"><embed pluginspage="http://www.adobe.com/go/getflashplayer" src="http://www.yourminis.com/Dir/GetContainer.api?uri=yourminis/CBS/mini:CBS" FlashVars="item0=' + v1 + '&item1=' + v2 + '&item2=' + v3 + '&item3=' + v4 + '&item4=' + v5 + '&width=301&hostname=www.yourminis.com&height=592&color=0&uri=yourminis/CBS/mini%3ACBS&" type="application/x-shockwave-flash" allowScriptAccess="always" wmode="transparent" width="302" height="594"></embed><br /><a title="For more widgets please visit www.yourminis.com" href="http://www.yourminis.com/index_minis.aspx?embeddedmini" target="_blank"><img border="0" alt="For more widgets please visit www.yourminis.com" src="http://www.yourminis.com/images/poweredby.png"/></a></p>';	
	
    var body = document.getElementById('codeDiv');
    body.innerHTML = theCode;
}

// Put votes in mashup and disable thumbnails put there
function showVote(slotN, title) {
    var performance = title.split(": ")[1];
    for (var i=1; i<=50; i++) {
        if (tPerformance[i] == performance) {
            // First put the large version in the mashup
            var src = "images/thumbs/lg/tn_lg_"+i+".jpg";
            var img = "<img src='"+src+
                         "' width='"+mashup.width+
                         "' height='"+mashup.height+
                         "' class='dropped_clip'/>";
             var dragTable = document.getElementById('dragTable');
             var tdslot = dragTable.getElementsByTagName('td')[slotN];
             var slot = tdslot.getElementsByTagName('div')[0];
             slot.innerHTML = img;

             // Now disable the thumbnail
             var thumb = document.getElementById('thumb'+i);
             var clip = thumb.parentNode;
             clip.style.opacity = .25;
             thumb.style.filter = "alpha(opacity=25);";
             clip.onmousedown = disabled_onmousedown;

        }
    }
}

// Disable further voting (either in thank you popup or on refresh
// - Show the "thank you" message
// - Hide the "vote now" button
// - Disable dragging thumbnails
function noMoreVoting(root) {
    root.getElementById('text-thankyou').style.display = "block";
    root.getElementById('wid_but').style.visibility = "visible";
    root.getElementById('votenow').style.display = "none";
    votingDone = true;
    ungray('*');
}

function populateVotes() {
    refreshVotes();
    if (v1) { showVote(0, v1); }
    if (v2) { showVote(1, v2); }
    if (v3) { showVote(2, v3); }
    if (v4) { showVote(3, v4); }
    if (v5) { showVote(4, v5); }

    // If all voted, show "thank you" and disable "vote now"
    if (v1 && v2 && v3 && v4 && v5) {
        noMoreVoting(document);
    }

}

function clearVotes() {
    eraseCookie('vote1');
    eraseCookie('vote2');
    eraseCookie('vote3');
    eraseCookie('vote4');
    eraseCookie('vote5');
}
