﻿var CurrentPictureId;
var CurrentRating = 0;

var latitude = 0;
var longitude = 0;

var map = null;
var geocoder = null;
var latsgn = 1;
var lgsgn = 1;
var zm = 0;
var marker = null;
var posset = 0;

var ClsPicture = function () { };

var Picture = new ClsPicture();


jQuery(document).ready(function() {

    pic1 = new Image(16, 16);
    pic1.src = "/system/images/star_yellow.png";

    pic2 = new Image(16, 16);
    pic2.src = "/system/images/star_frame.png";

    jQuery('#ctl00_ctl00_Content_Content_imgPicture').maxImage({
        position: 'relative',
        maxFollows: 'width',
        horizontalOffset: 470,
        verticalOffset: 291,
        maxAtOrigImageSize: true
    });

    setInterval(function() {

        var imgWidth = parseInt(jQuery('#ctl00_ctl00_Content_Content_imgPicture').css("width").replace('px', ''));

        if (!isNaN(imgWidth)) {
            jQuery('#ctl00_ctl00_Content_Content_divDetails').css('width', imgWidth + 0);
        };

    }, 100);

});

function ResizeDetailsPanel() {


}

function RatingHover(rating) {

    jQuery('.rating').empty();

    switch (parseInt(rating)){

        case 1:

            jQuery('#divRating1').css('background', 'url(/system/images/star_yellow.png)');
            jQuery('#divRating2').css('background', 'url(/system/images/star_frame.png)');
            jQuery('#divRating3').css('background', 'url(/system/images/star_frame.png)');
            jQuery('#divRating4').css('background', 'url(/system/images/star_frame.png)');
            jQuery('#divRating5').css('background', 'url(/system/images/star_frame.png)');

            break;

        case 2:

            jQuery('#divRating1').css('background', 'url(/system/images/star_yellow.png)');
            jQuery('#divRating2').css('background', 'url(/system/images/star_yellow.png)');
            jQuery('#divRating3').css('background', 'url(/system/images/star_frame.png)');
            jQuery('#divRating4').css('background', 'url(/system/images/star_frame.png)');
            jQuery('#divRating5').css('background', 'url(/system/images/star_frame.png)');

            break;

        case 3:

            jQuery('#divRating1').css('background', 'url(/system/images/star_yellow.png)');
            jQuery('#divRating2').css('background', 'url(/system/images/star_yellow.png)');
            jQuery('#divRating3').css('background', 'url(/system/images/star_yellow.png)');
            jQuery('#divRating4').css('background', 'url(/system/images/star_frame.png)');
            jQuery('#divRating5').css('background', 'url(/system/images/star_frame.png)');

            break;

        case 4:

            jQuery('#divRating1').css('background', 'url(/system/images/star_yellow.png)');
            jQuery('#divRating2').css('background', 'url(/system/images/star_yellow.png)');
            jQuery('#divRating3').css('background', 'url(/system/images/star_yellow.png)');
            jQuery('#divRating4').css('background', 'url(/system/images/star_yellow.png)');
            jQuery('#divRating5').css('background', 'url(/system/images/star_frame.png)');

            break;

        case 5:

            jQuery('#divRating1').css('background', 'url(/system/images/star_yellow.png)');
            jQuery('#divRating2').css('background', 'url(/system/images/star_yellow.png)');
            jQuery('#divRating3').css('background', 'url(/system/images/star_yellow.png)');
            jQuery('#divRating4').css('background', 'url(/system/images/star_yellow.png)');
            jQuery('#divRating5').css('background', 'url(/system/images/star_yellow.png)');

            break;    
    }

}

function RatingMouseOut() {

    jQuery('#divRating1').css('background', 'url(/system/images/star_frame.png)');
    jQuery('#divRating2').css('background', 'url(/system/images/star_frame.png)');
    jQuery('#divRating3').css('background', 'url(/system/images/star_frame.png)');
    jQuery('#divRating4').css('background', 'url(/system/images/star_frame.png)');
    jQuery('#divRating5').css('background', 'url(/system/images/star_frame.png)');

    if (CurrentRating > 0) {
        RatingHover(CurrentRating);
    }

}

function RatingSet(rating) {

    if (CookieRead('pic_' + CurrentPictureId + '_rated')=='true') {
        DisplayError('', Youhavealreadyratedthispicture, CetrineNet);
        RatingMouseOut();
        return;
    }

    var WS = new WsGalleries();
    WsGalleries.set_path('/WS/Galleries.asmx');
    
    WS.SetRating(CurrentPictureId, rating,
        function(args) {

            CookieCreate('pic_' + CurrentPictureId + '_rated', 'true', 356);
            CookieCreate('pic_' + CurrentPictureId, args, 1);
            jQuery('#ctl00_ctl00_Content_Content_hfRating').val(args);
            RatingHover(args);

        },

         function(error, userContext, methodName) {
            if (error) DisplayError('image.RatingSet','id: ' + CurrentPictureId + '| ' + error.get_message());
         }
     ); 
     
}

ClsPicture.prototype.GetLocationName = function (Lat, Lon) {

    latitude = Lat;
    longitude = Lon;

    var WS = new WsGalleries();
    WsGalleries.set_path('/WS/Galleries.asmx');

    WS.GetGpsLocationName(Lat, Lon,
        function (args) {

            jQuery('#ctl00_ctl00_Content_Content_trLocation').show();
            jQuery('#ctl00_ctl00_Content_Content_lblLocation').html(nearLocation.replace('$1', args));

            jQuery('#ctl00_ctl00_Content_Content_hlShowOnTheMap').show();

        },
        function (error, userContext, methodName) {
            if (error) DisplayError('image.GetLocationName', 'id: ' + CurrentPictureId + '| ' + error.get_message());
        }
     );

    }

ClsPicture.prototype.GetLocationCoordinates = function (placeName) {

        var WS = new WsGalleries();
        WsGalleries.set_path('/WS/Galleries.asmx');

        WS.GetCoordinatesByPlaceName(placeName,
        function (args) {

            var coordinates=args.split(',');

            latitude = coordinates[0];
            longitude = coordinates[1];

            jQuery('#ctl00_ctl00_Content_Content_trLocation').show();
            jQuery('#ctl00_ctl00_Content_Content_hlShowOnTheMap').show();

        },
        function (error, userContext, methodName) {
            if (error) DisplayError('image.GetLocationName', 'id: ' + CurrentPictureId + '| ' + error.get_message());
        }
     );

}

ClsPicture.prototype.ShowMap = function () {

    jQuery('#tblGoogleMap').slideToggle();

    if (GBrowserIsCompatible()) {

        map = new GMap2(document.getElementById("divGoogleMap"));
        map.setMapType(G_NORMAL_MAP);
        map.addControl(new GLargeMapControl());
        map.addControl(new MapTypeControl());
        map.addControl(new GScaleControl());
        map.enableScrollWheelZoom();
        geocoder = new GClientGeocoder();
        
        var point = new GLatLng(latitude, longitude);
        map.setCenter(point, 13);

        var baseIcon = new GIcon();
        baseIcon.iconSize = new GSize(32, 32);
        baseIcon.iconAnchor = new GPoint(16, 32);
        baseIcon.infoWindowAnchor = new GPoint(16, 0);
        var thisicon = new GIcon(baseIcon, "http://itouchmap.com/i/blue-dot.png", null, "http://itouchmap.com/i/msmarker.shadow.png");

        var marker = new GMarker(point, thisicon);

        map.addOverlay(marker);

    };
}
