var s='staging';
function expand(id,subcatID) {
	childNodeArray = document.getElementById('menuElementsID').getElementsByTagName('*')

	for(i=0;i<childNodeArray.length;i++) {
		if(childNodeArray[i].className=="expand") {
			document.getElementById(childNodeArray[i].id).className='colapse'
		}
//
		if(childNodeArray[i].className=="Subcateg") {
			document.getElementById(childNodeArray[i].id).className='hSubcateg'
		}
	}

	categElement = document.getElementById(id)
	categElement.className = 'expand';

	subcategs = document.getElementById(subcatID)
	subcategs.className='Subcateg'
}

function setCookie(c_name,value,expiredays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate()+expiredays);
document.cookie=c_name+ "=" +escape(value)+
((expiredays==null) ? "" : ";expires="+exdate.toGMTString()) + '; path=/;';
}

function getCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1)
    {
    c_start=c_start + c_name.length+1;
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length;
    return unescape(document.cookie.substring(c_start,c_end));
    }
  }
return "";
}

function changeFontSize(element,size, imgid)
{
	//step = parseInt(step,10);
	el = document.getElementById(element);
	el.style.fontSize= size + 'px';
	//alert(el.style.fontSize);
	//curFont = parseInt(el.style.fontSize,10);
	//el.style.fontSize = (curFont+step) + 'px';
	for(i=1;i<=3;i++)
	{
		elname='font' + i;
		el=document.getElementById(elname);
		el.src='/cms/images/' + i + '_off.png';
	}
	elname='font' + imgid;
	el=document.getElementById(elname);
	el.src='/cms/images/' + imgid + '_on.png';
	setCookie('fontSize', size, 356);
	setCookie('fontSizeImg', imgid, 356);
}

// USED FOR FILES
function requireFormAccessPopup(fileUrl, fileId, fromHomepage) {
    if(fromHomepage == null) fromHomepage = false;
    var formDiv = document.getElementById('show-access-form-' + fileId);
    document.getElementById('file-url-field').value = fileUrl;
    var backgroundDiv = document.getElementById('background-div-effect-' + fileId);
    backgroundDiv.style.display = 'block';
    setDivLeftMargin(fileId);
    if(fromHomepage) goToTheAnchor('top');   
}

function goToTheAnchor(anchorName) {
   var sPath = window.location.pathname;
   var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
   location.href = sPage+"#"+anchorName
}

function requireFormAccessPopup2(fileUrl,insightId) {
	document.getElementById('fileId').value = insightId;
	document.getElementById('file-url-field').value = fileUrl;
    var formDiv = document.getElementById('show-access-form');
    var backgroundDiv = document.getElementById('background-div-effect');
    backgroundDiv.style.display = 'block';
    setDivLeftMargin();
}

function requireFormAccessPopup3(fileUrl, fileId) {
    var formDiv = document.getElementById('show-access-form-' + fileId);
    document.getElementById('file-url-field').value = fileUrl;
    var backgroundDiv = document.getElementById('background-div-effect-' + fileId);
    backgroundDiv.style.display = 'block';
    setDivLeftMargin(fileId);
    goToTheAnchor('top');   
}

function setDivLeftMargin(fileId) {
    var firstDiv = (fileId != null && fileId != 'undefined') ? document.getElementById('background-div-effect-' + fileId) : document.getElementById('background-div-effect');
    var totalW;
    var wi = 0;
    var firstDivW;
    if(firstDiv != null) {
        totalW = firstDiv.offsetParent.offsetWidth;
        firstDivW = firstDiv.offsetWidth;
        wi = parseInt((totalW - firstDivW)/2);
        firstDiv.style.left = wi + 'px';
    }
}

function setFieldProps(errors, fieldName) {
    var msgArray = '';
    var extra = '';
    var field = document.getElementById(fieldName);
    if(errors.length > 0) {
        field.setAttribute('class', 'empty-field');
        for(i=0; i < errors.length; i++) {
            if(errors[i] == 'isEmpty') {
                msgArray += '<b>' + fieldName.substring(7, fieldName.length) + '</b> is mandatory.<br />';
            }
            if (errors[i] == 'emailAddressInvalid' || errors[i] == 'emailAddressInvalidHostname' ||
                errors[i] == 'hostnameUnknownTld' || errors[i] == 'hostnameLocalNameNotAllowed'
                ) {
                extra = 'Invalid <b>email address</b>.<br />';
            }
        }
        msgArray += extra;
    } else {
        field.setAttribute('class', '');
    }
    return msgArray;
}

function sendAccessFormData(elem) {
    document.getElementById('submitData').disabled = true;
    elem.submit();
    setTimeout('showFinalMessage()', 1000);
}

