/* djguide.js */

var strAgent = navigator.userAgent.toLowerCase(); 
var ord=Math.random()*10000000000000000; 	// nodig voor dart, geframede pagina's zonder kop 
cLanguage = Get_Cookie( 'language' );
var cStaticPathSite = 'http://www.presale.nl';
var cDomain='http://www.presale.nl';	

var timeDiff  =  {
    setStartTime:function (){
        d = new Date();
        time  = d.getTime();
    },

    getDiff:function (){
        d = new Date();
        return (d.getTime()-time);
    }
}




function Set_Cookie( name, value, expires, path, domain, secure ) 
{
// set time, it's in milliseconds
var today = new Date();
today.setTime( today.getTime() );

/*
if the expires variable is set, make the correct 
expires time, the current script below will set 
it for x number of days, to make it for hours, 
delete * 24, for minutes, delete * 60 * 24
*/
if ( expires )
{
expires = expires * 1000 * 60 * 60 * 24;
}
var expires_date = new Date( today.getTime() + (expires) );

document.cookie = name + "=" +escape( value ) +
( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
( ( path ) ? ";path=" + path : "" ) + 
( ( domain ) ? ";domain=" + domain : "" ) +
( ( secure ) ? ";secure" : "" );



}

// http://techpatterns.com/downloads/javascript_cookies.php
// this fixes an issue with the old method, ambiguous values
// with this test document.cookie.indexOf( name + "=" );
function Get_Cookie( check_name ) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f

	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );


		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}



