﻿$(function() {

    $('.markitup').markItUp(mySettings);

    // Dialog			
    $('#signup').dialog({
        autoOpen: false,
        width: 470,
        draggable: true,
        persist: true,
        modal: true
    });

    // Dialog Link
    $('#signup_link').click(function() {
        $('#signup').dialog('open');
        return false;
    });

    $('.markitup_toggle').click(function() {
        $(".editor_container").toggle("blind");
        return false;
    });

    // Facebook Connect
    FB.init("b1c348c95ca8c970b80f91823fcd8633", "xd_receiver.htm");
    $("#facebook_invite").click(function() { fbInvite(); return false; })


    // Styles Text-to-image
    $(".t2i_menu").each(function() { })


});

function style() {

    var test = "/gfx-bin/dynamic.ashx?text=RmlubiBrbMOmcg==&font=Tahoma&fontColor=255|255|255|255&shadowColor=50|0|0|0&bgColor=0|255|255|255&fontSize=10&shadowDistance=1&antialias=1&bold=1";

}

function frontTableHL(owner) {
    if (owner.className == 'front_new_row')
        owner.className = 'front_new_row_hl';
    else
        owner.className = 'front_new_row';
}
function setImage(newImage, newLink) {
    document.getElementById('mainImage').src = newImage;
    document.getElementById('mainImageLink').href = newLink;
}
function displaySearch() {
    $("#search").toggle("blind");
}

function changeCategoryChildren(owner, id) {
    var allInputs = $('#child_container_' + id + ' input')
    for (i = 0; i < allInputs.length; i++) {
        if (owner.checked)
            allInputs[i].checked = true;
        else
            allInputs[i].checked = false;
    }
}

function hasAccepted(sender, args) {
    args.IsValid = true;
    var IDHolder = document.getElementById('acceptID').value;
    if (!document.getElementById(IDHolder).checked)
        args.IsValid = false;

    return true;
}

function validateUsername(box, basePath) {
    document.getElementById('validateUsernameResponse').src = basePath + '/gfx-bin/verify.ashx?username=' + box.value;
    document.getElementById('validateUsernameResponse').style.visibility = 'visible';
    return true;
}

function allCategories(mode) {
    var ID = document.getElementById('categoryIDHolder').value;
    document.getElementById(ID).checked = mode;
}

function details(id, mode) {
    var element = document.getElementById(id);
    if (mode)
        element.style.display = 'block';
    else
        element.style.display = 'none';
}

function wishlist(add, base) {
    var owner = document.getElementById('wishlist_' + add);
    if (owner.src.indexOf('wishlist_disabled.png') > 0) {
        $('#signup').dialog('open');
        return false;
    }

    var i = Math.round(10000 * Math.random())
    $.get(base + "/api/wishlist.ashx?random=" + i + "&adID=" + add);
    if (owner.src.indexOf('wishlist_add.png') > 0)
        owner.src = owner.src.replace('wishlist_add.png', 'wishlist_remove.png');
    else
        owner.src = owner.src.replace('wishlist_remove.png', 'wishlist_add.png');
}

function defaultBackground(box, fieldID, className) {
    field = document.getElementById(fieldID);
    if (box.value == '')
        field.className = className;
}

function fbs_click() {
    u = location.href;
    t = document.title;
    window.open('http://www.facebook.com/sharer.php?u=' + encodeURIComponent(u) + '&t=' + encodeURIComponent(t), 'sharer', 'toolbar=0,status=0,width=626,height=436'); return false;
}

function fbs_item(u, t) {
    window.open('http://www.facebook.com/sharer.php?u=' + encodeURIComponent(u) + '&t=' + encodeURIComponent(t), 'sharer', 'toolbar=0,status=0,width=626,height=436'); return false;
}

function replyBid(bidID, reply, defaultMessage) {
    message = document.getElementById('bidmessage_' + bidID).value.replace(defaultMessage, '');
    window.location = '../user/trading.aspx?bidID=' + bidID + '&reply=' + reply + '&message=' + message;
}

function initTextbox(box, defaultMessage) {
    box.value = box.value.replace(defaultMessage, '');
}

function reply(id) {
    var header = $("#header_" + id).attr("value");
    $(".input_header").attr("value", header);
    $(".markItUpEditor").attr("value", header);
    $(".editor_container").show("blind");
}

function quote(id) {
    var original = $("#original_" + id).attr("value");
    var header = $("#header_" + id).attr("value");
    var by = $("#by_" + id).attr("value");
    $(".input_header").attr("value", header);
    $(".markItUpEditor").attr("value", "[quote from=" + by + "]" + original + "[/quote]");
    $(".editor_container").show("blind");
}

/* Peacock */
function selectColour(boxID, checkBoxID) {

    var box = document.getElementById(boxID);
    var checkBox = document.getElementById(checkBoxID);

    if (box.className == "selected") {
        box.className = "";
        checkBox.checked = false;
    }
    else {
        box.className = "selected";
        checkBox.checked = true;
    }
}

this.hasRates = false;
function ratePeacock(peacockID) {

    if (this.hasRated)
        return;

    var i = Math.round(10000 * Math.random())
    $.get("../../api/peacock.ashx?random=" + i + "&peacockID=" + peacockID);
    
    var rating = parseInt($(".count").html());
    rating++;

    $(".count").html(rating.toString());
    $(".action").attr("class", "action_disabled");
    this.hasRated = true;
}

