﻿//--- Design & Code by VietVo - vietvo.btr@gmail.com 

var dsReqAjax = null;
var dsReqAction = 0;
function ds_makeReqAjax(URL, val) {
    oURL = URL.toString();
    if (oURL.indexOf('?') > -1)
        URL += '&dsReqAjax=' + new Date().getTime();
    else
        URL += '?dsReqAjax=' + new Date().getTime();

    ds_preloadReqAjax(val);

    if (window.XMLHttpRequest) {
        dsReqAjax = new XMLHttpRequest();
        dsReqAjax.onreadystatechange = ds_processReqAjax;
        dsReqAjax.open("GET", URL, true);
        dsReqAjax.send(null);
    } else if (window.ActiveXObject) {
        dsReqAjax = new ActiveXObject("Microsoft.XMLHTTP");
        if (dsReqAjax) {
            dsReqAjax.onreadystatechange = ds_processReqAjax;
            dsReqAjax.open("GET", URL, true);
            dsReqAjax.send();
        }
    }
}
function ds_preloadReqAjax(val) {
    dsReqAction = val;
    str = '<img src="/Images/loading.gif" align="center" valign="middle" border="0"/>&nbsp;Loading...';
    switch (dsReqAction) {       
        case 97: //getIdol
            break;             
        default: alert('Check your code, please ! - ' + window.location); break;
    }
}
function ds_processReqAjax() {
    if (dsReqAjax.readyState == 4) {
        if (dsReqAjax.status == 200) {
            switch (dsReqAction) {
                case 97:
                    xmlDoc = dsReqAjax.responseXML;
                    errorcode = parseInt(xmlDoc.getElementsByTagName("ErrorCode")[0].childNodes[0].nodeValue, 10);
                    if (errorcode == -1) {
                        alert(xmlDoc.getElementsByTagName("Message")[0].childNodes[0].nodeValue);
                        window.location = '/';
                    } else if (errorcode == 1) {
                        jQuery('#jcrop_idol').html('<img id="jcrop_idol_img" src="' + xmlDoc.getElementsByTagName("Path")[0].childNodes[0].nodeValue + '"/>');
                        jQuery('#jcrop_idol').html('');
                        widthOriginal = parseInt(xmlDoc.getElementsByTagName("Width")[0].childNodes[0].nodeValue);
                        heightOriginal = parseInt(xmlDoc.getElementsByTagName("Height")[0].childNodes[0].nodeValue);

                        heightSHOW = 500;
                        widthSHOW = 800;
                        widthSETSELECT_MINIMUM = 800;
                        heightSETSELECT_MINIMUM = 600;

                        wiIMG_Fix = widthOriginal;
                        heIMG_Fix = heightOriginal;

                        if (wiIMG_Fix < widthSETSELECT_MINIMUM || heIMG_Fix < heightSETSELECT_MINIMUM) {
                            alert('Chất lượng hình không đạt yêu cầu (' + widthSETSELECT_MINIMUM + 'x' + heightSETSELECT_MINIMUM + ').');
                            window.location.reload();
                        }

                        if (wiIMG_Fix > widthSHOW) {
                            tmp = wiIMG_Fix;
                            wiIMG_Fix = widthSHOW;
                            heIMG_Fix = parseInt(wiIMG_Fix * heIMG_Fix / tmp);
                        }

                        if (heIMG_Fix > heightSHOW) {
                            tmp = heIMG_Fix;
                            heIMG_Fix = heightSHOW;
                            wiIMG_Fix = parseInt(wiIMG_Fix * heIMG_Fix / tmp);
                        }

                        jQuery.fancybox(
                            '<div style="border:1px solid #bcbcbc;height=' + (heIMG_Fix + 40) + '"><img width="' + wiIMG_Fix + '" height="' + heIMG_Fix + '" id="jcrop_idol_img" src="' + xmlDoc.getElementsByTagName("Path")[0].childNodes[0].nodeValue + '"/><input type="button" value="Hoàn thành" onclick="jQuery.fancybox.close();"/></div>',
                            {
                                'overlayShow': true,
                                'overlayColor': '#000',
                                'width': wiIMG_Fix,
                                'height': (heIMG_Fix + 40),
                                'autoScale': true,
                                'overlayOpacity': 0.8,
                                'hideOnOverlayClick': false,
                                'scrolling': 'no',
                                'padding': 10,
                                'margin': 0,
                                'autoDimensions': false,
                                'wmode': 'transparent',
                                'onClosed': function () {
                                    jQuery('#jcrop_idol').hide();
                                }

                            });

                        setResIdol(parseFloat(widthOriginal / wiIMG_Fix), parseFloat(heightOriginal / heIMG_Fix));

                        wii = parseInt(wiIMG_Fix * widthSETSELECT_MINIMUM / widthOriginal);
                        hee = parseInt(heIMG_Fix * heightSETSELECT_MINIMUM / heightOriginal);
                        jQuery('#t_idol').val(" " + xmlDoc.getElementsByTagName("FileName")[0].childNodes[0].nodeValue);
                        //alert(xmlDoc.getElementsByTagName("TimeShot")[0].childNodes[0].nodeValue);
                      //  try {
                            jQuery('.ubf-photo-shottime').val(xmlDoc.getElementsByTagName("TimeShot")[0].childNodes[0].nodeValue);
                            jQuery('.ubf-photo-camera').val(xmlDoc.getElementsByTagName("Camera")[0].childNodes[0].nodeValue);
                            jQuery('.ubf-photo-speed').val(xmlDoc.getElementsByTagName("Speed")[0].childNodes[0].nodeValue);
                            jQuery('.ubf-photo-ISO').val(xmlDoc.getElementsByTagName("ISO")[0].childNodes[0].nodeValue);
                            jQuery('.ubf-photo-aperture').val(xmlDoc.getElementsByTagName("Aperture")[0].childNodes[0].nodeValue);
                            jQuery('.ubf-photo-focal').val(xmlDoc.getElementsByTagName("Focal")[0].childNodes[0].nodeValue);
                       // } catch (ex) { };


                        jQuery('#jcrop_idol').attr('style', 'width:602px;height:auto');
                        jQuery('#jcrop_idol_img').Jcrop({
                            setSelect: [0, 0, wii, hee],
                            allowResize: true,
                            // aspectRatio: wii / hee,
                            minSize: [wii, hee],
                            maxSize: [wiIMG_Fix, heIMG_Fix],
                            onChange: setCropIdol,
                            onSelect: setCropIdol,
                            onRelease: setCropIdol
                        });
                    }
                    break;

                default: alert('Check your code, please ! - ' + window.location); break;
            }
        }
    }
}
