$(function(){             
//   $('#portfoliobtn').click(function()
//   {
//        $('.sulwrapper').slideToggle('normal');
//   });

//   $('li a').click(function(event)
//   {
//        $(this).siblings('div').slideToggle('normal',function() {
//            if($('.menu').height() > $('.maininfo').height())
//            {
//                $('.maininfo').height($('.menu').height()-50);
//            }
//        });
//   });
   
   $('.swfwrapper').each(function(){
       var strSwfPath = $(this).attr('rel');
       var strField = $(this).attr('id');
       
       var flashvars = {bgcolor : '000000'};
       var params = {wmode: "transparent"};
       var swfAttr = {};
       
       $(this).parent().bind('mouseup',function(){
           location.href = $(this).attr('href');
       });
       
       if(strField == 'topswfbanner')
       {
            swfobject.embedSWF(strSwfPath, "topswfbanner", "468", "60", "9.0.0",strRootPath+"flash/expressinstall.swf", flashvars, params);
       }
       else if(strField == 'rightswfbanner')
       {
            swfobject.embedSWF(strSwfPath, "rightswfbanner", "145", "300", "9.0.0",strRootPath+"flash/expressinstall.swf", flashvars, params);
       }
   });
   
   if($('.suitebox').size()==0)
   {
       $('.mbox .maininfo').css('min-height',600);
   }
   
//   $('.oppsite').hover(
//        function() { $(this).stop().animate({'opacity':'1.0'},500)},
//        function() { $(this).stop().animate({'opacity':'0.3'},500)}
//    );
        if($('.menu').height() > $('.maininfo').height())
        {
            $('.mbox .maininfo').css('height',$('.menu').height());
        }
});

var GoogleMap = 
{
    pMap:null,
    pLatLng:null,
    nMapWidth:800,
    nMapHeight:450,

    initMap: function(nLatitude, nLongitude) 
    {  
        if(nLatitude <= 0)
        {
            nLatitude = 42;    
        }

        if(nLongitude <= 0)
        {
            nLongitude = 23;    
        }

        GoogleMap.pLatLng = new google.maps.LatLng(nLatitude, nLongitude);   
        var pOptions = {zoom: 16, center: GoogleMap.pLatLng, mapTypeId: google.maps.MapTypeId.ROADMAP};
        GoogleMap.pMap = new google.maps.Map(document.getElementById("googlemap"), pOptions);

        var marker = new google.maps.Marker({ map:GoogleMap.pMap, position:GoogleMap.pLatLng });
        
//        var marker = new google.maps.Marker({
//            position: latlng, 
//            map: map,
//            title:"Max Graphic LTD",
//            icon: 'http://www.maxgraphic.eu/images/logo_map.png'
//        });
    },

    setAddress: function(strAddress)
    {
        var pGeocoder = new google.maps.Geocoder(); 
        pGeocoder.geocode({'address': strAddress}, function(results, status) 
        {
            if (status == google.maps.GeocoderStatus.OK) 
            {
                GoogleMap.pMap.setCenter(results[0].geometry.location);
                var marker = new google.maps.Marker({map:GoogleMap.pMap, position:results[0].geometry.location});
            } 
            else 
            {
                if(DEBUG)
                {
                    alert("Geocode was not successful for the following reason: " + status);
                }
            }
        });        
    },

    addMarker: function()
    {
        // alert('addMarker');
        var marker = new google.maps.Marker({map:GoogleMap.pMap, position:GoogleMap.pLatLng});    
    },

    showPopup: function(strAddress, nLatitude, nLongitude)
    {            
        $(window).bind("resize", GoogleMap.resizeWindow); 

        $('body').append('<div class="popupbg"></div>');
        var pPopupBG = $('.popupbg');
        pPopupBG.css({'width':'100%', 'height':'100%', 'opacity': 0});

        $('body').append('<div class="popup"><div class="popupcontent" id="googlemap"></div></div>');

        var pPopup = $('.popup'); 
        pPopup.center();

        pPopupBG.animate({'opacity': 0.5}, 200, function(){
            pPopup.css({'visibility':'visible'});

            var nWidth = GoogleMap.nMapWidth - 80; 
            var nHeight = GoogleMap.nMapHeight - 80; 
            var nTop = ($(window).height() / 2) - (GoogleMap.nMapHeight / 2) + 40;
            var nLeft = ($(window).width() / 2) - (GoogleMap.nMapWidth / 2) + 40; 

            var pPopupData = $('.popupcontent'); 

            pPopup.css({'top':nTop, 'left':nLeft, 'opacity':0});
            pPopupData.css({'width':nWidth, 'height':nHeight}); 
            pPopup.animate({'top':nTop-40, 'left':nLeft-40, 'opacity':1}, 200);
            pPopupData.animate({'width':nWidth+80, 'height':nHeight+80}, 200, 
                function(){ 
                    GoogleMap.initMap(nLatitude, nLongitude);
                    if(nLatitude <= 0 && nLongitude <= 0)
                    {
                        GoogleMap.setAddress(strAddress);
                    }
                    GoogleMap.addMarker();
            });

            pPopupBG.click(function() {GoogleMap.hidePopup();});
        });
    },

    hidePopup: function() 
    {     
        $(window).unbind("resize", GoogleMap.resizeWindow); 

        var pPopupBG = $('.popupbg');
        var pPopup = $('.popup');
        var pPopupData = $('.popupcontent'); 

        var nWidth = GoogleMap.nMapWidth - 80; 
        var nHeight = GoogleMap.nMapHeight - 80; 
        var nTop = ($(window).height() / 2) - (GoogleMap.nMapHeight / 2) + 40;
        var nLeft = ($(window).width() / 2) - (GoogleMap.nMapWidth / 2) + 40; 

        $('.popupcontent :first').animate({'opacity':0}, 200, function(){ 
            pPopup.animate({'top':nTop, 'left':nLeft, 'opacity':0}, 200);
            pPopupData.animate({'width':nWidth, 'height':nHeight}, 200, function(){pPopup.remove();pPopupBG.animate({'opacity': 0}, 200, function(){pPopupBG.remove();});});      
        });
    },

    resizeWindow: function(e)
    {
        var pPopupBG = $('.popupbg');
        pPopupBG.width($(window).width());
        pPopupBG.height($(window).height());
        $('.popup').center();
    } 
}