/* Facebook Connect integration */
function fbInvite() {
    FB.Connect.requireSession(function() { fbShowInvite(); }, isUserActionHint = true);
}
function fbPostNewAdInit(name, adURL, profileURL, imageURL) {
    FB.Connect.requireSession(function() { fbPostNewAd(name, adURL, profileURL, imageURL); }, isUserActionHint = true);
}

function fbShowInvite() {

    var userID = FB.Connect.get_loggedInUser();

    var fb = "";
    fb += "<fb:fbml>";
    fb += "<fb:request-form action=\"http://www.wardrobe.no\" method=\"POST\" invite=\"true\" type=\"Wardrobe\" ";
    fb += "content=\"Jeg er medlem på Wardrobe, Norges felles klesskap, og vil gjerne invitere deg. Klikk på knappen under for å besøke Wardrobe! <fb:req-choice url='http://www.wardrobe.no' label='Besøk Wardrobe' />\">";
    fb += "<fb:multi-friend-selector showborder=\"false\" actiontext=\"Inviter dine venner til Wardrobe\" />";
    fb += "</fb:request-form></fb:fbml>";

    var invite = new FB.UI.FBMLPopupDialog("Inviter vennene dine", fb);
    invite.setContentWidth(776);
    invite.setContentHeight(600);
    invite.show();

}

function fbPostNewAd(name, adURL, profileURL, imageURL) {
    var message = 'Ny vare på Wardrobe'; 
    var attachment = {
            'name': 'Jeg selger "' + name + '" på Wardrobe', 
            'href':adURL,
            'caption': '{*actor*} har lagt inn en ny vare med overskriften "' + name + '" på Wardrobe.',
            'description': 'På Wardrobe kan du selge klær du ikke bruker, og få noe nytt i stedet.',
            'media': [{ 'type': 'image', 'src': imageURL, 'href': adURL}]
        };

    var action_links = { 'text': 'Besøk min profil på Wardrobe', 'href': profileURL }; 
    FB.Connect.streamPublish(message, attachment, action_links);
}

function fbPostPeacock(name, peacockURL, profileURL, imageURL) {
    var message = 'Stil-bilde på Wardrobe';
    var attachment = {
        'name': 'Se bildet "' + name + '" på Wardrobe',
        'href': peacockURL,
        'caption': 'Ta en titt på bildet "' + name + '" på Wardrobe.',
        'description': 'Med Wardrobe iStyle kan du vise frem stilen din!',
        'media': [{ 'type': 'image', 'src': imageURL, 'href': peacockURL}]
    };

    var action_links = { 'text': 'Se stil-bilde', 'href': peacockURL };
    FB.Connect.streamPublish(message, attachment, action_links);
}


/* Third party tools */
var tooltip=function(){
 var id = 'tt';
 var top = 3;
 var left = 3;
 var maxw = 300;
 var speed = 20;
 var timer = 20;
 var endalpha = 95;
 var alpha = 0;
 var tt,t,c,b,h;
 var ie = document.all ? true : false;
 return{
  show:function(v,w){
   if(tt == null){
    tt = document.createElement('div');
    tt.setAttribute('id',id);
    t = document.createElement('div');
    t.setAttribute('id',id + 'top');
    c = document.createElement('div');
    c.setAttribute('id',id + 'cont');
    b = document.createElement('div');
    b.setAttribute('id',id + 'bot');
    tt.appendChild(t);
    tt.appendChild(c);
    tt.appendChild(b);
    document.body.appendChild(tt);
    tt.style.opacity = 0;
    tt.style.filter = 'alpha(opacity=0)';
    document.onmousemove = this.pos;
   }
   tt.style.display = 'block';
   c.innerHTML = v;
   tt.style.width = w ? w + 'px' : 'auto';
   if(!w && ie){
    t.style.display = 'none';
    b.style.display = 'none';
    tt.style.width = tt.offsetWidth;
    t.style.display = 'block';
    b.style.display = 'block';
   }
  if(tt.offsetWidth > maxw){tt.style.width = maxw + 'px'}
  h = parseInt(tt.offsetHeight) + top;
  clearInterval(tt.timer);
  tt.timer = setInterval(function(){tooltip.fade(1)},timer);
  },
  pos:function(e){
   var u = ie ? event.clientY + document.documentElement.scrollTop : e.pageY;
   var l = ie ? event.clientX + document.documentElement.scrollLeft : e.pageX;
   tt.style.top = (u - h) + 'px';
   tt.style.left = (l + left) + 'px';
  },
  fade:function(d){
   var a = alpha;
   if((a != endalpha && d == 1) || (a != 0 && d == -1)){
    var i = speed;
   if(endalpha - a < speed && d == 1){
    i = endalpha - a;
   }else if(alpha < speed && d == -1){
     i = a;
   }
   alpha = a + (i * d);
   tt.style.opacity = alpha * .01;
   tt.style.filter = 'alpha(opacity=' + alpha + ')';
  }else{
    clearInterval(tt.timer);
     if(d == -1){tt.style.display = 'none'}
  }
 },
 hide:function(){
  clearInterval(tt.timer);
   tt.timer = setInterval(function(){tooltip.fade(-1)},timer);
  }
 };
}();
