﻿//let's describe what is intended with this library
// all pages that will come from sharepoint when rendering the pages
// will be something like the following forms.

// SiteCollection/SubSite/Subsite/Pages/page.aspx
// or in the form of variation site.
// SiteCollection/Variation/Subsite/Subsite/Pages.aspx
//
// ---------------
// we need to modify all links of <a> to be of the folowing form
// 
// Full Url:= Kapsch.Net/[country]/[company]/Language/(AnyPage).aspx
//
// Assumptions:
//   1) we need to know the current site path.  
//   2) we need to know if this site is under variation by passing the variation name
// Algorithm
//   1) Remove Pages from the link  {this is a  must due to it is bad in search engines
//   2) if the site under variation 
//        2.a) get the variation name from its location
//        2.b) move it before /page.aspx [or practically speaking replace /Pages/ with Variation name
//   3) retain the full path of the current site
    



//This module is for helping the
if(!KibsiUrlReWriter) var KibsiUrlReWriter = 
{ 

    //The function will only remove /Pages from all of urls
    ProcessPageUrls : function(Variations)
    {
        //alert("looping through all links information");
        
        var links = document.getElementsByTagName('a');
    	
        for(linkIndex=0; linkIndex<links.length; linkIndex++)
        {
            
            var url = links[linkIndex].href.toLowerCase();
            
            //alert(url);
            
            
            var NotFound = "true";
            
            //loop in all variations and select 
            // the match that have the big identical charachters.
            for (var i=0; i<Variations.length; i++)
            {
                
                if(url.match(Variations[i].toLowerCase()))
                {
                    links[linkIndex].href = KibsiUrlReWriter.ProcessUrl(url, Variations[i].toLowerCase());
                    NotFound = "false";
                    break;
                }
            }
            
            if(NotFound == "true")
            {
                //modify non variated site.
                
                if (links[linkIndex].host == location.host) 
                {     
                    links[linkIndex].href = KibsiUrlReWriter.ProcessUrl(url, "");
                }
            }
            
            //alert("Converted => "+links[linkIndex].href);
        }  
    },
    
    IsVariationExist: function(url, topWebUrl)
    {
        if(topWebUrl=="") return false;
        
        if(url.match(topWebUrl))
        {
            return true;
        }
        else
        {
            return false;
        }
    },
    
    GetLabel: function(topWebUrl)
    {
    
        //get the top url until variation.
        var sidx = topWebUrl.lastIndexOf('/');
        
        
        var label = topWebUrl.substring(sidx + 1);
        
        return label;
    },
    
    RemoveVariation: function(url, topWebUrl)
    {
        //get the top url until variation.
        var sidx = topWebUrl.lastIndexOf('/');
        var newTop = topWebUrl.substring(0, sidx);
        
        
        var newUrl = url.replace(topWebUrl, newTop);
        
        //replace the url with the new cutted url.
        
        return newUrl
    },
    
    ReplacePagesInLink: function(url, label)
    {
        // check if the url contain pages/    //match() is case sensitive so add //i
        
    	if (url.match(/.+\/pages\/.+/i))
	    {
	        if(label!="" ) label=label + "/";
	        
	        //remove it
	        var newValue = url.replace(/Pages\//i, label);
	        
	        return newValue;
	        
	    }
	    else
	    {
	        return url;
	    }
    },
    
    ProcessUrl : function(url, topWebUrl)
    {    
        // the input is the current managed path
        // the url input to be modified
        
        var convertedUrl  = url;
        
        if (KibsiUrlReWriter.IsVariationExist(url, topWebUrl))
        {   
            convertedUrl = KibsiUrlReWriter.RemoveVariation(url, topWebUrl);
            
            var lbl = KibsiUrlReWriter.GetLabel(topWebUrl);    
            
            convertedUrl = KibsiUrlReWriter.ReplacePagesInLink(convertedUrl, lbl);
        }
        else
        {
            //we may have link that it is not pointing to variation.
            convertedUrl = KibsiUrlReWriter.ReplacePagesInLink(url, "");
        }
        
        return convertedUrl;
    }
};

if(!KibsiUnitTest) var KibsiUnitTest = 
{
    AreEqual : function(expected, actual)
    {
        //alert("hello");
        
        if(expected != actual) 
        {
            throw "Expected:= "+expected+" != Actual:= "+actual+"";
        }
    }  
};

