﻿//anonymous function - c
//# by ID
//.Class
//$jQuery
//Comments text





jQuery(function($) {


    $('a.publish').click();
    /*  Comment submit to db
    =============================================================================*/
    $("body").delegate(".frmBtnSubmitComment", "click", function() {

        var url = $(this).attr("postURL");
        // alert(url);
        var param = new Array();
        var name = $("#ctl00_CPMain_txtName");
        var email = $("#ctl00_CPMain_txtEmail");
        var comment = $("#ctl00_CPMain_txtComment");

        var box = name.val();
        re = /^[a-zA-Z,\s]*$/;

        if (!re.exec(box)) {
            //alert("Invalid Entry:\nOnly Alphabetic characters or Underscore allowed!");
            box.focus();
            return;
        } //end name if

        // Split a classnote into array by space
        var words = comment.val().split("'");
        var tmp = "";

        // Replace double quote(") to single quote (') through LOOP
        for (i = 0; i < words.length; i++) {
            tmp = tmp + words[i].replace("'", '"') + ' ';
        }


        //alert(comment);
        param[0] = " userId:'" + readCookie('userid_') + " '"
        param[1] = "mediaFileId :'" + $("#ctl00_CPMain_hdnContentLikeId").val() + " '"
        param[2] = "Name :'" + name.val() + " '"
        param[3] = "Email :'" + email.val() + " '"
        param[4] = "Text :'" + tmp + " '"
        param.join(",");
        AjaxCall(url, param);

        // alert(url);
        return false;
    });


    ///Comments individual

    /*  Comment submit to db
    =============================================================================*/
    $("body").delegate(".SubmitCommentInd", "click", function() {

        var url = $(this).attr("postURL");
        // alert(url);
        var param = new Array();
        var name = $("#txtName");
        var email = $("#txtEmail");
        var comment = $("#txtComment");

        var box = name.val();
        re = /^[a-zA-Z,\s]*$/;

        if (!re.exec(box)) {
            //alert("Invalid Entry:\nOnly Alphabetic characters or Underscore allowed!");
            box.focus();
            return;
        } //end name if

        // Split a classnote into array by space
        var words = comment.val().split("'");
        var tmp = "";

        // Replace double quote(") to single quote (') through LOOP
        for (i = 0; i < words.length; i++) {
            tmp = tmp + words[i].replace("'", '"') + ' ';
        }


        //alert(comment);
        param[0] = " userId:'" + readCookie('userid_') + " '"
        param[1] = "mediaFileId :'" + $("#hdnContentLikeId").val() + " '"
        param[2] = "Name :'" + name.val() + " '"
        param[3] = "Email :'" + email.val() + " '"
        param[4] = "Text :'" + tmp + " '"
        param.join(",");
        //  alert(url);
        AjaxCall(url, param);


        return false;
    });


    /*  Like function With Login script on video.aspx
    =============================================================================*/
    $("body").delegate(".frmBntLikeTest", "click", function() {

        if (readCookie('userid_') > 0) {
            var url = $(this).attr("postURL");
            var param = new Array();

            param[0] = "Id :'" + $("#ctl00_CPMain_hdnContentLikeId").val() + " '"
            param[1] = "userId:'" + readCookie('userid_') + " '"
            param[2] = "tableName :'" + $("#ctl00_CPMain_hdnTableName").val() + " '"

            param.join(",");
            // alert("inajax");
            AjaxCallback(url, param, function() {
                $("#notify").html("");
            });

            $(this).addClass("disable");


            // alert(url);
            return false;
        }
        else {
            //display message in div to login user 
            // alert("Login now");
        }
    });

    /*  Like function script on video.aspx
    =============================================================================*/
    $("body").delegate(".frmBntLike", "click", function() {

        var userId = readCookie('userid_');
        // alert("df");
        if (userId > 0) {
            var url = $(this).attr("postURL");
            var param = new Array();

            param[0] = "Id :'" + $("#ctl00_CPMain_hdnContentLikeId").val() + " '"
            param[1] = "userId:'" + readCookie('userid_') + " '"
            param[2] = "tableName :'" + $("#ctl00_CPMain_hdnTableName").val() + " '"

            param.join(",");
            //alert("inajax");
            //alert(param);
            AjaxCallback(url, param, function() {
                $("#notify").html("");
            });

            $(this).addClass("disable");

        }
        else {
            //not login
            //show fancybox
            //alert("else");
            //            var $gigyaMarkup = $("#inlineDivLike");
            //            //var html = $gigyaMarkup.html();
            //            $gigyaMarkup.find("#inlineDivLike_bottomLink").detach();
            //            //alert($gigyaMarkup.size());
            //            $("#inlineDivLike #gigya").html($gigyaMarkup.html());
            //            $("#dummylinkLike").trigger("click");


            var $gigyaMarkup = $("#loginDiv");
            //var html = $gigyaMarkup.html();
            $gigyaMarkup.find("#loginDiv_bottomLink").detach();
            // alert($gigyaMarkup.size());
            $("#inlineDivLike #gigya").html($gigyaMarkup.html());

            //resizing
            ResizeGigyaImproved("#gigya");
            /*var $Div = $("#gigya");
            $Div.find("img").each(function(i) {
            $(this).css({ height: "30px", width: "30px" }).closest("td").css({ height: "30px", width: "30px" }).find("div").css({ height: "30px", width: "30px" });
            });
            $Div.find("table").css({ height: "auto" });
            $Div.show();                  
            */
            $("#dummylinkLike").trigger("click");

        }
        // alert(url);
        return false;
    });


    /* Videos & Artist Likee Feature
    ==================================================*/

    $("body").delegate(".BntLikeFeature", "click", function() {

        var userId = readCookie('userid_');
        // alert("df");

        if (userId > 0) {
            var url = $(this).attr("postURL");
            var param = new Array();

            param[0] = "Id :'" + $(this).attr("contentLikeId") + " '"
            param[1] = "userId:'" + userId + " '"
            param[2] = "tableName :'" + $(this).attr("tableName") + " '"
            param.join(",");

            //alert("inajax");
            //alert(param);
            AjaxCallback(url, param, function() {
                $("#notify").html("");
            });

            $(this).addClass("disable");

        }
        else {
            var $gigyaMarkup = $("#loginDiv");
            //var html = $gigyaMarkup.html();
            $gigyaMarkup.find("#loginDiv_bottomLink").detach();
            // alert($gigyaMarkup.size());
            $("#inlineDiv #gigya").html($gigyaMarkup.html());

            //resizing
            ResizeGigyaImproved("#gigya");

            $("#dummylink").trigger("click");

        }
        // alert(url);
        return false;
    });

    /*  Like function script on Artist.aspx
    =============================================================================*/
    $("body").delegate(".BntLike", "click", function() {

        var userId = readCookie('userid_');

        if (userId > 0) {
            //alert("in if");
            var url = $(this).attr("postURL");
            var param = new Array();

            param[0] = "Id :'" + $("#ctl00_CPMain_hdnContentLikeId").val() + " '"
            param[1] = "userId:'" + readCookie('userid_') + " '"
            param[2] = "tableName:'" + $("#ctl00_CPMain_hdnTableName").val() + " '"

            param.join(",");
            //alert(param);
            AjaxCall(url, param);
            $(this).addClass("disable");
        }
        else {

            var $gigyaMarkup = $("#loginDiv");
            //var html = $gigyaMarkup.html();
            $gigyaMarkup.find("#loginDiv_bottomLink").detach();
            // alert($gigyaMarkup.size());
            ResizeGigyaImproved("#gigya");
            $("#inlineDiv #gigya").html($gigyaMarkup.html());
            $("#dummylink").trigger("click");

        }
        return false;
    });

    /*  Form Artists like 
    =============================================================================*/
    $("body").delegate(".frmArtstLike", "click", function() {


        var userId = readCookie('userid_');
        if (userId > 0) {
            var url = $(this).attr("postURL");
            var param = new Array();
            param[0] = "Id :'" + $("#ctl00_CPMain_hdnContentLikeId").val() + " '"
            param[1] = "userId:'" + readCookie('userid_') + " '"
            param[2] = "tableName :'" + $("#ctl00_CPMain_hdnTableName").val() + " '"

            param.join(",");
            AjaxCall(url, param);
            $(this).addClass("disable");

        }
        else {
            //not login
            //show fancybox

            var $gigyaMarkup = $("#loginDiv");
            //var html = $gigyaMarkup.html();
            $gigyaMarkup.find("#loginDiv_bottomLink").detach();
            // alert($gigyaMarkup.size());
            $("#inlineDiv #gigya").html($gigyaMarkup.html());
            $("#dummylink").trigger("click");
        }

        return false;
    });


    /*  Like function script on Artists.aspx
    =============================================================================*/
    $("body").delegate(".artistsLike", "click", function() {

        // alert("in");
        //$(this).disabled = "disabled";
        // alert($("#ctl00_CPMain_hdnUserId").val());
        // $("#ctl00_CPMain_hdnUserId").val()
        //alert("in");

        var url = $(this).attr("postURL");
        var param = new Array();

        var userId = readCookie('userid_');
        if (userId > 0) {
            //alert(userId);

            param[0] = "Id :'" + $(this).attr("contentLikeId") + " '"
            param[1] = "userId:'" + userId + " '"
            param[2] = "tableName :'" + $(this).attr("tableName") + " '"
            param.join(",");

            //alert(param);
            $(this).addClass("disable").removeClass("artistsLike");
            AjaxCall(url, param);


        }
        else {
            //not login
            //show fancybox

            // alert("else");
            var $gigyaMarkup = $("#loginDiv");
            //var html = $gigyaMarkup.html();
            $gigyaMarkup.find("#loginDiv_bottomLink").detach();
            // alert($gigyaMarkup.size());
            $("#inlineDiv #gigya").html($gigyaMarkup.html());
            ResizeGigyaImproved("#gigya");
            $("#dummylink").trigger("click");


            // alert("out");
            //alert(document.URL);


        }

        return false;
    });


    $("body").delegate(".UnLike", "click", function() {

        //alert("in");
        //$(this).disabled = "disabled";
        // alert($("#ctl00_CPMain_hdnUserId").val());
        // $("#ctl00_CPMain_hdnUserId").val()
        //alert("in");

        var url = $(this).attr("postURL");
        var param = new Array();

        var userId = readCookie('userid_');
        if (userId > 0) {
            //alert(userId);

            param[0] = "Id :'" + $(this).attr("contentLikeId") + " '"
            param[1] = "userId:'" + userId + " '"
            param[2] = "tableName :'" + $(this).attr("tableName") + " '"
            param.join(",");

            // alert(param);
            $(this).addClass("disable").removeClass("UnLike");
            AjaxCall(url, param);

        }

        return false;
    });
    /*  Gigya share link - Click Event handler 
    //
    =============================================================================*/
    $("body").delegate(".gigyaShareLink", "click", function() {

        //        if (UGUID != -1) {
        //litShare.Text = "<a class='gigyaShareLink' commentText='my Comment' title='" + title + "'" + "desc='" + desc + "' previewImage='" + previewImage + "' actionLink='" + actionLink + "'" + ">" + " Share Me" + "</a>";
        var commentText = $(this).attr("commentText");

        var title = $(this).attr("title");
        var desc = $(this).attr("desc");
        var actionLink = $(this).attr("actionLink");
        var previewImage = $(this).attr("previewImage");
        var backLink = $(this).attr("backLink");
        //        var param = new Array();
        //        param[0] = "tableName:'" + $("#ctl00_CPMain_hdnTableName").val() + " '";
        //        param[1] = "id:'" + $("#ctl00_CPMain_hdnContentLikeId").val() + " '";
        //        param[2] = "userid:'" + readCookie('userid_') + " '";
        //        param[3] = "socialNetworkName:'" + 'event.providers' + " '";
        //        param.join(",");
        //        var url = "Video.aspx/AddShare";
        //        alert(url);
        //        AjaxCall(url, param);


        showShareUI(commentText, title, desc, backLink, actionLink, previewImage);

        //  var url = $(this).attr("postURL");
        //  var param = new Array();
        // param[0] = "title :'" + title + " '"
        //  param[1] = "desc:'" + desc + " '"
        //  param[2] = "sourcelink :'" + backLink + " '"
        //  param[3] = "videolink :'" + backLink + " '"
        //  param[4] = "shareUIimage :' '"
        // param[5] = "shareUIVideoLink :' '"

        //  param.join(",");

        // console.log($(this));
        // AjaxCall(url, param);

        //        }
        //        else {
        //            alert("please login from any social network and then share.");
        //        }
        // return false;
    });

    /* Post comments -
    ==============================================*/

    $("body").delegate(".postComment a", "click", function() {
        var guid_ = readCookie('guid_');
        //alert("social=" + UGUID);
        //alert("in");
        if (guid_) {
            $("#CommentBox").removeClass("none");
            PopulateUserInfo("commentBox");
        }

        else {
            $("#CommentBox").removeClass("none");
            //   alert("please login from any social network to post comments.");

        }
        return false;
    });


    ///Affiliate Registration

    /*  affiliate registration
    =============================================================================*/
    $("body").delegate(".XbtnRegisters", "click", function(e) {
        // alert("najam - kamran - mohib");
        return false;

    });

    $("body").delegate(".btnRegister", "click", function() {

        // alert("in");

        // return false;
        var url = $(this).attr("postURL");
        var param = new Array();
        var firstName = $("#ctl00_CPMain_txtFirstName");
        var lastName = $("#ctl00_CPMain_txtLastName");
        var userName = $("#ctl00_CPMain_txtUsername");
        var password = $("#ctl00_CPMain_txtPassword");
        var confirmPass = $("#ctl00_CPMain_txtConfirmPassword");
        var email = $("#ctl00_CPMain_txtEmail");
        var affName = $("#ctl00_CPMain_txtAffName");
        var address = $("#ctl00_CPMain_txtAddress");
        var city = $("#ctl00_CPMain_txtCity");
        var zip = $("#ctl00_CPMain_txtZip");
        var state = $("#ctl00_CPMain_txtState");
        var country = $("#ctl00_CPMain_ddlCountry");
        var contactNo = $("#ctl00_CPMain_txtPhone");
        var website = $("#ctl00_CPMain_txtWebsite");
        var confCode = $("#ctl00$CPMain$captcharAffiliateRegister");


        param[0] = " firstName:'" + firstName.val() + "'";
        param[1] = " lastName:'" + lastName.val() + "'";
        param[2] = " userName:'" + userName.val() + "'";
        param[3] = " password:'" + password.val() + "'";
        param[4] = " email:'" + email.val() + "'";
        param[5] = " affName:'" + affName.val() + "'";
        param[6] = " address:'" + address.val() + "'";
        param[7] = " city:'" + city.val() + "'";
        param[8] = " zip:'" + zip.val() + "'";
        param[9] = " state:'" + state.val() + "'";
        param[10] = " country:'" + country.val() + "'";
        param[11] = " contactNo:'" + contactNo.val() + "'";
        param[12] = " website:'" + website.val() + "'";
        param[13] = " confirmPass:'" + confirmPass.val() + "'";
        param[13] = " confirmCode:'" + confCode.val() + "'";




        param.join(",");

        AjaxCall(url, param);
        //alert(param);
        //alert("out");


        return false;
    });
    //======= Affiliate Register END ======

    /*User profile management function -  Start
    ==========================================
    */

    $("body").delegate(".UpdateProfile", "click", function() {
        //alert("in");
        //int userId, string email, string mobile, string streetAddress1, string streetAddress2, string city, string country, string zipCode, string webSite, string bio, string favQuote, string profileImage
        var userId = readCookie("userid_");

        if (userId > 0) {
            var url = $(this).attr("postURL");
            //alert(url);

            var email = $("#ctl00_CPMain_txtEmail").val();
            var mobile = $("#ctl00_CPMain_txtCellNo").val();
            var streetAddress1 = $("#ctl00_CPMain_txtAddress1").val();
            var streetAddress2 = $("#ctl00_CPMain_txtAddress2").val();
            var city = $("#ctl00_CPMain_txtCity").val();
            var country = $("#ctl00_CPMain_ddlCountry").val();
            var zipCode = $("#ctl00_CPMain_txtZipCode").val();
            var webSite = $("#ctl00_CPMain_txtWebsite").val();
            var bio = $("#ctl00_CPMain_txtBio").val();
            var favQuote = $("#ctl00_CPMain_txtFavQuote").val();
            var profileImage = " "; //$("#ctl00_CPMain_txtEmail").val();
            var gender = $("#ctl00_CPMain_ddlSex").val();
            var dateOfBirth = $("#ctl00_CPMain_ddlMonth").val() + "/" + $("#ctl00_CPMain_ddlDay").val() + "/" + $("#ctl00_CPMain_ddlYear").val();

            var webSite = $("#ctl00_CPMain_txtWebsite").val();
            var Interest = $("#ctl00_CPMain_txtInterest").val();
            var Hobby = $("#ctl00_CPMain_txtHobby").val();

            AddEditProfile(url, userId, email, mobile, streetAddress1, streetAddress2, city, country, zipCode, webSite, bio, favQuote, profileImage, gender, dateOfBirth, Interest, Hobby);

            return false;


        }
    });




});              //jquery ready ends here


