﻿Type.registerNamespace('PhotoSite');
//Custom Calendar variable
var selected_ThemeID;
var selected_Month;
var selected_Size;
var ThemeList = new Object();
var currTheme;
var pleaseSelectStartingMonth = "- select -";




function GetMonthsDDLDateCustomCal() {
    PageMethods.PopulateMonthYearDDL(CreateMonthsDDL_CustomCal, gotError);
}

function CreateMonthsDDL_CustomCal(result) {
    if (result) {
        var MonthsList = new Object();
        MonthsList = result;
        var ddl_place = document.getElementById("ddl_starting_month");

        //    var divEl = document.createElement("div");
        var ddlEl = document.createElement("select");
      
        if (ddl_place != null) {
            ddl_place.innerHTML = "";
            var newOption0 = document.createElement('option');
            newOption0.value = 0;
            newOption0.text = pleaseSelectStartingMonth;
            if (Sys.Browser.agent == Sys.Browser.InternetExplorer) {
                ddlEl.add(newOption0);
            }
            else {
                ddlEl.add(newOption0, null);
            }

            for (var i = 0; i < MonthsList.length; i++) {
                var newOption = document.createElement('option');
                newOption.value = MonthsList[i].MonthValue;
                newOption.text = String.format('{0}', MonthsList[i].MonthText);
                if (Sys.Browser.agent == Sys.Browser.InternetExplorer) {
                    ddlEl.add(newOption);
                }
                else {
                    ddlEl.add(newOption, null);
                }
            }
        }

        ddlEl[0].selected = true;
        selected_Month = ddlEl[0].value;
        $addHandler(ddlEl, 'change', ChangeStartingMonth);
        //    divEl.appendChild(ddlEl);
        ddl_place.appendChild(ddlEl);

    }
    //start with 3 step 
    PageMethods.GetThemes(CreateThemesCustomCal, gotError);
}

function ChangeStartingMonth(e) {
    selected_Month = e.target.value;
}

function CreateThemesCustomCal(result) {
    if (result)
    {
   
        var img_preview = $get("img_theme_preview");
        selected_ThemeID = 0;
        var ThList = result;
        var select = false;
        var m = 0;
        for (var i in ThList) {
            select = (m == 0) ? true : false;
            var Th = ThList[i];

            if (typeof (Th) != 'function')
            {
                m++;
                var NewTheme = new PhotoSite.Theme(Th.ThemeID, Th.ThemeName, Th.ThumbPath, Th.ImgPreview, select);
                if (NewTheme._selected) {
                    selected_ThemeID = NewTheme._id;
                    img_preview.src = NewTheme._preview;
                }
                ThemeList[NewTheme._id] = NewTheme;
            }
        }


        if(selected_ThemeID > 0)
            ThemeList[selected_ThemeID]._selected = true;
            
        LoadThemesCustomCal();

    }
}

function LoadThemesCustomCal() {
    var theme_list = $get("calendar_style");
    var themeListHtml = "";
    for (var i in ThemeList) {
        currTheme = ThemeList[i];
        themeListHtml += currTheme.thumb_view();
    }
    theme_list.innerHTML = themeListHtml;

}


function CreateCalendarInfo(result) {
    if (result) {
        var calenArray = Sys.Serialization.JavaScriptSerializer.deserialize(result);
        CalendarCount = calenArray.length;
        var img_preview = $get("img_themePreview");
        var name_list = $get("calendar_list");
        name_list.innerHTML = "";
        var HasSelected = false;
        var FirstCalendarEncID = "";
        var BackStepCalEncID = "";
        if (CalEncRPID != undefined && CalEncRPID != "") {
            BackStepCalEncID = CalEncRPID;
        }
        var break_img = document.createElement("img");
        break_img.src = "../images/dotted_line_horiz2.gif";
        for (i = 0; i < calenArray.length; ++i) {
            select = (i == 0) ? true : false;
            var Cal = calenArray[i];
            if (Cal.EncryptedID == null || typeof(Cal.EncryptedID)=='undefinded') {
                
            }
            var CalendObj = new PhotoSite.Calendar(i, Cal.EncryptedID, Cal.Name, Cal.Price, Cal.ShortDescription, Cal.LargeGraphicUrl, select);
            CalendObj._queryparam = Cal.QueryParam;
            CalendObj._selected = Cal.IsSelected;
         
            if (CalendObj._selected) {
                selectedEncCalendarID = CalendObj._encryptedID;
                selectedQueryParam = CalendObj._queryparam;
                selectedCalendarID = CalendObj._id
                img_preview.src = CalendObj._themeurl;
                HasSelected = true;
            }
            CalendarsList[CalendObj._id] = CalendObj;
            if (i == 0 ) {
                FirstCalendarID = CalendObj._encryptedID;
            }

        }
        if (BackStepCalEncID !="") {
            SelectTheme(BackStepCalEncID);
        }
        else if (!HasSelected) {
            SelectTheme(FirstCalendarID);
        }
        else {
            SelectTheme(selectedEncCalendarID);
        }
    }
}