var Guestbook = {
    send: function()
    {
        if(!CReservation.CheckField('GuestbookPersonName', '.{2,}'))
        {
            return false;
        }
        
        if(!CReservation.CheckField('GuestbookContent', '.{2,}'))
        {
            return false;
        }
        
        var strPerson = escape($('#GuestbookPersonName').val());
        var strContent = escape($('#GuestbookContent').val());
        var strSite = escape($('#SiteName').val());
        var strLang = escape($('#Language').val());
        
        if(strPerson.length > 1 && strContent.length > 1)
        {
            var strParams = 'PersonName='+strPerson+'&Content='+strContent;
            
            $.ajax({
                cache: false,
                type: 'POST',
                data: strParams,
                url: strRootPath+strSite+'/'+strLang+'/guestbook/send.html',
                success: function(data) {
                    if(data)
                    {
                        $('.postform').html(data);
                    }
                }
            })
        }
    }
};

var Popup = { 
    bLoading:false,
    nTimer:false,
    htmlContent:'',
    
    showContent: function(strContentURL) 
    {                       
        //alert(strContentURL);
        $(window).bind("resize", Popup.resizeWindow); 
        
        $('body').append('<div class="popupbg"></div>');
        var pPopupBG = $('.popupbg');
        
        //pPopupBG.width($(window).width()+"px");
        //pPopupBG.height($(window).height()+"px");
        //alert($(document).height());
        pPopupBG.css({'width':'100%', 'height':'100%', 'opacity': 0})
        
        //pPopupBG.css({'opacity': 0});
         
        $('body').append('<div id="PopupForm" class="popup"><div class="popupcontent"><img src="'+strRootPath+'images/ajax-loader.gif" /></div></div>');
        
        Popup.bLoading = true;
        Popup.nTimer = setTimeout(Popup.showLoading, 500);
        
        var pPopup = $('.popup'); 
        pPopup.center();

        $.ajax({
            cache: false,
            url: strContentURL,
            success: function(data) {  
                if(data != '')
                {           
                    Popup.htmlContent = data;   
                }
            }
        });
        
        pPopupBG.animate({'opacity': 0.5}, 200, function(){Popup.initContent();});
        
        // If the content is the reservation form, we dont add a click event handler to the background
        if(!strContentURL.match('reservation'))
        {
            pPopupBG.click(function() {Popup.hidePopup();});  
        }
    }, 
    
    initContent:function()
    {
        if(Popup.htmlContent != '')
        {
            var pPopupBG = $('.popupbg'); 
            var pPopup = $('.popup');
            
            pPopup.css({'visibility':'visible'});
            Popup.bLoading = false; 
            clearTimeout(Popup.nTimer);
             
            $('.popupcontent').html(Popup.htmlContent);
            Popup.htmlContent = '';
            
            var pPopupData = $('.popupcontent');
            pPopup.center();  

            var nPopupTop = parseInt(pPopup.css('top'));
            if(nPopupTop < 10)
            {
                pPopup.css({'top':10});
            }
            
            var nWidth = pPopupData.width() - 80;
            if(nWidth < 0) 
            {
                nWidth = 0;
                var nTop = parseInt(pPopup.css('top')) + (pPopupData.height() / 2); 
            }
            else
            {
                var nTop = parseInt(pPopup.css('top')) + 40;     
            }
            
            var nHeight = pPopupData.height() - 80; 
            if(nHeight < 0) 
            {
                nHeight = 0;
                var nLeft = parseInt(pPopup.css('left')) + (pPopupData.width() / 2);
            }
            else
            {
                var nLeft = parseInt(pPopup.css('left')) + 40;     
            }
            

            pPopup.css({'top':nTop, 'left':nLeft, 'opacity':0});
            pPopupData.css({'width':nWidth, 'height':nHeight}); 
            pPopup.animate({'top':nTop-40, 'left':nLeft-40, 'opacity':1}, 200);
            pPopupData.animate({'width':nWidth+80, 'height':nHeight+80}, 200, 
                function(){ 
                    pPopupData.css({'width':'auto', 'height':'auto'}); 
                    $('.popupcontent div:first').css({'visibility':'visible','opacity':0}); 
                    $('.popupcontent div:first').animate({'opacity':1}, 200); 
                    Popup.initDrag();
                    
                    $('.form .title .close').click(function(){Popup.hidePopup();});  
                                            
                    if(document.getElementById('GalleryUploadify') != undefined)
                    {
                        AdminGallery.init();
                    }
                    
                    if(document.getElementById('togglemetabtn') != undefined)
                    {
                        $('#togglemetabtn').click(function(){$('.metawrapper').slideToggle('normal');});
                    } 
                    
                    if(document.getElementById('banners') != undefined)
                    {
                        SpecialImages.init();
                    }
                    
                    if(document.getElementById('weditor') != undefined)
                    {
                        Editor.init();
                    }                    
                    
                    if(document.getElementById('topformswf') != undefined)
                    {
                        var strSwfPath = $('#topformswf').attr('rel');
                        
                        swfobject.embedSWF(strSwfPath, "topformswf", "468", "60", "9.0.0");
                    }                    
                    
                    if(document.getElementById('rightformswf') != undefined)
                    {
                        var strSwfPath = $('#rightformswf').attr('rel');
                        
                        swfobject.embedSWF(strSwfPath, "rightformswf", "145", "300", "9.0.0");
                    }
            });            
        }
        else
        {
            setTimeout(Popup.initContent, 50);
        }        
    },  
    
    replaceContent: function(strContentURL)
    {
        $.ajax({
            cache: false,
            url: Url.encode(strContentURL),
            success: function(data) {
                if(data != '')
                {
                    $('.popupcontent').html(data);
                    $('.popupcontent div:first').css({'visibility':'visible'}); 
                }
            }
        });
    },
    
    hidePopup: function() 
    {     
        $(window).unbind("resize", Popup.resizeWindow); 
        var pPopupBG = $('.popupbg');
        var pPopup = $('.popup');
        var pPopupData = $('.popupcontent'); 
        
        var nWidth = pPopupData.width() - 80;
        if(nWidth < 0) 
        {
            nWidth = 0;
            var nTop = parseInt(pPopup.css('top')) + (pPopupData.height() / 2); 
        }
        else
        {
            var nTop = parseInt(pPopup.css('top')) + 40;     
        }
        
        var nHeight = pPopupData.height() - 80; 
        if(nHeight < 0) 
        {
            nHeight = 0;
            var nLeft = parseInt(pPopup.css('left')) + (pPopupData.width() / 2);
        }
        else
        {
            var nLeft = parseInt(pPopup.css('left')) + 40;     
        }
        
        $('.popupcontent :first').animate({'opacity':0}, 200, function(){ 
            pPopup.animate({'top':nTop, 'left':nLeft, 'opacity':0}, 200);
            pPopupData.animate({'width':nWidth, 'height':nHeight}, 200, function(){pPopup.remove();pPopupBG.animate({'opacity': 0}, 200, function(){pPopupBG.remove();});});      
        });
    },
    
    resizeWindow: function(e)
    {
        $('.popup').center();
    },
    
    showLoading: function()
    {
        if(Popup.bLoading)
        {             
           $('.popup').css({'visibility':'visible', 'opacity':0}).animate({'opacity':1}, 500);    
        }
    },
    
    initDrag: function()
    {
        if($('.popup .title').length)    //$('.popup h2.formheader').length
        {
            $('.popup .title').bind('mousedown', function(pEvent){
                
                var pPopup = $('.popup'); 
                var nCursorStartX = pEvent.clientX + $(window).scrollLeft();
                var nCursorStartY = pEvent.clientY + $(window).scrollTop();
                var nStartLeft  = parseInt(pPopup.position().left, 10);     
                var nStartTop   = parseInt(pPopup.position().top,  10);   
                
                $(window).bind('mousemove', function(pEvent){
                    pPopup.css({'left':(nStartLeft + pEvent.clientX + $(window).scrollLeft() - nCursorStartX) + "px"});  
                    pPopup.css({'top':(nStartTop + pEvent.clientY + $(window).scrollTop() - nCursorStartY) + "px"});     
                });
                
                $(window).bind('mouseup', function(pEvent){
                    $(window).unbind('mousemove');
                    $(window).unbind('mouseup');    
                });   
            });            
        }
    }    
};