function SetWebTrend() {

    //alert("in");
    var PageName = $("#ctl00_CPMain_hdnPageName").val();
    var Title = $("#ctl00_CPMain_hdnTitle").val();
    //alert(PageName);
    //alert(Title);
    dcsMultiTrack('DCS.dcsuri', PageName, 'WT.ti', Title);

}



function PageMetaData(pageTitle, metaText) {
    //alert(pageTitle);

    var PTitle = document.getElementById("ctl00_CPMain_hdnPageTitle");
    //alert(PTitle);
    try {
        if (PTitle != null) {
            document.title = PTitle.value;
            //alert(PTitle.value);
            
        }
    }
    catch (e) {
        document.title = pageTitle;
    }
}


//Populate User info from cookie
function PopulateUserInfo(formContainer) {

    var guid_ = readCookie('guid_');
    //alert(formContainer);

    if (guid_) {
        if (formContainer == "commentBox") {
            socialUserCookie();

            //$("#postComment").val(readCookie('UNAME_'))
            //Save(int userId, int mediaFileId, string Name, string Email, string Text)

            // $("#CommentBox").removeClass("none");
            //disable these boxes
            if (readCookie('UNAME_') != 0)
                $("#ctl00_CPMain_txtName").val(readCookie('UNAME_'));
            if (readCookie('UEMAIL_') != 0)
                $("#ctl00_CPMain_txtEmail").val(readCookie('UEMAIL_'));
        }
        else if (formContainer == "loginForm") {
            if (readCookie('UNAME_') != 0) {


                //                    Disbale this functionlaity due top SSL ISSUE
                $("#user").html("Welcome, <strong> <a href='users/MyCokeStudio.aspx' class='ajax current'>" + readCookie('UNAME_') + "</a></strong>");
                //$("#user").html("Welcome, <strong> " + readCookie('UNAME_') + "</strong>");
                $("#btnLogout").removeClass("none");



            }
        }

        else {
            if (readCookie('UNAME_') != 0) {

                $("#user").html("Welcome, <strong> " + readCookie('UNAME_') + "</strong>");
                $("#btnLogout").removeClass("none");
            }
        }



    }

    var uid = readCookie("userid_");
    if (uid > 0) {
        LoggedIn();
    }
    else {
        LoggOut();
    }
}