// this deletes the cookie when called
function Delete_Cookie( name, path, domain ) {
if ( Get_Cookie( name ) ) document.cookie = name + "=" +
( ( path ) ? ";path=" + path : "") +
( ( domain ) ? ";domain=" + domain : "" ) +
";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}
			
// http://www.codeproject.com/KB/scripting/replace_url_in_ajax_chat.aspx
function chat_string_create_urls(input)
{
    return input
    .replace(/(ftp|http|https|file):\/\/[\S]+(\b|$)/gim,
'<a href="$&" target="_blank">$&</a>')
    .replace(/([^\/])(www[\S]+(\b|$))/gim,
'$1<a href="http://$2" target="_blank">$2</a>');
}

// http://www.hunlock.com/blogs/Howto_Dynamically_Insert_Javascript_And_CSS
function loadExternalScript( cScriptUrl, cExecuteAfterLoad  )
{ // insert script dynamicaly into head, needed for ajax 
  lScriptAlreadyInserted = false;

  
   $("head script" ).each(function(i)
     { if ( $(this).attr( "src") == cScriptUrl ) 
     	{ lScriptAlreadyInserted = true;
     	  return false; // exit each
     	}
     });   
  
/**
  if (lScriptAlreadyInserted == false) 
  { alert( '<script src="' + cScriptUrl + '" type="text/javascript" ></script>' );
  	 $('head').append('<script src="' + cScriptUrl + '" type="text/javascript" ></script>' );
  }
**/

  if (lScriptAlreadyInserted == false) 
  {   var headID = document.getElementsByTagName("head")[0];         
      var newScript = document.createElement('script');
      newScript.type = 'text/javascript';
      
      
      if (cExecuteAfterLoad != "" )
      { // alert ( 'Execute after load:' + cExecuteAfterLoad ); 

        setTimeout( cExecuteAfterLoad ,500);
      	// newScript.onload=eval(cExecuteAfterLoad) ;
      }
      
      newScript.src =  cScriptUrl;
      headID.appendChild(newScript);

      
  }
  else eval( cExecuteAfterLoad );
       
 } 




function viewhide( id ) {
 
 el = document.getElementById(id);
 var nextSibStatus = (el.style.display == 'none') ? 'block' : 'none';
 el.style.display = nextSibStatus;

}

function stopbubble( e )
{
  // stop onclick bubbling
  var e = e || window.event;  
  e.cancelBubble = true;
  if (e.stopPropagation) e.stopPropagation();
}


function ShowMenu(div_id) {
    

    el = document.getElementById(div_id);
    

    pijltje_id = 'pijltje_' + div_id;   


    
    if (el.style.display == 'block')
    {
      el.style.display = 'none';
      document.getElementById(pijltje_id).src='http://www.presale.nl/image/formulier/pijltje_rechts.gif';
      document.getElementById(pijltje_id).alt='>';
      
      
      
      Set_Cookie( div_id, 'none', 60 , '/', '', '' ) 
      
      
    }
    else
    {  el.style.display = 'block';
       document.getElementById(pijltje_id).src='http://www.presale.nl/image/formulier/pijltje_beneden.gif';
       document.getElementById(pijltje_id).alt='v';


       Set_Cookie( div_id, 'block', 60 , '/', '', '' ) 
    }

    
    return false; // return false is nodig ivm aanroep via href
}



function OpenMenu( div_id )
{

     
    el = document.getElementById(div_id);
    pijltje_id = 'pijltje_' + div_id;   

    if (Get_Cookie( div_id )  == 'none' )
    {
      el.style.display = 'none';
      document.getElementById(pijltje_id).src='http://www.presale.nl/image/formulier/pijltje_rechts.gif';
      document.getElementById(pijltje_id).alt='>';
       
    }
    else
    {
      el.style.display = 'block';
      document.getElementById(pijltje_id).src='http://www.presale.nl/image/formulier/pijltje_beneden.gif';
      document.getElementById(pijltje_id).alt='v';
    
    }
    return false; // return false is nodig ivm aanroep via href

}





function handleAltskyscraper(response) 
{
 el = document.getElementById( 'rechtsbuiten' );
 el.innerHTML = response;
}

function handleAltcentraalbovenin(response) 
{
 
 if ( response.length > 156 )
 {el = document.getElementById( 'centraalbovenin' );
  el.innerHTML = response;
 } 
}





function handleEigenMenu(response) 
{

 el = document.getElementById( 'contentEigenMenu' );

 if (el.style.display == 'block')
 { el.style.display = 'none';
 
   el = document.getElementById( 'bannerprofielinfo');
   if (el != undefined)
   {
   	el.style.display = 'block';
   }

   el = document.getElementById( 'rechtsbuiten');
   if (el != undefined)
   {
   	el.style.display = 'block';
   }

   return;
 }  
 
 el.innerHTML = response;
 el.style.display = 'block';
 
 // om flash hidden te maken, kan ik helaas niet overeenkomen omdat ik de transparant param niet kan zetten van extern geladen flash en z-index niet werkt
 el = document.getElementById( 'bannerprofielinfo');
 if (el != undefined)
 {
   el.style.display = 'none';
 }

 el = document.getElementById( 'rechtsbuiten');
 if (el != undefined)
 {
   el.style.display = 'none';
 }

}

function sluitEigenMenu() 
{
 el = document.getElementById( 'contentEigenMenu' );
 el.style.display = 'none';
   
 el = document.getElementById( 'bannerprofielinfo');
 if (el != undefined)
 {
   el.style.display = 'block';
 }
 el = document.getElementById( 'rechtsbuiten');
 if (el != undefined)
 {
   el.style.display = 'block';
 }
   
}





function updateSearchAction()
	{
   
      
   current = document.SubmitSearch.SearchIn.selectedIndex;
   
   el = document.getElementById( 'ExtraSearchOptions' );

   if (el != undefined) {el.style.display = 'none';}

   

   if ( document.SubmitSearch.SearchIn[current].value == 'P' )
   {
     document.SubmitSearch.action=cDomain+'/maand.p';
     document.SubmitSearch.method='post';
   }
   else
   if ( document.SubmitSearch.SearchIn[current].value == 'F' )
   {
     document.SubmitSearch.action=cDomain+'/fotoboekhist.p?view=lijst';
     document.SubmitSearch.method='post';
   }
   else
   if ( document.SubmitSearch.SearchIn[current].value == 'A' )
   {
     document.SubmitSearch.action=cDomain+'/djlist.p?scope=search';
     document.SubmitSearch.method='post';
   }
   else
   if ( document.SubmitSearch.SearchIn[current].value == 'N' )
   {
     document.SubmitSearch.action=cDomain+'/nieuwshist.p';
     document.SubmitSearch.method='post';
   }
   else
   if ( document.SubmitSearch.SearchIn[current].value == 'L' )
   {
     document.SubmitSearch.action=cDomain+'/locatie.p';
     document.SubmitSearch.method='post';
   }
   else
   if ( document.SubmitSearch.SearchIn[current].value == 'O' )
   {
     document.SubmitSearch.action=cDomain+'/organiserlist.p';
     document.SubmitSearch.method='post';
   }
   else
   if ( document.SubmitSearch.SearchIn[current].value == 'D' )
   {
     document.SubmitSearch.action='http://www.presale.nl/googleresults.p';
     document.SubmitSearch.method='get';
     document.getElementById('ss1').value='www.presale.nl';
     
   }
   else
   if ( document.SubmitSearch.SearchIn[current].value == 'G' )
   {
     document.SubmitSearch.action='http://www.presale.nl/googleresults.p';
     document.SubmitSearch.method='get';
     document.getElementById('ss1').value='';
   }
   else
   if ( document.SubmitSearch.SearchIn[current].value == 'M' )
   {
     document.SubmitSearch.action=cDomain+'/profielenoverzicht.p?scope=zoek';
     document.SubmitSearch.method='post';
     if (el != undefined) {el.style.display = 'inline';}
   }
   
}

function Meld_Misbruik( cSource , cMemberName, cRowid ) 
{

  var agree=confirm( 'Vind je het bericht van ' + cMemberName + ' echt niet kunnen en wil je het bericht naar DJGuide versturen ter beoordeling?' )
      
  if (agree)
  {
    
    ajaxSend( '/ajaxmeldmisbruik.p?source=' + cSource + '&rowid=' + cRowid , handleMisbruik );
    el = document.getElementById( cRowid );
    el.style.display = 'none';

  }  
}

function handleMisbruik(response) 
{
 
 alert( response );
 
}


function trimAll(sString) 
{
  while (sString.substring(0,1) == ' ')
  {
  sString = sString.substring(1, sString.length);
  }
  while (sString.substring(sString.length-1, sString.length) == ' ')
  {
  sString = sString.substring(0,sString.length-1);
  }
  return sString;
}


function ScrollToElement(theElement){
  var selectedPosX = 0;
  var selectedPosY = 0;
  while(theElement != null){
    selectedPosX += theElement.offsetLeft;
    selectedPosY += theElement.offsetTop;
    theElement = theElement.offsetParent;
  }
 window.scrollTo(selectedPosX,selectedPosY);

}

function setCaretTo(obj, pos) {   
	  
	  
    if(obj.createTextRange) {   
        /* Create a TextRange, set the internal pointer to  
           a specified position and show the cursor at this  
           position  
        */  
        var range = obj.createTextRange();   
        range.move("character", pos);   
        range.select();   
    } else if(obj.selectionStart) {   
        /* Gecko is a little bit shorter on that. Simply  
           focus the element and set the selection to a  
           specified position  
        */  
        obj.focus();   
        obj.setSelectionRange(pos, pos);   
    }   
    
}  



function attentafriend()
{
  // alert( $('formattentafriend').serialize()  );
  
  new Ajax.Request('/ajaxprofielguestbookadd.p', {
  parameters: $('formattentafriend').serialize(true) ,
  
  onSuccess: function(transport){
      
      
      
      var response = transport.responseText || "no response text";
      
      
      
      alert(response);
      return false;
    },
 	 onFailure: function()
 	 { alert('Something went wrong...') ;
 	   return false;	
 	 	}
 	 
  });
  

} 	 


function containsDOM (container, containee) {
  var isParent = false;
  do {
    if ((isParent = container == containee))
      break;
    containee = containee.parentNode;
  }
  while (containee != null);
  return isParent;
}

function checkMouseEnter (element, evt) {
  if (element.contains && evt.fromElement) {
    return !element.contains(evt.fromElement);
  }
  else if (evt.relatedTarget) {
    return !containsDOM(element, evt.relatedTarget);
  }
}

// checkMouseLeave for onmouseout on outer div
function checkMouseLeave (element, evt) {
  if (element.contains && evt.toElement) {
    return !element.contains(evt.toElement);
  }
  else if (evt.relatedTarget) {
    return !containsDOM(element, evt.relatedTarget);
  }
}

var default_submenu = '';


var iContainerLeft  = 0;
function showsubmenu( tabid , containername, menuname )
{
	// prototype:     $$('div.submenu').each(function(node) { if(!node.id != menuname)  node.hide(); }); 
	
	$('div.submenu').hide( );
	
	iContainerLeft  = $( '#' + containername ).offset().left ;
	var iContainerWidth = $( '#' + containername ).width() ; // 980
	
  iTabidLeft =  $( tabid ).offset().left ;

  if (menuname == '') return;
  iMenuWidth =  $( '#' + menuname ).width() + 50 ; // 50 marge for changing names
  
  if ( ( iTabidLeft + iMenuWidth ) > iContainerWidth  + iContainerLeft  ) 
  {
  	iLeft = ( iContainerWidth  - iMenuWidth );
  }
  else
  	iLeft  = $( tabid ).offset().left - iContainerLeft ;
  	
  iLeft = iLeft - 15;
	
	tabMenu = document.getElementById( menuname );
	
	if (tabMenu == null ) return ; // probably empty menuname; not a normal situation
	
	
	tabMenu.style.position = 'absolute' ;
	
  $('#' + menuname).css("left", iLeft + "px");
  $('#' + menuname).show();
	
}	




function backToDefaultSubMenu( element, evt) 
{    

 if (checkMouseLeave( element, evt)) 
 {
   
 	 $('div.linksubmenu').hide( );
 	 $('div.submenu').hide( );
 	 
   // if (default_submenu != '')  $('#' + default_submenu).show();
   if ( document.getElementById( default_submenu ) ) $('#' + default_submenu).show();
 }

}


function showsubmenuonlink( linkid, menuname )
{
	// prototype:     $$('div.submenu').each(function(node) { if(!node.id != menuname)  node.hide(); }); 
	
	
	$('div.linksubmenu').hide( );
	
	
  iLinkidLeft =  $( linkid ).offset().left ;
	
	
	
	LinkMenu = document.getElementById( menuname );
	LinkMenu.style.top = '77px' ;
	
	
  $(LinkMenu).css("left", (iLinkidLeft - iContainerLeft) + "px");
  $(LinkMenu).show( );
	
	//alert( menuname );
}	




function printContent(id){
   str=document.getElementById(id).innerHTML
   newwin=window.open('','printwin','left=100,top=100,scrollbars=yes,width=600,height=600')
   newwin.document.open();
   newwin.document.write('<html><head><style type="text/css">p{text-align:center;font-weight:600;}body{padding:20px;background-color:#ffffff;font-family:verdana,tahoma,sans-serif;font-size:10pt;}ul{margin-top:30px;}</style></head>');
   newwin.document.write('<title>Print Page</title>\n')
   newwin.document.write('</HEAD>\n')
   newwin.document.write('<BODY>\n')
   newwin.document.write(str)
   newwin.document.write('<hr />');
   /*newwin.document.write('<a href="#" onClick="window.print();">Print</a>')*/
   newwin.document.write('<p> www.chocolatelover.net</p>');
   newwin.document.write('<a href="javascript:window.close();">Close Window</a></body></html>');
  /* newwin.document.write('</HTML>\n')*/
   newwin.document.close();
   newwin.print();
}

function showmessage( elOffSet, cType , cText, lAutoHide )
{
	
	if (elOffSet == null) return true;
	
	var cImage = '';
  var cMessage = '';
  var iTop = 0;
  var iLeft = 0;  
  var pos = $(elOffSet).offset();
  
  
  
  msg = $( '#msgbox' );
  msg.stop(true,true); // stop animations 
  msg.attr('class', cType );
  
  iTop = pos.top;
  iLeft = pos.left;
   
  
  if ((iTop == 0) && (iLeft == 0)) // if offset is deleted, bin is gone after refresh
  { 
  	// $(elOffSet).parent().offset().top;
  	
  	iTop = 200;
  	iLeft = 300;
  }
  else
  {	 
  iTop = pos.top - 110;
  iLeft = pos.left - 70;
  }
 
  
  if (iTop < 0) iTop = 20 ;
  if (iLeft < 0) iLeft = 20 ;
  msg.css( { top: iTop, left: iLeft });     

  msg.html( '<div style="margin-left:30px; min-height:32px;">' + cText + '</div>' );
  if (lAutoHide) msg.show().animate({opacity: 1.0}, 2500).hide(3500);
  else msg.show();

  return true;  
 
}


function showlayer( elOffSet, cHtml )
{
	
	if (elOffSet == null) return true;
	
  var pos = $(elOffSet).offset();
  
  layer = $( '#layer1' );
  
  iTop = pos.top - 110;
  iLeft = pos.left + 30;
  
  if (iTop < 0) iTop = 20 ;
  if (iLeft < 0) iLeft = 20 ;
  layer.css( { top: iTop, left: iLeft });     

  layer.html(  cHtml );
  
  layer.show();
  
  return true;  
 
}

function hidelayer(  )
{ $( '#layer1' ).hide(); }

function loginform( elSource )
{
	
	
	var offset = $(elSource).offset();
	var iTop  = 0;
	var iLeft = 0;
	iLeft = offset.left;
	if (iLeft > 650) iLeft = 650;
	else if (iLeft > 200) iLeft = iLeft - 200;
	iTop  = offset.top + 20;
	
	if (iTop < 120) iTop = 120; // voorkomen dat je onder een flash advertentie in header komt te liggen, wmode kan mogelijk niet op transparent staan
  // $("#dialogcontainer").dialog( 'option', 'position', [ iLeft, iTop ] );
  
  
  // $("#dialogcontainer").dialog( 'option', 'position', [ iLeft, iTop ] );
  $("#dialogcontainer").dialog( 'option', 'position', 'center' );
  
  
  $('#dialogcontainer').dialog( 'option', 'height', 220);
	$('#dialogcontainer').dialog( 'option', 'width', 400);	
	$('#dialogcontainer').dialog('option', 'title', 'Login');		
	


  $.get( cDomain + "/ajax/ajaxlogin.p", { act: 'showform' },
         function(data){
          if ( data == undefined) 
          { alert( 'no data' );
          	return;
          }	
          

          $("#dialogcontainer").html(data);
          
          $("#login").submit(function() { return false;} );
          
          $('#loginbutton').click(function(e) {   
             doLogin(this);
           });

          
          
          
			    $("#dialogcontainer").dialog( "open" );
			    
			    
			    $("#login input").keypress(function(e) 
			      { if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) 
			      	{ doLogin( elSource ); 
			      	  return false; // stops beep	
			      	}
			      	
            });
  
          $('#login input[value=""]:first').focus();
      
     });
     
  return false;   
}
	