// USED FOR FILES
function sendAccessFormDataFiles(elem) {
    document.getElementById('submitData').disabled = true;
    var fileId = '';
    for(var i=0; i < elem.elements.length; i++){
        if(elem.elements[i].name == 'fileId') {
            fileId = elem.elements[i].value;
            continue;
        }
    }
    elem.submit();
    setTimeout('showFinalMessage(' + fileId + ')', 1000);
}

function showFinalMessage(fileId){
    var myIFrame    = document.getElementById('accessFormSubmit');
    var content     = myIFrame.contentWindow.document.body.innerHTML;
    var json        = eval('(' + content + ')');
    var errorsDiv   = (fileId == null || fileId == 'undefined') ? document.getElementById('form-access-errors') : document.getElementById('form-access-errors-' + fileId);
    var msg         = '';

    if(!json['success']) {
        var errors = json['errors'];
        msg += setFieldProps(errors['visitorName'], 'visitorName');
        msg += setFieldProps(errors['visitorCompany'], 'visitorCompany');
        msg += setFieldProps(errors['visitorEmail'], 'visitorEmail');
        msg += setFieldProps(errors['visitorPhone'], 'visitorPhone');
        if(msg.length > 0) {
            errorsDiv.innerHTML = msg;
        }
        document.getElementById('submitData').disabled = false;
    } else {
        var fileUrl = document.getElementById('file-url-field').value;
        var cleanErrors = [];
        setFieldProps(cleanErrors, 'visitorName');
        setFieldProps(cleanErrors, 'visitorCompany');
        setFieldProps(cleanErrors, 'visitorEmail');
        setFieldProps(cleanErrors, 'visitorPhone');
        var myDiv = (fileId == null || fileId == 'undefined') ? document.getElementById('form-access-errors') : document.getElementById('form-access-errors-' + fileId);
        myDiv.innerHTML = 'Your download is about to start...';
        myDiv.setAttribute('class', 'download-notification');
	//pageTracker._trackPageview(fileUrl);
        //window.location = fileUrl;
        window.open(fileUrl, 'kkmk2');

        setTimeout('closeAccessFormDiv(' + fileId + ')', 3000);
    }
}

function closeAccessFormDiv(fileId) {
    // we should reset the form and hide the errors
    var errorsDiv   = (fileId == null || fileId == 'undefined') ? document.getElementById('form-access-errors') : document.getElementById('form-access-errors-' + fileId);
    errorsDiv.innerHTML = '';
    var myForm = document.getElementById('request-access-form');
    myForm.reset();

    var myDiv = (fileId != null && fileId != 'undefined') ? document.getElementById('background-div-effect-' + fileId) : document.getElementById('background-div-effect');
    if(myDiv != null) { 
        myDiv.style.display = 'none';
    }
}

var divX;
var divY;
function openFeatureView(id, typeOfFeature, type) {
    var myDiv = document.getElementById('show-feature-view-' + typeOfFeature + '-' + type + '-' + id);

    if (myDiv != null) {

        document.onmousedown = getMouseCoords;
        // show the div
        myDiv.style.display = 'block';
        var myPadding   = 10;
        // setting how much to get to left/right the div
        // divX is the x-coordinate of the mouse click
        var myDivW      = myDiv.offsetWidth;
        var totalW      = 0;
        var contentW    = 0;
        if(myDiv.offsetParent != null) {
            totalW      = myDiv.offsetParent.offsetWidth;
        }
        var mainDiv = getElementsByClassName('main');
        if(mainDiv != null) {
            contentW    = mainDiv[0].offsetWidth;
        }
        // the margine from content to edge of browser
        var pageMargine = (parseInt(totalW) - parseInt(contentW)) / 2;
        // distance to left edge of content
        var rez = parseInt(divX) - parseInt(pageMargine) + parseInt(myPadding);
        var rightMargine = ( parseInt(contentW) + parseInt(pageMargine)) - parseInt(divX);
        // test if the div can be displayed in the content (not to the right edge)
        if( rightMargine < myDivW ) {
            // "move" the div to left
            rez -= parseInt(myDivW) + parseInt(myPadding);
        }
        myDiv.style.marginLeft = parseInt(rez) + 'px';

        // set the margin bottom of the displaying info div
        var myDivH      = myDiv.offsetHeight;
//        var totalH      = 0;
//        var contentH    = 0;
//        if(myDiv.offsetParent != null) {
//            totalH      = myDiv.offsetParent.offsetHeight;
//        }
//        if(mainDiv != null) {
//            contentH    = mainDiv[0].offsetHeight;
//        }
        var temp        = getWindowSize();
        var winH        =  temp[1];
        rez = 0;

        if( (parseInt(winH) - parseInt(divY)) < parseInt(myDivH)) {
            if(parseInt(divY) > parseInt(myDivH)) {
                rez = parseInt(myDivH);// + parseInt(myPadding);
            }
        }

        myDiv.style.marginTop = -parseInt(rez) + 'px';
    }
}