//artists like by user


function AjaxCallback(URL, arrParam, CallbackFunc) {

    //alert("in"+arrParam);
    // alert("url" + URL);

    $.ajax({
        type: "POST",
        url: URL,
        data: "{" + arrParam + "}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {
            // alert(msg.d);
            if ($("#notify").size() > 0) {
                $("#notify").html(msg.d);
            }
            CallbackFunc();
            // alert("success:" + msg.d);
            //return false;
        },
        error: function() { $("#notify").html(msg.d); return false; }

    });
}


///URL - PageName/Method Name method must be static
//function parameter
//#notify <div> set succes or fali messages
function AjaxCall(URL, arrParam) {

    $.ajax({
        type: "POST",
        url: URL,
        data: "{" + arrParam + "}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {
            // alert(msg.d);
            if ($("#notify").size() > 0) {
                try {
                    $("#notify").html(msg.d);
                } catch (e) { }
            }

            // alert("success:" + msg.d);
            //return false;
        },
        error: function() {
            try {
                $("#notify").html(msg.d);
            } catch (e) { }

            return false;
        }

    });
}


/// On fail return -1
///On Success Return
// ajaxResponse.Id
//ajaxResponse.ErrorCode
//ajaxResponse.SuccessMessage
//ajaxResponse.Exception

