﻿var Contact = {}
Contact.toggleSelection = function (e) {
 
    var obj = $(e.target);
    var container = obj.attr("container");
    var objc = $("#container" + container);
    
    var deselect = $(".selectedcontact");
    $.each(deselect, function () {
        $(this).toggleClass("selectedcontact");
        $(this).toggleClass("unselected");
    });
    objc.toggleClass("selectedcontact");
    objc.toggleClass("unselected");

    deselect = $(".selectedcontact");
    $("#facebooksend").attr("disabled", !(deselect.length > 0))
    $("#linkedinsend").attr("disabled", !(deselect.length > 0))
}

function setsearchlabels() {
    var keywordlabel = $("#keyword_display");
    var locationlabel = $("#location_display");
    var keyword = $("#keyword").val();
    var location = $("#location").val();
    var company = "<%=Model.companyName %>";

    if (null != keywordlabel && "" != keyword) {
        var skeyword = "";
        if ("" != keyword) {
            skeyword = " for the search term \"" + keyword + "\"";
        }
        if (company != "") {
            skeyword += " at " + company;
        }
        keywordlabel.html(skeyword);
    }
    if (null != locationlabel && null != location) {
        if ("" == location) {
            locationlabel.html("");
        }
        else {
            locationlabel.html(" near " + location);
        }
    }
}
var searching = false;
var currentpage = 1;
function searchcompany(reset, companyname) {

    if (!searching) {
        searching = true;
        var view = $("#searchresults");
        if (null != view) {
            view.searchview();

            if (reset) {
                view.searchview("reset");
                currentpage = 1;
            }
            var searchparams = {
                "location": $("#location").val(),
                "keyword": $("#keyword").val(),
                "currentCompany": companyname,
                "page": currentpage,
                "pagesize": "10"
            };

            var keyword = $("#keyword").val();
            var location = $("#location").val();
            setsearchlabels();
            $.ajax(
                    { url: "/search/searchcompany",
                        type: "POST",
                        async: true,
                        data: searchparams,
                        error: function (xhr, textStatus, err) {
                            props.loadingMsg.fadeOut('normal');
                            self.insertElement.trigger("searchComplete");
                        },
                        success: function (data) {

                            $("#viewMoreCompanyResults").remove();
                            $("#searchresults").append(data);
                            $(".faded").slideDown("slow");
                            currentpage++;
                            bindevents();
                            searching = false;
                        }
                    }
                );
        }
    }
}


function searchContact(reset, snID, snType) {
    if (!searching) {

        searching = true;
        var view = $("#searchresults");
        if (null != view) {
            view.searchview();

            if (reset) {
                view.searchview("reset");
            }
            var keyword = $("#keyword").val();
            var location = $("#location").val();

            setsearchlabels("");
            view.searchview("searchConnection", $("#keyword").val(), $("#location").val(), snID, snType);

            var viewMore = $("#viewMore");
            if (null != viewMore) {
                viewMore.hide();
            }
        }
    }
}  



function searchall(reset) {
    if (!searching) {

        searching = true;
        var view = $("#searchresults");
        if (null != view) {
            view.searchview();

            if (reset) {
                view.searchview("reset");
            }
            var keyword = $("#keyword").val();
            var location = $("#location").val();
            setsearchlabels();
            view.searchview("search", $("#keyword").val(), $("#location").val());

            var viewMore = $("#viewMore");
            if (null != viewMore) {
                viewMore.hide();
            }
        }
    }
} 
      

function popup(url) {
      $.fancybox({
        'transitionIn': 'fade',
        'transitionOut': 'fade',
        'speedIn': 300,
        'speedOut': 200,
        'overlayShow': true,
        'width': 800,
        'height': 500,
        'opacity': true,
        'modal': false,
        'centerOnScroll': true,
        'hideOnContentClick': true,
        'showNavArrows': true,
        'overlayOpacity': .6,
        'href': url,
        'type': 'iframe',
        'padding': 0
    });   
}

function fb_invite(options) {
    options.type = 'Flotones.com';
    options.title = 'Invite your fans to Flotones';
    options.invitation_copy = 'Join up with me on Flotones and help me promote my music.';
    options.invitation_choice_label = "Join Flotones";
    //   options.invitation_choice_url
  //  options.request_action_url = window.location.protocol + '//' + window.location.host + '/connections/fbaccept?page=1';
    options.friend_selector_rows = 1;
    options.friend_selector_cols = 1;  
    fb_connect_invite_friends(options);
}

