var main = {

    SERVERPATH		: '',
    PHPSESSID       : '',
    data            : '',
    fadeOutTime     : 4000,

	initialize: function(serverpath,phpsessid){
            main.SERVERPATH = serverpath;
            main.PHPSESSID = phpsessid;
            // watch the loginButton,logoutButton,forgotPassword link.
            $('#login').click(main.login);
            $('#logout').click(main.logout);
            $('#forgotPassword').click(main.forgotPassword);
            $('#resetPassword').click(main.resetPassword);

            $('#searchButtonProducts').click(main.searchProducts);

            $('#deleteSearchQuery').click(main.deleteProductSearch);
            $('#menuBrandsHeader').click(main.showBrands);
            $('#menuBeautyHeader').click(main.showBeauty);

            $('.paginatorItemCountControlSelectContainer select').change(main.savePageSetting);
            $('[class^="savePageSettingText"]').click(main.savePageSettingText);

            $('#searchProduct').focus(main.clearSearchForm);
            $('#searchProduct').blur(main.refillSearchForm);

            $('[id^="brandMenuItemData"]').click(main.saveBrandSelection);

            $('[id^="selectedBrandCategory"]').click(main.saveCategoryBrandSelection);

            $('#deleteBrandSelection').click(main.deleteBrandSelection);
            $('[id^="toggleImg_"]').click(main.showHideDivSub);

            $('#signIn').click(main.toggleLoginForm);

            $('#searchProduct').keyup(function(e) {
                if(e.keyCode == 13) {
                   main.searchProducts();
                }
            });
          var originalFontSize = $('html').css('font-size');

          $('#decreaseFontSize').click(main.decreaseFontSize);
          $('#increaseFontSize').click(main.increaseFontSize);
          $('#resetFontSize').click(main.resetFontSize);

          $('span.dontDisplay').css('display','none');
          if($('#liveAmount').val() == 0) {
              $('#cartIsEmpty').css('display','inline');
          } else if($('#liveAmount').val() == 1) {
              $('#cartIsOne').css('display','inline');
          } else {
              $('#cartIsMore').css('display','inline');
          }
          
            $('#resetPasswordForm').keyup(function(e) {
                if(e.keyCode == 13) {
                   main.resetPassword();
                }
            });


          $('#dialogHolder').dialog({
                autoOpen: false,
                bgiframe: true,
                modal: true,
                buttons: {
                    Ok: function(){ $(this).dialog('close');}
                }
            });
        },

        clearSearchForm: function(){

          if($('#searchProduct').val() == 'zoeken /search'){
              $('#searchProduct').val('');
          }
        },

        refillSearchForm: function(){

          if($('#searchProduct').val() == ''){
              $('#searchProduct').val('zoeken /search');
          }
        },

        showDialog:function(message){
            message = message.replace(/\n/g,"<br />");
            $('#dialogHolder').html(message).dialog('open');
        },
        
        savePageSetting: function(){
            //var pageSetting = this.id.replace('savePageSetting','');
            var pageSetting = $(this).val();
            $.getJSON(main.SERVERPATH + 'user/savepageviewsetting/pageViewSetting/' + pageSetting + '/phpsessid/' + main.PHPSESSID + '/rnd/' + Math.random()*99999,
            '',
            main.savePageSettingResult);

        },


        savePageSettingText: function(){
            var pageSetting = $(this).attr('class').replace('savePageSettingText','');
            $.getJSON(main.SERVERPATH + 'user/savepageviewsetting/pageViewSetting/' + pageSetting + '/phpsessid/' + main.PHPSESSID + '/rnd/' + Math.random()*99999,
            '',
            main.savePageSettingResult);

        },

        savePageSettingResult: function(data){
            if(data.status == 'success'){
               window.location.reload();
            } else {

            }
        },


        changeLanguage: function(lang){

             $.ajax({
               type: "POST",
               dataType: 'json',
               url: main.SERVERPATH+'user/changelanguage/PHPSESSID/'+main.PHPSESSID,
               data: ({languageID : lang}),
               success: function(data){
                   //alert(data.url);
                   window.location = data.url;
               }
             });

        },
        
       toggleLoginForm: function(){
           
           $('#loginContainer').toggle();
           
       },

       showHideDivSub: function(){
           var showDivID = this.id.replace('toggleImg_','');
			
           $('#subItemDIV_' + showDivID).toggle('slow');
           
        if($('#toggleImg_'+ showDivID).attr('src') == main.SERVERPATH + 'public/img/plus.gif'){

            $('#toggleImg_' + showDivID).attr({'src' : main.SERVERPATH + 'public/img/min.gif'});
        } else {
            $('#toggleImg_' + showDivID).attr({'src' : main.SERVERPATH + 'public/img/plus.gif'});
        }

           
       },

        showProductDetails: function(data){
            window.location = data;
        },

       showBrands: function(){

         $('#brandMenuItemContainer').toggle('slow');
         //$('#menuCategories').hide();

       },

       showBeauty: function(){

         $('#beautyMenuItemContainer').toggle('slow');
         //$('#menuCategories').hide();

       },

       searchProducts: function(){
 		$.getJSON(main.SERVERPATH+'product/searchproduct/PHPSESSID/'+main.PHPSESSID,
					$('#searchProductForm').serialize(),
					main.searchProductsResult
				 );
       },

       searchProductsResult: function(data){

           if(data.redirectUrl != ''){
                window.location = data.redirectUrl;
           }
       },

    deleteProductSearch: function(){
        var currentAction = $('#currentAction').val();
        var currentController = $('#currentController').val();
        $.getJSON(main.SERVERPATH + 'product/deleteproductsearch/currentController/' + currentController + '/currentAction/' + currentAction + '/phpsessid/' + main.PHPSESSID + '/rnd/' + Math.random()*99999,
        '',
        main.deleteProductSearchResult);
    },

    deleteProductSearchResult: function(data){
        if(data.status == 'success'){
                    window.location = data.redirectUrl;
            }
    },

    login: function(){

		// post the serialized form to the loginAction
        // with getJSON we're expecting a JSON string as response
            $.getJSON(main.SERVERPATH+'user/login/PHPSESSID/'+main.PHPSESSID,
                                    $('#loginform').serialize(),
                                    main.loginResult);

    },

    loginResult: function(data){
        // handle the response
        if(data.status == 'success'){

            // hide the loginform
            //$('#loginFormDiv').hide();
            window.location = data.redirectUrl;
        }else{
        main.showDialog(data.message);
        /*
            $('#loginResultDiv').html(data.message);
            $('#loginResultDiv').show('slow');
            $('#loginResultDiv').fadeOut(main.fadeOutTime);*/
        }
    },

    logout: function(){
      // Send a logout request to the user controller.
      $.getJSON(main.SERVERPATH+'user/logout/PHPSESSID/'+main.PHPSESSID,'',main.logoutResult);
    },

    logoutResult: function(data){
        // Handle the logout response
       if(data.status == 'success'){
            window.location = data.redirectUrl;
       }
    },

    forgotPassword: function(){
        if($('#forgotPasswordDiv').css('display') == 'none'){
            $('#forgotPasswordDiv').slideDown('slow','');
        } else {
            $('#forgotPasswordDiv').slideUp('slow','');
        }
    },

    resetPassword: function(){

        $.getJSON(	main.SERVERPATH+'user/resetpassword/PHPSESSID/'+main.PHPSESSID,
                $('#resetPasswordForm').serialize(),
                main.resetPasswordResult
             );
    },
    resetPasswordResult: function(data){
        if(data.status == 'success'){
            $('#forgotPasswordDiv').hide('slow');
        }
            $('#forgotPasswordResultDiv').html(data.message);
            $('#forgotPasswordResultDiv').show();
    },

// This function will be removed once we go live.
    quickLogin: function(user){
       $('#userLoginUserName').attr('value',user);
       $('#userLoginPassword').attr('value','lkhdg');
       main.login();
    },

    growl:function(message,timeout)
    {
        $.blockUI({
            message: message,
            fadeIn: 700,
            fadeOut: 1700,
            timeout: timeout,
            showOverlay: false,
            centerY: false,
            css: {
                width: '350px',
                top: '10px',
                left: '',
                right: '10px',
                border: 'none',
                padding: '5px',
                backgroundColor: '#000',
                '-webkit-border-radius': '10px',
                '-moz-border-radius': '10px',
                opacity: '.6',
                color: '#fff'
            },
             overlayCSS:  {
                opacity:        '0.0'
            }
        });
    },

    saveBrandSelection: function(){
        var brandID = this.id.replace('brandMenuItemData','');
        $.getJSON(main.SERVERPATH+'brand/savebrandselection/brandID/' + brandID + '/phpsessid/' + main.PHPSESSID + '/rnd/' + Math.random()*99999,
        '',
        main.saveBrandSelectionResult);
    },

    saveBrandSelectionResult: function(data){
        if(data.status == 'success'){
             window.location = data.url;
            }
    },

    saveCategoryBrandSelection: function(){
        var categoryID = this.id.replace('selectedBrandCategory','');
        $.getJSON(main.SERVERPATH+'brand/savecategorybrandselection/categoryID/' + categoryID + '/phpsessid/' + main.PHPSESSID + '/rnd/' + Math.random()*99999,
        '',
        main.saveCategoryBrandSelectionResult);
    },

    saveCategoryBrandSelectionResult: function(data){
        if(data.status == 'success'){
             window.location = data.url;
            }
    },


    deleteBrandSelection: function(){

        $.getJSON(main.SERVERPATH+'brand/deletebrandselection/phpsessid/' + main.PHPSESSID + '/rnd/' + Math.random()*99999,
        '',
        main.deleteBrandSelectionResult);
    },

    deleteBrandSelectionResult: function(data){
        if(data.status == 'success'){
                    window.location = data.redirectUrl;
            }
    }


};