function doLogin( elSource )
{ 	
	var form = $("#login");  
  var serializedFormStr = form.serialize();  
  var cError    = "";
  var cMessage  = "";
    // alert( serializedFormStr );
    
	  $.get(cDomain + "/ajax/ajaxlogin.p", serializedFormStr, 
         function(data){
         
         $(data).find('result').each(function(){ 
         	cError    = $("error", this).text();
         	cMessage  = $("message", this).text();
         	if (cError != "") 
         	  { $("#loginmessage").addClass("invalidmessage");
         	  	$("#loginmessage").html(cError); } 
         	else
         	 { $('#dialogcontainer').dialog( "close" );
         	 
         	   // setTimeout( eval( 'window.location.href=window.location.href;' ) ,200);
         	   if ( showmessage( elSource , 'succes' , ((cLanguage=='en') ? 'Login in process' : 'Bezig met inloggen' ), false ))
         	     window.location.reload(true);
         	 	 
         	 }

         	})
  },  "xml");

  return false;
}	

function doLogout(elSource)
{ 	
  var cError    = "";
  var cMessage  = "";
	$.get(cDomain + "/ajax/ajaxlogin.p", { act: 'logout' }, 
         function(data){
         $(data).find('result').each(function(){ 
         	cError    = $("error", this).text();
         	cMessage  = $("message", this).text();
         	if (cError != "") 
         	  { alert( cError); } 
         	else
         	 { 
         	 	 if ( showmessage( elSource , 'succes' , ((cLanguage=='en') ? 'Logout in process' : 'Bezig met uitloggen' ), false ) )
         	 	 	 	   window.location.reload(true);
           }
         	})
  },  "xml");

  return false;
}	