//
//
function fb_connect_invite_friends(options) {
        
    if (typeof options != 'object') { options = {}; }
    if (options.title == undefined) { options.title = "Invite your friends"; }
    if (options.type == undefined) { options.type = window.location.hostname; }
    if (options.all_friends_invited == undefined) { options.all_friends_invited = "<div style='padding: 10px; font-size: 1.2em;'>You've already invited all of your friends and they've accepted.</div>"; }
    if (options.invitation_copy == undefined) { options.invitation_copy = ""; }
    if (options.invitation_choice_url == undefined) { options.invitation_choice_url = window.location.protocol + '//' + window.location.host + '/'; }
    if (options.invitation_choice_label == undefined) { options.invitation_choice_label = 'Accept'; }
    if (options.request_action_url == undefined) { options.request_action_url = window.location.href; }
    if (options.request_action_text == undefined) { options.request_action_text = options.title; }
    if (options.friend_selector_rows == undefined) { options.friend_selector_rows = 3; }
    if (options.friend_selector_cols == undefined) { options.friend_selector_cols = 3; }
    if (options.friend_selector_email_invite == undefined) { options.friend_selector_email_invite = 'true'; }
    if (options.friend_selector_bypass == undefined) { options.friend_selector_bypass = 'cancel'; }
    if (isNaN(options.width)) { options.width = 600; }
    if (isNaN(options.height)) { options.height = 600; }
    
    FB.getLoginStatus(function(response) {
        if (!response.session) { return; }

        FB.api({ method: 'friends.get' }, function(friends) {
            var friend_ids = '';

         /*   try {
                var friend_ids_arr = friends.sort();
                removeByElement(friend_ids_arr, options.fbid);
                friend_ids = friend_ids_arr.join(',');
            } catch (e) { }*/

            var content = '';
            content += options.invitation_copy;
            content += "<fb:req-choice url='" + options.invitation_choice_url + "' label='" + options.invitation_choice_label + "' />";

            fbml = '';
            fbml += '<fb:fbml>';
            fbml += '<fb:request-form type="' + options.type + '" content="' + content + '" invite="true" action="' + options.request_action_url + '" method="post">';
            fbml += '<fb:multi-friend-selector';
            fbml += ' actiontext="' + options.request_action_text + '" ';
            fbml += ' showborder="true" import_external_friends="true" ';
            fbml += ' rows="' + options.friend_selector_rows + '" ';
            fbml += ' cols="' + options.friend_selector_cols + '" ';
           // fbml += ' exclude_ids="' + friend_ids + '" ';
            fbml += ' bypass="cancel"';
            fbml += ' email_invite="false" ';
            fbml += '/>';
            fbml += '</fb:request-form>';
            fbml += '</fb:fbml>';
            
            FB.ui({ method: 'fbml.dialog',
                display: 'dialog',
                fbml: fbml,
                width: 200,
                height: 180
            });           
        }); 
    });
}


function removeByElement(arrayName,arrayElement)
{
  for(var i=0; i<arrayName.length;i++ )
  {
    if(arrayName[i] == arrayElement) {
      arrayName.splice(i,1);
    }
  }
}



function sendURL(actionURL, obj, redirAfter, funcAfter) {
    var thisObj = obj;
    var resulttext = "You are a fan";
    if (thisObj.text() == resulttext) {
        return;
    }

    var isfan = thisObj.attr("isfan")=="True";
    
    if (isfan) {
        resulttext = "Become a fan";
    }

    var obj = {
        "artist": thisObj.attr("artistid")
    };

    var json = JSON.stringify(obj);

    $.ajax({
        url: actionURL,
        data: json,
        type: "POST",
        processData: false,
        contentType: "application/json",
        timeout: 10000,
        dataType: "text",  // not "json" we'll parse
        success:
                        function (data, textStatus, XMLHttpRequest) {
                            // alert(data);             
                            if (redirAfter == true) {
                                var myObject = eval('(' + data + ')');

                                if (null != funcAfter) {
                                    funcAfter(myObject.redirectaction);
                                }
                                else {
                                    window.location = myObject.redirectaction;
                                }
                            }
                            else {
                                thisObj.text(resulttext);
                            }
                        },
        error:
                    function (XMLHttpRequest, textStatus, errorThrown) {
                        alert(textStatus);
                    }
    });
}