function SelectTheme_CustomCal(theme_id) {
    var img_preview = $get("img_theme_preview");

    for (var i in ThemeList) {
        CurrTheme = ThemeList[i];
        if (i == theme_id) {
            selected_ThemeID = theme_id;
            CurrTheme._selected = true;
            img_preview.src = CurrTheme._preview;

        }
        else {
            CurrTheme._selected = false;
        }


    }
    LoadThemesCustomCal();

}

function SelectTheme(encCal_id) {
    if (CalendarsList != null) {
        var name_list = $get("calendar_list");
        var img_preview = $get("img_themePreview");
        name_list.innerHTML = "";
        var break_img = document.createElement("img");
        break_img.src = "../images/dotted_line_horiz2.gif";
        var i = 0;
        var cal_id = 0;
        for (var n in CalendarsList){
            var curCal=CalendarsList[n];
            if (curCal._encryptedID == encCal_id) {
                curCal._selected = true;
                selectdEncCalendarID = curCal._encryptedID;
                selectedQueryParam = curCal._queryparam;
                selectedCalendarID = curCal._id;
                cal_id = selectedCalendarID
            }
            else {
                curCal._selected = false;
            }
            name_list.innerHTML += curCal.list_view();
            if (i != (CalendarCount - 1)) {

                name_list.appendChild(break_img);
            }
        }
    }
    img_preview.src = CalendarsList[cal_id]._themeurl;
}


function RedirectToNextStep() {
    PageMethods.CheckIfAlbumExists(('../album/' + selectedQueryParam),RedirectToNextStep_CallBack);
}

function RedirectToNextStep_CallBack(result) {
    if (result == "True") {
        location.href = selectedQueryParam;
    }
    else {
        location.href = "../upload/upload.aspx?p=url_set";
    }
}

function RedirectForCustomCal(result) {
    if (result) {
        location.href = result;
    }
   
}

function LoadThemesCustomCal() {
    var theme_list = $get("calendar_style");
    var themeListHtml = "";
    for (var i in ThemeList) {
        currTheme = ThemeList[i];
        themeListHtml += currTheme.thumb_view();
    }
    theme_list.innerHTML = themeListHtml;

}

function SetUpBorder(object) {
    var theme_img = $get(object.id);
    //theme_img.border = "4";
    //theme_img.style.borderColor = "#FFC565";
    theme_img.style.cursor = "pointer";
    theme_img.style.border = "2px solid #FFC565";
}
function GetOutBorder(object) {

    var theme_img = $get(object.id);
    //theme_img.border = "2";
    //theme_img.bordercolor = 'white';

    theme_img.style.border = "2px solid white";
}

function CreateCalendarProject() {

    if (selected_Month == 0) {
        setLyrCentered3(this, 'popup_select_start_month',448,350,null);
    }
    else {
        var encprojectid = "";
        projectService.CreateCalendarProject(currEncUserID, encprojectid,selected_ThemeID, selected_Month, encSelectedrpovID, IsHasSavedDates, gotError);
    }
}

function Redirect(result) {
  
}

function gotError(ex, ctx, methodName) {
    if (ex != null) {
        var errMsg = ex.get_message();
        var stkTrace = ex.get_stackTrace();
        var errType = ex.get_exceptionType();
        alert('{"Message":"' + errMsg + '","StackTrace":"' + stkTrace + '","ExceptionType":"' + errType + '","MethodName":"' + methodName + '"}');
        // get_statusCode(), get_timedOut()            
    }
    document.location = "../error.aspx";
}


function ChangeCurrentMonth(month_id) {
    if (MonthsList != undefined) {
        var htmlMarkup = new Sys.StringBuilder();
        var CurrentAppertureID = "";
        for (var n in MonthsList) {
           
            if (MonthsList[n]._id == month_id) {
                MonthsList[n]._isSelected = true;
                SelectedMonthObjecID = MonthsList[n]._id
                CurrentAppertureID = MonthsList[n]._apertureIDOfMonth;
            }
            else {
                MonthsList[n]._isSelected = false;
            }
            
            htmlMarkup.append(MonthsList[n].months_view());
        }
        var months = $get("months_objects");
        months.innerHTML = "";
        months.innerHTML = htmlMarkup.toString();
        ShowCurrentTemplate(CurrentAppertureID);
    }
}


 
 