function AjaxCallOut(URL, arrParam) {
    // alert("in" + arrParam);
    // alert("url" + URL);

    var result;
    // alert(arrParam);
    $.ajax({
        type: "POST",
        url: URL,
        data: "{" + arrParam + "}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {
            // alert("msg.d=" + msg.d);
            var ajaxResponse = $.parseJSON(msg.d);

            //alert(ajaxResponse.Id);          
            //  alert(ajaxResponse.Exception);
            //alert(ajaxResponse);
            result = ajaxResponse;
            return result;
            //return result;
            //alert("success:" + ajaxResponse.Id);
            //return false;
        },
        error: function() {
            result = 0;
            return result;
            //return result;
        }


    });

}

/*  Social Network Login and register user to local DB
=============================================================*/
/// On fail return -1
///On Success Return
// ajaxResponse.Id
//ajaxResponse.ErrorCode
//ajaxResponse.SuccessMessage
//ajaxResponse.Exception
function LocalizeSocialUser(URL, arrParam) {

    // alert(arrParam);
    $.ajax({
        type: "POST",
        url: URL,
        data: "{" + arrParam + "}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {
            // alert("msg.d=" + msg.d);
            var ajaxResponse = $.parseJSON(msg.d);
            if (ajaxResponse.Id != "-1")
                createCookie('userid_', ajaxResponse.Id, 7);
            else
                createCookie('userid_', 0, 7);

        },
        error: function() {

            //return result;
        }
    });

}