var Command = {                                     
    Execute: function(strTargetURL) 
    {     
        $.ajax({
            cache: false,
            // url: Url.encode(strTargetURL),
            url: strTargetURL,
            success: function(response) {
                if(response == 'REFRESH')
                {
                    location.reload(true);
                } 
                else if(response.match('^(http|\/).+\.html$'))
                {
                    location.href = response; 
                }
                else if(response.match('^[0-9]+'))
                {
                    $('div.galitem[rel='+response+']').parent().remove();
                }
                else
                {
                    alert(response);
                }
            }
        });
    }
};

var CReservation = {
    pErrorField : false,
    pFieldBorderColor : false,

    CheckField:function(strFieldName, strRegExpr)
    {
        pField = document.getElementById(strFieldName);    
        var invalidData = false;

        if (strRegExpr)
        {             
            if (!pField.value.match(strRegExpr))
            {          
                invalidData = true;  
            } 
        }
        else
        {
            if (pField.value === '')
            {
                invalidData = true;
            }     
        }         

        if (invalidData)
        {                                                                                                               
            CReservation.ShowError(strFieldName);
            return false;                            
        }  
        return true;            
    },

    ShowError:function(strFieldName)
    {
        if (CReservation.pErrorField)
        {
            CReservation.HideError();    
        }                 

        var pField = document.getElementById(strFieldName);   

        CReservation.pErrorField = pField;
        CReservation.pFieldBorderColor = pField.style.borderColor;
        pField.style.borderColor='#FF0000';
        pField.focus();
    },

    HideError:function()
    {
        if (CReservation.pErrorField)
        {
            CReservation.pErrorField.style.borderColor = CReservation.pFieldBorderColor; 
            CReservation.pErrorField = false;
            CReservation.pFieldBorderColor = false;
        } 
    },

    SubmitForm:function(strForm, strTargetURL)
    {                     
        pForm = document.getElementById(strForm);

        var params = "";      
        for (i=0; i<pForm.getElementsByTagName("input").length; i++) 
        {
            if (pForm.getElementsByTagName("input")[i].type == "text" || pForm.getElementsByTagName("input")[i].type == "hidden") 
            {                     
                params += pForm.getElementsByTagName("input")[i].name + "=" + escape(pForm.getElementsByTagName("input")[i].value) + "&";
            }
        }
        for (i=0; i<pForm.getElementsByTagName("textarea").length; i++) 
        {
            params += pForm.getElementsByTagName("textarea")[i].name + "=" + escape(pForm.getElementsByTagName("textarea")[i].value) + "&";
        }
        if(params != '')
        {
            params = params.substring(0, params.length-1);
        }  

        pForm.innerHTML = '<img src="' + strRootPath + 'images/ajax-loader.gif" />';   

        $.ajax({
            cache: false,
            type: "POST",
            url: strTargetURL,
            data: params,
            success: function(data){
                if(data != '')
                {
                    pForm.innerHTML = data;
                }    
            }
        });

        return false; 
    },

    CheckForm:function()
    {
        if(!CReservation.CheckField('ComeDate', /[0-9]{2}\.[0-9]{2}\.[0-9]{4}/))
        {
            return false;
        } 
        if(!CReservation.CheckField('GoDate', /[0-9]{2}\.[0-9]{2}\.[0-9]{4}/))
        {
            return false;
        } 
        if(!CReservation.CheckField('SenderName', '.{2,}'))
        {
            return false;
        }                    
        if (!CReservation.CheckField('Phone', '[0-9 \+\(\)]{6,}'))
        {
            return false;
        }
        if (!CReservation.CheckField('Email', '.{3,}@.{3,}\..{2,}'))
        {
            return false;
        }
        
        $('#RoomTypeHidden').val($('#RoomType option:selected').val());

        CReservation.SubmitForm('ReservationForm', $('#ReservationForm').attr('action'));
        return false; 
    }
}