PhotoSite.MonthsOptionsObject = function(id) {
    this._id = id;
    this._index = 0;
    this._name = "";
    this._isSelected = false;
    this._hasBeenPreviewed = false;
    this._isImageSelected = false;
    this._mediaUrl = "";
    this._mediaIDOfMonth = 0;
    this._apertureIDOfMonth = 0;
    this._apertureUrl = "";
};

PhotoSite.MonthsOptionsObject.prototype = {
    months_view: function() {
        var htmlMarkup = new Sys.StringBuilder();
        var imgSrc = this._isImageSelected ? "../images/album/img_check.gif" : "../images/album/img_uncheck.gif"
        var selected = this._isSelected ? "color:Black" : "";
        htmlMarkup.append("<div style=\"text-align:left;float:left;width:150px;padding-bottom:10px;\" border=\"0\">");
        htmlMarkup.append("            <div style=\"width:14px;float:left\">");
        htmlMarkup.append("                    <img src='" + imgSrc + "' alt=\"\"  />");
        htmlMarkup.append("             </div>");
        htmlMarkup.append("             <div style=\"cursor:pointer;"+selected+"; \" onclick=\"ChangeCurrentMonth(" + this._id + ")\">");
        htmlMarkup.append(this._name);
        htmlMarkup.append("             </div>");
        htmlMarkup.append(" </div>");
        return htmlMarkup.toString();
    }
};


PhotoSite.Calendar = function(id, encryptedid, name, price, shortdescription, themeurl, selected) {
    this._id = id;
    this._encryptedID = encryptedid;
    this._name = name;
    this._price = price;
    this._shortdescription = shortdescription;
    this._themeurl = themeurl;
    this._selected = selected;
    this._queryparam = "";
};
PhotoSite.Calendar.prototype = {
    list_view: function() {
//        var selected = this._selected ? 'checked' : '';

        if (this._selected) {
            return "<p ><a href=\"#\"  onclick=\"SelectTheme('" + this._encryptedID + "')\" style=\"cursor:pointer;\" >" +
                                   "<strong>"+ this._name + " - " + this._price + "</strong>" +
                                   "<img src=\"../images/icon_side_arrow.gif\" border=\"0\" />" +
                                   "</a>" + "</p>";
        }
        else {

            return "<p ><a href=\"#\"  onclick=\"SelectTheme('" + this._encryptedID + "')\" style=\"cursor:pointer;\" >" +
                                   this._name + " - " + this._price + 
                                   "</a>" + "</p>";
        }
    }
};

PhotoSite.Theme = function(id, name, thumb_path, img_preview, selected) {

    this._id = id;
    this._name = name;
    this._thumb_path = thumb_path;
    this._preview = img_preview;
    this._selected = selected;
};
PhotoSite.Theme.prototype = {
    thumb_view: function() {
        var selected = this._selected ? 'checked' : '';

        if (this._selected) {
            return "<div id=\"cal_theme\" onclick=\"SelectTheme_CustomCal(" + this._id + ")\" style=\"cursor:pointer;width:142px;float:left;\" >" +
                                   "<table border=\"0\" width=\"140\" align=\"center\" ><tr height='90px'><td align=\"center\" >" +
                                   "<img id=\"img_" + this._id + "\" border=\"2\" style=\"borderColor:'#FFC565';width:136px;cursor:'pointer';border='2px solid #FFC565';\" src=\"" + this._thumb_path + "\" />" +
                                   "</td></tr><tr><td align=\"center\" style='font-weight:bold;font-family:arial'>" +
                                   this._name +
                                   "</td></tr></table>" +
                                   "</div>";
        }
        else {

            return "<div id=\"cal_theme\"   onclick=\"SelectTheme_CustomCal(" + this._id + ")\" style=\"cursor:pointer;width:142px;float:left;\" >" +
                                   "<table border=\"0\" width=\"140\" align=\"center\" ><tr height='90px'><td align=\"center\" >" +
                                   "<img id=\"img_" + this._id + "\" src=\"" + this._thumb_path + "\" style=\"width:136px;border:solid 2px white;\" onmouseover=\"SetUpBorder(this)\" onmouseout=\"GetOutBorder(this)\" />" +
                                   "</td></tr><tr><td align=\"center\" style='font-weight:bold;font-family:arial'>" +
                                   this._name +
                                   "</td></tr></table>" +
                                   "</div>";
        }
    }
};


PhotoSite.MonthsOptionsObject.registerClass('PhotoSite.MonthsOptionsObject');
PhotoSite.Calendar.registerClass('PhotoSite.Calendar');
PhotoSite.Theme.registerClass('PhotoSite.Theme');
Sys.Application.notifyScriptLoaded();