
Home = function() {
        
    // Cargo los scripts de GMaps
    function mapLoadScript()
    {
        var script = document.createElement("script");
        script.type = "text/javascript";
        script.src = "http://maps.google.com/maps/api/js?sensor=false&language=es&region=AR&callback=Home.map";
        document.body.appendChild(script);
    }
    
    function staticMap()
	{
        var str_url = 'http://maps.google.com/maps/api/staticmap?';
        var params = [];
        params.push('maptype=roadmap');
        params.push('sensor=false');
        params.push('size=225x160');
        if(mapMarkers.length > 0) {
            for(var i = 0; i < mapMarkers.length; i++) {
                var markerData = mapMarkers[i];
                params.push('markers=' + markerData.map_lat + ',' + markerData.map_lng);
            }
        }else{
            params.push('center=Argentina');
        }
		
        var str_url_full = str_url + params.join('&');
        if(str_url_full.length > 2048)
		{
			// Lo armo a mano incluyendo los que se puedan hasta 2048 caracteres en total:
			for(var i = 0; i < params.length; i++)
			{		
				var temp = str_url + params[i] + "&";		
		        if(temp.length >= 2048)
					break;
				
				str_url += params[i] + "&";
			}

			str_url_full = str_url;
		}

        var img = '<img class="map-location" src="'+str_url_full+'" />';
        $('.cupon-detalle-mapa').prepend(img).css('cursor','pointer').click(function(){
            $.colorbox({
                iframe:true,
                href:'/assets/tpl/home/map-iframe.php',
                innerWidth:'80%',
                innerHeight:'80%'
            });
        });
        $('.cupon-detalle-mapa').show();
    }

    function _verifyProfileUpdate(email, token) {
        $.ajax({
            url: Ebowe.ajaxRequestPath('User_Ui','profileUpdateBlock'),
            dataType: 'html',
            data:{
                email: email,
                token:token
            },
            type:'POST',
            success: function(html) {
                if(html) {
                    $.colorbox({
                        html:html,
                        onComplete:_profileUpdateForm,
                        onCleanup:function(){
                            $('#profile-update-form').validationEngine('hideAll');
                            $('#profile-update-form').validationEngine('detach');
                        }
                    });
                }
            }
        });
    }

    function _profileUpdateForm() {
        $('#profile-update-form').ajaxForm({
            url: Ebowe.ajaxRequestPath('User_Ui','updateProfile'),
            type: 'POST',
            dataType: 'json',
            beforeSubmit: function(arr, jqForm, options) {
                if($('#profile-update-form').validationEngine('validate')) {
                    Ebowe.block(true, '#profile-update-form');
                    return true;
                }
                return false;
            },
            success: function(response) {
                Ebowe.block(false, '#profile-update-form');
                $.colorbox.close();
            },
            error: function() {
                Ebowe.block(false, '#profile-update-form');
            }
        });

        var $city = $('#profile-update-form').find('input[name=city]');
        var $state = $('#profile-update-form').find('select[name=state_id]');

        $city.autocomplete(
            Ebowe.ajaxRequestPath('User_Ui','searchCity'), {
                width: 300,
                matchContains: true,
                cacheLength: 1,
                extraParams:{
                    state_id: function(){
                        return $state.val();
                    }
                }
            });

        $state.change(function(){
            $city.flushCache();
        });

        $('#profile-update-form').validationEngine({scroll:false});		
    }
    
    function _modalNewsletterForm()
    {    
        var $form = $('#welcome-form');
        if( $form.length > 0 ) {
            $form.ajaxForm({
                url: Ebowe.ajaxRequestPath('User_Ui','modalNewsletterRegister'),
                type: 'POST',
                dataType: 'json',
                beforeSubmit: function(arr, jqForm, options) {
                    if($form.validationEngine('validate')) {
                        Ebowe.block(true, '#welcomeModal');
                        return true;
                    }                        
                    return false;
                },
                success: function(response) {
                    if(response.success) {
                        Ebowe.redir('/');
                    }                    
                    else {
                        Ebowe.block(false, '#welcomeModal');
                        // TODO: Mensaje de error
                    }   
                },
                error: function() {
                    //TODO: Handle error
                    Ebowe.block(false, '#welcomeModal');
                }
            });            
            
            //$form.submit();
        }        
    }
    
    return {
        init:function() {
            Common.timer();
            //mapLoadScript();
            staticMap();
			
			// Wolcome Modal Close trigger
			$(".welcomeModalClose").live('click', function(e) 
			{
				e.preventDefault();				
                                $("#welcomeMain").validationEngine('hideAll');
				$('#welcomeModal').fadeOut(800, function()
				{
					  $('body').css('overflow','auto');            
                      $('html').css('overflow-y','scroll');
					  //$("#welcomeModal").remove();
                      $("#welcomeMain").remove();
					  return false;
				});
			});			
        },
        map: function ()
        {
            if(mapMarkers.length > 0)
            {
                var myOptions = {
                    zoom: 8,
                    mapTypeId: google.maps.MapTypeId.ROADMAP
                };
                var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
                    
                bounds = new google.maps.LatLngBounds();
                    
                var markers = [];
                for (var i = 0; i < mapMarkers.length; ++i) {
                    var latlng = new google.maps.LatLng(mapMarkers[i].map_lat, mapMarkers[i].map_lng);
                    var marker = new google.maps.Marker({
                        position: latlng,
                        map: map,
                        title: mapMarkers[i].name
                    });
                    bounds.extend(latlng);
                }
                      
                map.setCenter(bounds.getCenter());
                map.fitBounds(bounds);
                      
            //$('.cupon-detalle-mapa').show();
            }
            else if(parseInt(requireAddress) > 0)
            {
                var myLatlng = new google.maps.LatLng(-38.341656,-63.28125);
                var myOptions = {
                    zoom: 2,
                    center: myLatlng,
                    mapTypeId: google.maps.MapTypeId.ROADMAP
                };
                var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
            //$('.cupon-detalle-mapa').show();
            }
        },
        verifyProfileUpdate: _verifyProfileUpdate,
        profileUpdateForm: _profileUpdateForm,
		modalCode123: function()
		{
			$.colorbox({
				html: '<img src="/assets/img/modales/modalCode123.jpg" width="700" height="313" alt="" />',
				opacity: 0.5,
				onComplete: function(){				
					code123Timer = setTimeout(function(){$.colorbox.close()}, 20000);
				},
				onCleanup: function(){
					clearTimeout(code123Timer);
				}
			});
		},
		welcomeModal: function()
		{
			$('#welcomeModal').fadeIn('fast', function()
            {
                $('html').css('overflow-y','hidden');
                $('body').css('overflow','hidden');  
                $("html").scrollTop();
                $("#welcome-form").validationEngine({scroll: false});
                
                _modalNewsletterForm();                 
                
                $('.trigger-modal-registro').live('click', function(event)
                {
                    event.preventDefault();
                    $("#welcome-form").submit();
                });                  
            });                       
                
		}
    };
}();
