Vudu.util.formateDate = function(directorDate)	{
	
	directorDate;
	
	if(!directorDate) return null;
	
	var stringDate = ""+directorDate+"";
	 
	if(stringDate.indexOf(" ") != -1)	{
		stringDate = stringDate.substring(0,stringDate.indexOf(" "));
	}
	var dates = stringDate.split("-");
	
	if(dates.length != 3) return null;
	
	stringDate = dates[1]+'-'+dates[2]+'-'+dates[0];
	
	return stringDate;
}

Vudu.util.getMovieHomePage = function( root, title , year )        {
	
	var isSeries = false;
	
	if(title.match(/\[TV Series\]$/))	{
		title = title.replace(/\[TV Series\]$/, "");
		isSeries = true;
	}
	
	// drop all non word chars
	title = title.replace(/[^\w\s]+/g, "");
	// clean leading and ending spaces
	title = title.replace(/^\s+|\s+$/g, '') ;
	// replace all prev spaces to -
	title = title.replace(/\W+/g, "-");
	// close the title string    
	
	if(year)	{
		year = year.replace(/(\d{4})(-\d{2}-\d{2})/, '$1');
		title += "/"+year+"/";
	}
	else	{
		title += "/";
	}
	
	if(isSeries)	{
		title += "SERIES/";
	}
	
	return root+"/"+title;
}


Vudu.util.calculateParamValueFromRequest = function( oRequest , oParam )        {
	
	var sIndex = oRequest.indexOf(oParam);
	if( sIndex != -1 )      {
		return oRequest.substring(
			sIndex + oParam.length + 1 /* for = */, 
			oRequest.indexOf('&', sIndex ) != -1 ? oRequest.indexOf('&', sIndex ): oRequest.length
		);
	}
	
	return null;
}


Vudu.util.TextWrap = function( text  , charLength , splitChar , suffix )        {
       
        var sArray = text.split(splitChar);
       
        var chars = 0;
        var resultText = '';
        var lWordIndex = 0;
        var i =0 ;
        for( ; i < sArray.length; i++)    {               
			// update char count
			chars += sArray[i].length;
			// check if it exceeds desired
			if(chars > charLength)  {
				
				// if the first word is too long
				if(resultText.length <= 0 )     {
					resultText = text.substring(0,charLength);
				}
				// if add suffix 
				if(suffix)	{
					resultText += suffix;
				}
				
				break;  
			}
			// if not - append to array
			resultText += sArray[i] + splitChar;
        }
       
        return resultText;
}


Vudu.util.TextWrapWithHighlight = function( text  , words , charLength , splitChar , suffix )        {
       
        var sArray = text.split(splitChar);
        // leeps track of word starts and ends
        var pIndexes = new Array();
        // first word index is 0
        pIndexes[0] = 0;
        var wordsFound = false;
        var didWrap = false;
        var lengthExceeded = false;
        var firstEnd = 0;
        
        var chars = 0;
        var resultText = '';
        var lWordIndex = 0;
        var i =0 ;
        for( ; i < sArray.length; i++)    {               
			
			
			// check if the word is in highlight words
			for(var j=0; j < words.length; j++)	{
				
				var m1 = words[j].toLowerCase();
				var m2 = sArray[i].toLowerCase().split(/\W/);
				var haveMatch = false;
				
				if(m2.length == 1 && m1 == m2[0] ) {
					haveMatch = true;
				}
				else if(m2.length > 1 )	{
					for(var mi=0; mi < m2.length; mi++)	{
						if(m2[mi] == m1)	{
							haveMatch = true;
							break;
						}
					}
				}
			}
			
			if(haveMatch)	{
				resultText += '<span style="color:FF9900;">'+sArray[i]+'</span>'+ splitChar;
				wordsFound = true;
			}
			else	{
				// add to result text
				resultText += sArray[i] + splitChar;
			}
			
			// next position index is last position + splitChar length + current word length 
			pIndexes[pIndexes.length] = pIndexes.length > 1? 
				pIndexes[pIndexes.length-1] + splitChar.length + sArray[i].length : sArray[i].length;
			
			
			// check if it exceeds desired
			// next length for lookahead of next word
			var nextLength = (i + 1 < sArray.length)? sArray[i+1].length : 0;
			if(resultText.length + nextLength > charLength)  {
				
				// if didnt find a word yet
				if(!wordsFound)	{
					lengthExceeded = true;
					if(!firstEnd)	{
						firstEnd = resultText.length -1;
					}
				}
				// exist
				else	{
					didWrap = true;
					break;
				}  
			}
        }
       	
       	if( lengthExceeded )	{
       		
       		if(!wordsFound )	{
       			resultText =  resultText.substring(0 , firstEnd) + suffix;
       		}
       		else	{
		       	var p = 0;
		       	while( pIndexes[p]  < (resultText.length - charLength) && p < pIndexes.length )	{
		       		p++;
		       	}
		       	resultText = suffix+resultText.substring(pIndexes[p]);
			}
		}
		
		// if add suffix 
		if(suffix && didWrap )	{
			resultText += suffix;
		}
		
		return resultText;
}



Vudu.util.URLEncode = function (string) {

    string = string.replace(/\r\n/g,"\n");
    var utftext = "";

    for (var n = 0; n < string.length; n++) {

        var c = string.charCodeAt(n);

        if (c < 128) {
            utftext += String.fromCharCode(c);
        }
        else if((c > 127) && (c < 2048)) {
            utftext += String.fromCharCode((c >> 6) | 192);
            utftext += String.fromCharCode((c & 63) | 128);
        }
        else {
            utftext += String.fromCharCode((c >> 12) | 224);
            utftext += String.fromCharCode(((c >> 6) & 63) | 128);
            utftext += String.fromCharCode((c & 63) | 128);
        }
    }
    return escape(utftext);
}

Vudu.util.URLDecode = function( string)	{
	
	string = string.replace(/\r\n/g,"\n");
	var utftext = "";
	
	for (var n = 0; n < string.length; n++) {
	
	    var c = string.charCodeAt(n);
	
	    if (c < 128) {
	        utftext += String.fromCharCode(c);
	    }
	    else if((c > 127) && (c < 2048)) {
	        utftext += String.fromCharCode((c >> 6) | 192);
	        utftext += String.fromCharCode((c & 63) | 128);
	    }
	    else {
	        utftext += String.fromCharCode((c >> 12) | 224);
	        utftext += String.fromCharCode(((c >> 6) & 63) | 128);
	        utftext += String.fromCharCode((c & 63) | 128);
	    }
	}
	
	return unescape(utftext);
}

Vudu.util.parseDirectorJSONResponse = function( text ) {
    
    var results = null;
    text = text.replace(/\/\*-secure-/ , "result = ");
    text = text.replace(/\*\// , "");
            
    eval(text);
    
    return result;
}