// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults


/*
// Safe Cross-sub-domain scripting between frames
if (location.href.indexOf(parent.document_domain) > 0) {
	try {document.domain = parent.document_domain;}
	catch (e) {alert(e.message)};
}
*/

//reinitialiseScrollPane
$(function()
{
    reinitialiseScrollPane = function()
    {
        $('#scrolly').jScrollPane();
    }

});


/* Commented MLR, nout used in inbox, are confliting with facebook and orkut read mesages
//toggle functions used on email inbox page

$(document).ready(function()
{
  //hide the all of the element with class msg_body
  $(".msg_body").hide();
  //toggle the componenet with class msg_body
  $(".msg_head").click(function()
  {
    $(this).next(".msg_body").slideToggle(600, reinitialiseScrollPane);
	$(this).hide();
  });

	$(".msg_close").click(function()
	{
		 $(this).parent().hide();
		 $(this).parent().prev(".msg_head").show();
		 reinitialiseScrollPane();
	}
	)
	
	
});
*/

function proxy_call_parent(parent_base_url,call){
	//alert('update_selection_test');
	frontHolderDiv=document.getElementById("frontHolder");

		src = parent_base_url + 'proxy.html?call=' + escape(call);
		
		var newElem = document.createElement('iframe');
		newElem.src = src;
		newElem.id="proxy_iframe";
		frontHolderDiv.appendChild(newElem);
}

function callback_link(path) {
	url=path
	url += "?parent=";
	url += escape(get_parent_url()) ;
	return url;
}

function get_parent_url() {
	parent_url = window.parent.location.href
	if(parent_url.indexOf('?')>-1) {
		parent_url = parent_url.substring(0,(parent_url.indexOf('?')-1) );
	}
	return parent_url;
}

function update_modal_size(size) {
	if (size=='small') {
		parent.update_modal_size(size);
	}
	else {
		parent.update_modal_size();
	}
}
/**
 * Timezone Detect and Ignore Daylight Saving Time (DST)
 * @author Michael Khalili
 * @author Julio Vedovatto <julio@smartlabsf.com>
 * @see http://www.michaelapproved.com/articles/timezone-detect-and-ignore-daylight-saving-time-dst/
 * @version 20090927
 * @version 20100506
 * return integer
 */
function TimezoneDetect(){
    var dtDate = new Date('1/1/' + (new Date()).getUTCFullYear()),
    	intOffset = 10000, //set initial offset high so it is adjusted on the first attempt
    	intMonth, intHoursUtc, intHours, intDaysMultiplyBy;

    //go through each month to find the lowest offset to account for DST
    for (intMonth=0;intMonth < 12;intMonth++){
        //go to the next month
        dtDate.setUTCMonth(dtDate.getUTCMonth() + 1);

        //To ignore daylight saving time look for the lowest offset.
        //Since, during DST, the clock moves forward, it'll be a bigger number.
        if (intOffset > (dtDate.getTimezoneOffset() * (-1)))
            intOffset = (dtDate.getTimezoneOffset() * (-1));
    }

    return intOffset;
}
/**
 * set cookie for timezone of user
 * @author Julio Vedovatto <julio@smartlabsf.com>
 */
function setCookieTimezone() {
	document.cookie = "TZ=" + TimezoneDetect();
}
/**
 * Replace URL by html links
 * @author madlep
 * @author Julio Vedovatto <julio@smartlabsf.com>
 * @see http://stackoverflow.com/questions/37684/replace-url-with-html-links-javascript
 * @version 20100506
 * @param string text
 * @param string target (optional)
 * @return string
 */
function replaceURLWithHTMLLinks(text, target) {
  return text.replace(/(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig,'<a href="$1"' + (target ? ' target="'+ target + '"' : '') + '>$1</a>'); 
}
/** 
 * Format twitter message, putting links and hash links
 * @author Julio Vedovatto <julio@smartlabsf.com>
 * @param string text
 */
function twitter_text_format(text){
	//make links
	return replaceURLWithHTMLLinks(text, '_blank')
				.replace(/[\@]+([A-Za-z0-9-_]+)/ig,'<a href="http://twitter.com/$1" target="_blank" class="profile-link">@$1</a>')
				.replace(/[\#]+([A-Za-z0-9-_]+)/ig,'<a href="http://search.twitter.com/search?q=%23$1" target="_blank" class="hash-link">#$1</a>'); 
}