function AddEditProfile(functionUrl, userId, email, mobile, streetAddress1, streetAddress2, city, country, zipCode, webSite, bio, favQuote, profileImage, gender, dateOfBirth, Interest, Hobby) {
    // alert("in Ad");
    var param = new Array();
    param[0] = " userId:'" + userId + " '"
    param[1] = " email:'" + email + " '"
    param[2] = " mobile:'" + mobile + " '"
    param[3] = " streetAddress1:'" + streetAddress1 + " '"
    param[4] = " streetAddress2:'" + streetAddress2 + " '"
    param[5] = " city:'" + city + " '"
    param[6] = " country:'" + country + " '"
    param[7] = " zipCode:'" + zipCode + " '"
    param[8] = " webSite:'" + webSite + " '"
    param[9] = " bio:'" + bio + " '"
    param[10] = " favQuote:'" + favQuote + " '"
    param[11] = " profileImage:'" + profileImage + " '"
    param[12] = " gender:'" + gender + " '"
    param[13] = " dateOfBirth:'" + dateOfBirth + " '"
    param[14] = " Interest:'" + Interest + " '"
    param[15] = " Hobby:'" + Hobby + " '"
    param.join(",");
    AjaxCall(functionUrl, param);

}
/*User profile management function - END
==========================================
*/



/*tes function
=======================================
*/


//Redirect page from. to www

function RedirectOnWWW() {

    var hostName = window.location.hostname;
    var newURL;
    if (hostName.toLowerCase() == "cokestudio.com.pk") {
        var tmp = document.URL;
        newURL = tmp.replace("cokestudio.com.pk", "www.cokestudio.com.pk");
        //  newURL = tmp.replace("localhost", "mabbas");
        // alert(newURL);
        window.location = newURL;
    }
}

function RedirectOnHttpToHTTPS(pageName, URL) {


    var str = "Hello world!";
   // alert(sPath.indexOf(pageName));
    var from = 0;
    var to = pageName.indexOf("?");

    var tt = pageName.substring(from, to);
   // alert("tt:" + tt);

    //alert(pageName);
    //alert(URL);
    var newURL;

    var sPath = document.URL;
    var p = sPath.indexOf(pageName);
    //alert("p:" + a.length);
    if (tt.length > 0) {
        if (IsSecurepage(tt)) {
          //  alert(p);
        }
    }




}

function IsSecurepage(Page) {

    switch (Page) {
        case "episode.aspx":
            return true;
            break;

        default:
            return false;

    }


}




// end hide -->