function showMoodImg()
{ 
	current = document.getElementById( 'mood' ).selectedIndex;
  document.getElementById( 'moodsmiley' ).src = 
  ( cStaticPathSite + document.getElementById( 'mood' )[current].rel );  
  // document.images.show.src = document.login.mood[current].value;
	}

window.viewport = 
{
    height: function() { 
        return $(window).height(); 
    },
    
    width: function() {
        return $(window).width();
    },
    
    scrollTop: function() {
        return $(window).scrollTop();
    },
    
    scrollLeft: function() {
        return $(window).scrollLeft();
    }
};


function elementInViewport( elname )
{
	
	offsetel     = $('#' + elname).offset();
	
	return ( ( offsetel.left < ($(window).scrollLeft() + $(window).width() ) ) &&
	       ( offsetel.top < ($(window).scrollTop() + $(window).height() ) ) &&
	        ( offsetel.top  > $(window).scrollTop() )
	         );

}



$(document).ready(function() {
    $("#submenucontainer_DJGMain").mouseleave(function(){

   	  $('div.linksubmenu').hide( );
 	    $('div.submenu').hide( );

 	    if ( document.getElementById( default_submenu ) ) $('#' + default_submenu).show();
 	    // if (default_submenu != '')  $('#' + default_submenu).show();
    });

	$("#dialogcontainer").dialog({
				autoOpen: false,
				modal: true,
				closeOnEscape: true ,
				resizable: true,
				draggable: true,
				overlay: { 
					opacity: 0.5, 
					background: "black" 
				},
				close: function(e, ui)
        { startTimers(); },
        open: function(e , ui)
        { stopTimers(); }
        
			});
			
	$('img.helpinfo').cluetip(
	{showTitle: false,
	 width: 500
		});
	
}); 