function getWindowSize() {
  var myWidth = 0, myHeight = 0;
  var rez = new Array();
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  rez[0]    = myWidth;
  rez[1]    = myHeight;
  return rez;
}

function getMouseCoords(event) {
    var ev = event || window.event;
    divX = ev.clientX;
    divY = ev.clientY;
}

function closeFeatureView(id, typeOfFeature, type) {
    var myDiv = document.getElementById('show-feature-view-' + typeOfFeature + '-' + type + '-' + id);
    if (myDiv != null) {
        myDiv.style.display = 'none';
    }
}

function downloadFeatureFile(path) {
    window.location = path;
}

function closeAccessRequestForm(fileId) { 
    // we should reset the form and hide the errors
    var errorsDiv   = (fileId == null || fileId == 'undefined') ? document.getElementById('form-access-errors') : document.getElementById('form-access-errors-' + fileId);
    errorsDiv.innerHTML = '';
    var myForm = document.getElementById('request-access-form-' + fileId);
    myForm.reset();
    var myDiv = (fileId != null && fileId != 'undefined') ? document.getElementById('background-div-effect-' + fileId) : document.getElementById('background-div-effect');
    if (myDiv != null) { 
        myDiv.style.display = 'none';
    }
}
function getElementsByClassName(classname, node) {
    if(!node) node = document.getElementsByTagName("body")[0];
    var a = [];
    var re = new RegExp('\\b' + classname + '\\b');
    var els = node.getElementsByTagName("*");
    for(var i=0,j=els.length; i<j; i++)
        if(re.test(els[i].className))a.push(els[i]);
    return a;
}

function resizeIframe()
{
//    f = document.getElementById('inframe');
//    if (f == null) {
//        return;
//    }

    
    try {
        f = document.getElementById('inframe');
        // iframe src is currently local

        if (f.contentWindow != null && f.contentWindow.document != null && f.contentWindow.document.body != null) {
            if (typeof(window.innerWidth) == 'number') {
                //IE9 or Chrom or FF	
				var iframe_content_height = $('#inframe').contents().find('div#special-event-content').height();

				if(iframe_content_height == null || iframe_content_height == 0)
					// maybe in listing area
					iframe_content_height = $('#inframe').contents().find('div#grid_container').height();
					
				if	(iframe_content_height == null || iframe_content_height == 0)
					iframe_content_height = $('#inframe').content().find('div.contact-form-holder').height();
				
				if(iframe_content_height == null || iframe_content_height == 0)
					iframe_content_height = $('#inframe').contents().find('body').height();

					
				var iframe_height = $('#inframe').height();
				if (iframe_height  < iframe_content_height) {
                    $('#inframe').height(iframe_content_height + 5)
                } else if(iframe_height > iframe_content_height + 5 ) {
					$('#inframe').height(iframe_content_height + 5);
				}
            } else {
			// IE8 and IE7
                if (f.height < f.contentWindow.document.body.scrollHeight) {
                    f.height = f.contentWindow.document.body.scrollHeight + 5;
                } else if (f.height > f.contentWindow.document.body.scrollHeight+5) {
                    f.height = f.contentWindow.document.body.scrollHeight + 5;
                }
            }
        }
    } catch (e) { 
        return;
    }
}

window.setInterval("resizeIframe()", 500);

/*function checkIframe()
{
    timer = 0;
    while (!loaded && timer < 10) {
        f = document.getElementById('inframe');
        if (f == null || f.contentWindow.document.body == null) {
            timer += 1;
            window.setTimeout('checkIframe()', 500);
        } else {
            loaded = true;
            //window.setInterval('resizeIframe()', 50);
        }
    }
}

var loaded = false;
*///checkIframe();

function showSearchResults(q) {
    var searchIframe = document.getElementById('searchIframe');
    searchIframe.setAttribute('src', 'http://66.170.16.150/search?q=' + q);
    searchIframe.style.display = 'block';
}


function insightsGridDisplay(id) {
    setTimeout('updateInsightsGrid()', 1000);
}

function updateInsightsGrid(){
    //alert('fuck you!~');
    var insightsGrid = Ext.getCmp('grid_container');
    insightsGrid.body.getUpdater().load({url:'/cms/features/insights/getlist/pageId/' + id});
}
