/**
 *  Access javascript from Flash
 *  Triggers Arnostats counter
 *  @param  string  identifier
 *  @script "javascript:ArnostatDelegate(identifier)"
 */
function ArnostatDelegate(identifier)
{
	var lc_site = "fancy.nl";
    var lc_page = "\"" + identifier + "\"";
    var lc_pagevariant = "caroussel";
    var lc_campaign = escape(document.referrer);
	lc_visit(lc_site, lc_page, lc_pagevariant, lc_campaign);
}

/**
 * Simple function which resize an iframe-element to completely show its
 * contents in order to avoid scrollbars
 *
 * Usage:
 *  - load this script within the page where the iframe resides
 *  - within the iframecontent put insert:
 *      function adjustIFrameSize() {
 *          if (parent.adjustIFrameSize) {
 *              parent.adjustIFrameSize(window, 25);
 *          }
 *      }
 *  - call the function at body onload within the iframecontent
 */
function adjustIFrameSize (iframeWindow, extra) {
	if (iframeWindow.document.height) {
		var iframeElement = document.getElementById(iframeWindow.name);
		iframeElement.style.height = iframeWindow.document.height + 'px';
		//iframeElement.style.width = iframeWindow.document.width + 'px';
	} else if (document.all) {
		var iframeElement = document.all[iframeWindow.name];
		if (iframeWindow.document.compatMode && iframeWindow.document.compatMode != 'BackCompat')
		{
			iframeElement.style.height =
			iframeWindow.document.documentElement.scrollHeight + extra + 'px';
			//iframeElement.style.width =
			//iframeWindow.document.documentElement.scrollWidth + extra + 'px';
		} else {
			iframeElement.style.height =
			iframeWindow.document.body.scrollHeight + extra + 'px';
			//iframeElement.style.width =
			//iframeWindow.document.body.scrollWidth + extra + 'px';
		}
	}
}

function setForumBreadcrumb (breadcrumbsrequest) {
    new Ajax.Request(
        breadcrumbsrequest,
        {
            method: 'get',
            onSuccess: function (transport) {
                var response = transport.responseText || '';
                var breadcrumbs = document.getElementById('breadcrumbs');
                breadcrumbs.innerHTML = response;
            }
        }
    );
}

function preview_browse(url, pag)
{
    new Ajax.Request(url,
    {
        method:'get',
        parameters: { p_pag: pag },
        onSuccess: function(transport){
            var response = transport.responseText || "no response text";
            var to_show = $('the_preview');
            to_show.innerHTML = response;
            to_show.style.display = 'block';
        },
        onFailure: function(){ }
    });
}

//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
function getPageSize(){

    var xScroll, yScroll;

    if (window.innerHeight && window.scrollMaxY) {
        xScroll = document.body.scrollWidth;
        yScroll = window.innerHeight + window.scrollMaxY;
    } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
        xScroll = document.body.scrollWidth;
        yScroll = document.body.scrollHeight;
    } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
        xScroll = document.body.offsetWidth;
        yScroll = document.body.offsetHeight;
    }

    var windowWidth, windowHeight;
    if (self.innerHeight) { // all except Explorer
        windowWidth = self.innerWidth;
        windowHeight = self.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
        windowWidth = document.documentElement.clientWidth;
        windowHeight = document.documentElement.clientHeight;
    } else if (document.body) { // other Explorers
        windowWidth = document.body.clientWidth;
        windowHeight = document.body.clientHeight;
    }

    // for small pages with total height less then height of the viewport
    if(yScroll < windowHeight){
        pageHeight = windowHeight;
    } else {
        pageHeight = yScroll;
    }

    // for small pages with total width less then width of the viewport
    if(xScroll < windowWidth){
        pageWidth = windowWidth;
    } else {
        pageWidth = xScroll;
    }

    arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight)
    return arrayPageSize;
}

function show_preview()
{
    $('the_preview').style.display = 'block';

    pageSize = getPageSize();
    $('overlay').style.height = pageSize[1] + 'px';
    $('overlay').style.display = 'block';
}

function hide_preview()
{
    $('the_preview').style.display = 'none';
    $('overlay').style.display = 'none';
}

function clearValueAtFocus(e,a)
{
    if( (e.value == a) ) {
        e.value = '';
    }
}

function setValueAtBlur(e,a)
{
    if( e.value == '' ) {
        e.value = a;
    }
}

function toggle_search_frm(id) {
    if ( id == 'archive_tab' ) {
        $('archive_tab').style.display = 'block';
        $('search_tab').style.display = 'none';
    } else if ( id == 'search_tab' ) {
        $('search_tab').style.display = 'block';
        $('archive_tab').style.display = 'none';
    }
}

function lc_void()
{
    return true;
}

function lc_handle(url)
{
    var url = 'http://stats.ilsemedia.nl/click.gif' + url;

    if (document.images) {
        var lc_img = new Image(1,1);
        lc_img.src = url;
        lc_img.onload = function () {
                lc_void();
        }
    } else {
            document.write('<img src="' + url + '" width="1" height="1" alt="">');
    }
}

function lc_click(lc_s, lc_p, lc_pv, lc_c, lc_l)
{
    var lc_url='?a=c&s='+lc_s+'&p='+lc_p+'&pv='+lc_pv+'&c='+lc_c+'&l='+escape(lc_l)+'&t='+(new Date()).getTime();
    lc_handle(lc_url);
}

function lc_visit(lc_s, lc_p, lc_pv, lc_c)
{
    var lc_url='?a=v&s='+lc_s+'&p='+lc_p+'&pv='+lc_pv+'&c='+lc_c+'&t='+(new Date()).getTime();
    var ref=document.referrer;

    ref=(ref.lastIndexOf('/')==ref.length-1)?ref.substring(ref.lastIndexOf('/'),0):ref;

    if(ref.length>0)
        lc_url+='&r='+escape(ref);

    lc_handle(lc_url);
}

/**
 * link stats, Arnostats and STIR for external links
 *
 * @param string    $page
 * @param string    $url
 */
function linkStats(page, url)
{
	// ARNOSTATS
	var lc_site = "fancy.nl";
	var lc_pagevariant = "";
	var lc_campaign = escape(document.referrer);

	lc_visit(lc_site, page, lc_pagevariant, lc_campaign);

	// STIR
	sitestat(url);
}

function linkurl(el, page) 
{ 
    if (!el.href.match(/^http:\/\/jmp\.ilsemedia\.nl/)) { 
        el.href = 'http://jmp.ilsemedia.nl/?s=fancy.nl&p=' + page + '&l=' + encodeURIComponent(el.href); 
    } 
    return true; 
}
