/**
 * DEFINE DEFAULT VARIABLES
 *
 */
var dom_ready = false;
var load_banners_onstart = true;
var slided_out;
var IS_FLASH_SHOWN = true;
var	global_show_comments_page = 1;
var	global_show_comments_order = "asc";
var global_show_comments_type = "article";
var global_show_comments_id   = false;	

var videoList_div = new Array();
var videoList_img = new Array();

var banner_flashvar = ''; // holder for banner flashvar which we sent to player
/**
 *   Event loads when DOM is ready
 *   This functions will fire right after HTML DOM is ready (before pictures load)
 */
window.addEvent('domready', function()
{
	/**
	 * SET VARIABLES
	 */
	dom_ready = true;



	/**
	 *  PRELOAD FOLLOWING IMAGES:
     */
	preload_images = new Array(
		"/static/shared/img/icons/ajax.gif",
		"/static/shared/img/icons/ico_home_page_ff_on.gif",
		"/static/shared/img/icons/ico_home_page_ie_on.gif",
		"/static/shared/img/icons/ico_home_page_netscape_on.gif",
		"/static/shared/img/icons/ico_home_page_opera_on.gif",
		"/static/shared/img/icons/ico_home_page_safari_on.gif",
		"/static/shared/img/icons/ico_home_page_ff_off.gif",
		"/static/shared/img/icons/ico_home_page_ie_off.gif",
		"/static/shared/img/icons/ico_home_page_netscape_off.gif",
		"/static/shared/img/icons/ico_home_page_opera_off.gif",
		"/static/shared/img/icons/ico_home_page_safari_off.gif"
	);

	preload_images.each ( function(el_url) {
		var tmp_image = new Image();
		tmp_image.src = el_url;
	});

	// fills in the username or registration link
	reload_user_info();
	
});


/**
 *  ########################################################################################
 *  ############################  FUNCTIONS BELOW THIS LINE  ###############################
 *  ########################################################################################
 */
function include_css(file)  	 
{ 	 
  if (document.createElement && document.getElementsByTagName) { 	 
  var d_head = document.getElementsByTagName('head')[0]; 	 
  	 
  var d_script = document.createElement('link'); 	 
  d_script.setAttribute('type', 'text/css'); 	 
  d_script.setAttribute('rel', 'stylesheet'); 	 
  d_script.setAttribute('href', file); 	 
  	 
  d_head.appendChild(d_script); 	 
  } 	 
}

/**
 * Reloads user info with either username or registration link
 * @author Matej Balantič <matej@balantic.si>
 */
function reload_user_info()
{
	var username = Cookie.get('user')
	
	// HIDE / SHOW all user blocks
	$ES('.user_display_when_loggedin').each ( function (el)
	{
		if (username)
			el.style.display = 'block';
		else
			el.style.display = 'none';
	});
	$ES('.user_display_when_loggedout').each ( function (el)
	{
		if (username)
			el.style.display = 'none';
		else
			el.style.display = 'block';
	});

	// FILL IN USERNAME
	if (username)
		$ES('.user_username').each ( function (el)
		{
			username = username.replace('+', ' ');
			el.innerHTML = username;
		});
}

function open_close_subform(div_id,div_head)
{
	var div_name = $(div_id);
	if(div_name)  {
		if (div_name.style.display == "block") {
			if (div_id == 'video_content') {
				try {
					$('flvvideo').innerHTML = ""; //remove flvvideo
				} catch(e) {
					null;
				}
			}
			div_name.style.display = "none";
			$(div_head).className = "head";
			
		} else {
			div_name.style.display = "block";
			$(div_head).className = "headopen";
			if (div_id == 'video_content') {
				video_player_setup(); //reopen flvvideo
			}
		}
	}	
}


/**
 * Sends AJAX request to $script_name and updates $div_name with results.
 * Animates $div_name while waiting for AJAX response
 * @param div_name Name of the div to write in
 * @param script_name name of the script to execute
 * @param callbackFn Calback function executed after successful request
 * @param callbackFnScope Scope in which callback function will be executed
 * @author Anže Robida
 * @author Matej Balantič <matej@balantic.si> (ajax-loader animation)
 */
function returnContent(div_name,script_name,media_title,media_id,callbackFn,callbackFnScope) {

	// hide flash
	flash_show(false, $(div_name));

	slided_out = false;

	// create hover layer
	hover_layer = new Element("div")
		.setStyle('position', 'absolute')
		.setStyles($(div_name).getCoordinates())
		.injectInside(document.body)
		.setOpacity(0.3)
		.setStyles({
				'background-color':'black',
				'position': 'absolute',
				'z-index': '5'
			});

	
	if (div_name == 'vsection_content_margin')
	{
		hover_layer.setStyles({
				'margin-left': '10px'
			});
	}

	// create ajax loader
	ajax_load = new Element("img")
	ajax_load.src = '/static/shared/img/icons/ajax.gif';
	move_top  =   ($(div_name).getPosition()['y'] + $(div_name).getSize()['size']['y']/2) - (ajax_load.height/2);
	move_left =   ($(div_name).getPosition()['x'] + $(div_name).getSize()['size']['x']/2) - (ajax_load.width/2);
	ajax_load.setStyles({
			"position": 	'absolute',
			"top": 		 	move_top,
			"left":			move_left,
			"z-index": 		'10'
		}).injectInside(document.body);

	// call ajax script
	new Ajax(script_name, {
		method: 'get',
		encoding: 'UTF-8',
		onComplete: function(response)
		{
			$(div_name).innerHTML = response;

			if (div_name == 'videosection' || div_name == 'video_section_main_video') {
				//put # tag into location
				var url_temp = window.location.href; //parse fixed location
				url_temp = url_temp.substring(0,url_temp.indexOf('#')); 
				var media_id = script_name; //parse media_id
				media_id = media_id.substring(media_id.indexOf('media_id=')+9);
				media_id = parseInt(media_id.substring(0,media_id.indexOf('section_id')-1));
				window.location = url_temp + '#media_id=' + media_id;

				$('video_main_box').setStyle("visibility",'visible');
			}

			//if video page
			if (div_name == 'videosection' || div_name == 'video_section_main_video' || div_name == 'embed_flvvideo') {
				//replace title of the window - for ajax calls
				if (div_name != 'embed_flvvideo') {
					var explodestring = document.title;
					explodestring = explodestring.split('-');
					explodestring = explodestring[explodestring.length-1];
					explodestring = explodestring.split('#');
					explodestring = explodestring[0];
					if (media_title) {
						document.title = media_title + ' - ' + explodestring.trim();
					}
				}
				else
				{
					autostart = true; // we automaticaly start video if requested through ajax in article
				}
			}
			
			//change style of active media
			if (media_id) {
				try {
					//remove background of non-active media
					$ES('.small_video').each ( function(el) 
					{
						el.setStyles({background: ''});			  
					});
					
					$("media_id_"+media_id).setStyles({background: 'url(/static/'+app_lang+'/main/img/backgrounds/back_video.gif) repeat-y'});
					
				} catch(e) {
					null;
				}
				
			}

			// show flash back
			flash_show(true, $(div_name));
			// destroy ajax loader & div layer
			hover_layer.remove();

			// Call callback user function
			if (typeof callbackFn == 'function') {
				// Execute with scope
				var scope = callbackFnScope || this || document;
				callbackFn.call(scope);
			}

			ajax_load.remove();

		},
		// do eval scripts inside the AJAX response
		evalScripts: true
	}).request();
}

function do_slide() {

	if (!slided_out)
	{
		setTimeout(do_slide,300);
		return true;
	}
	mySlide.slideIn();
}

/**
 * REGISTRATION FUNCTIONS
 */

function close_banner() {}
function open_banner() {}
function pre_fullscreen() {}
function post_fullscreen() {}

function login_show()
{
    close_banner();
    mbox_open(500, 500);
    pre_fullscreen();

    step_1_help = new Array("main_help");
    active_step = 1;
    help_displayed = 0;

    new Ajax('/bin/registration2/login.php', {
	     method: 'get',
	     encoding: 'utf-8',
	     update: $('MBOX_window'),
	     evalScripts: true
	}).request();
}


function register_show()
{
    close_banner();
    mbox_open(500, 500);
    pre_fullscreen();

    // help boxes in step 1
    step_1_help = new Array("username_help", "password_help", "email_help", "main_help");

    // url of script for ajax purposes
    registration_script_url = '/bin/registration2/';

    // how many steps are there
    all_steps = 3;
    active_step = 1;

    // help box visibility status
    help_displayed = false;

    password_renewal = false;

    load_settings();

    url = '/bin/registration2/' + '?action=display' + '&what=1' + '&return=' + location.href;
    new Ajax(url, {
	     method: 'get',
	     encoding: 'utf-8',
	     update: $('MBOX_window'),
	     onComplete: register_show_complete,
	     evalScripts: true
	    }).request();
}


function register_show_complete( result )
{
    active_step = $('active_step').value;

    // get HTML code of ajax loader
    ajax_loader     = $('ajax_loader_div').innerHTML;
    ajax_loader_big = $('ajax_loader_big_div').innerHTML;

    build_indicators( $('active_step').value );
}


function load_settings()
{
    new Ajax('/bin/registration2/' + '?action=javascript' + '&what=register', {
	     method: 'get',
	     encoding: 'utf-8',
	     onComplete: load_settings_complete
	    }).request();
}


function load_settings_complete( result )
{
    eval( result );
}

/*
 * Function send article
 * @param div_name Name of the div to write in
 * @param script_name name of the script to execute
 * @param text_ok text if article was sent
 * @param text_error text if there goes something wrong
 *
*/
function articleSend(div_name,script_name,text_ok, text_error) {

	// lets setup title
	if(script_name.indexOf('?')>0){script_name+="&title="+document.title;}else{script_name+="?title="+document.title;}
	new Ajax(script_name, {
		method: 'post',
		encoding: 'utf-8',
		postBody: document.article_send,
		onComplete: function(response) {
			if (response == 'OK') {
				$(div_name).innerHTML = text_ok;
				try	{
					$("email_to").value = "";
				} catch (r) {null;}
				
			} else {
				$(div_name).innerHTML = text_error;
			}
		}
	}).request();
}

/**
 * This function execute PHP file, which write into database rating for current video creator
 */
function ajax_put_video_creator_vote(id, vote) {

	var url = '/bin/ajax_video_creator_vote.php?id='+id+'&rate=' + vote;

	new Ajax(url, {
		method: 'get'
	}).request();
}
/**
 * This function execute PHP file, which write into database rating for current article
 */
function ajax_put_vote(article_id, vote) {

	var url = '/bin/ajax_article_vote.php?article_id=' + article_id + '&rate=' + vote;

	new Ajax(url, {
		method: 'get'
	}).request();
}

/**
 * This function execute PHP file, which write into database rating for current article from chart
 */
function ajax_put_vote_chart(article_id, vote) {

	var myCookie = Cookie.get('chartvote');
	
	mbox_open(450, 500, true);

	if (myCookie) {
		//open error voting dialog and shows hours left till voting is possible
		ts = (new Date()).getTime();
		diff = parseInt(24-(ts-myCookie)/1000/60/60);
		$('MBOX_window').innerHTML = $('mbox_notvalid').innerHTML.replace('24',diff);
		return false;
	}

	var url = '/lbin/ajax_article_chart_vote.php?article_id=' + article_id + '&rate=' + vote;
	new Ajax(url, {
		method: 'get'
	}).request();
	
	//show ok mark
	$('tnx_vote_'+article_id).innerHTML = '<img src="/static/bg/microsites/profm/img/buttons/vote_music_chart_butt_on.gif" />';
	
	//set cookie
	ts = (new Date()).getTime();
	var myCookie = Cookie.set('chartvote', ts, {duration: 1, path: '/'});

	//open vote proccessed dialog
	$('MBOX_window').innerHTML = $('mbox_valid').innerHTML;
}

/**
 * This function execute PHP file, which write into database rating for current media
 */
function ajax_put_media_vote(article_id, vote, response_f) {

	var url = '/bin/ajax_media_vote.php?media_id=' + article_id + '&rate=' + vote;

	response_f = response_f ? response_f : function(){};
	new Ajax(url, {
		method: 'get',
		    onComplete: eval(response_f)
	}).request();
}

/**
 * This function execute PHP file, which write into database rating for current media
 */
function ajax_put_comic_vote(article_id, vote) {

	var url = '/bin/ajax_comic_vote.php?media_id=' + article_id + '&rate=' + vote;

	new Ajax(url, {
		method: 'get'
	}).request();
}
/**
 * Function sends comment on article to server
 */
function ajax_comment_send(type, text_wait, text_saved, text_pwd, text_bnd, text_pnd, text_err, text_act, text_sent) {
	
	var url = '/bin/ajax_comments_save_'+type+'.php'; 
	if ($('comment_txt').value == '')
		return false;

	//write "wait" message
	span_comment_msg_1 = $('span_comment_msg');
	if (!span_comment_msg_1)
		return false;
	span_comment_msg_1.innerHTML = text_wait;

	new Ajax(url, {
		method: 'post',
		postBody: document.comment_send,
		onComplete: function(response) {

			var result_1 = response;

			//comment saved
			if (result_1 == 'SUCCESS') {
				if($('article_comment_form_id'))
					$('article_comment_form_id').innerHTML = text_saved;
				else
					span_comment_msg_1.innerHTML = text_saved;
				
				show_comments();
				
			}

			//user banned temp
			if (result_1 == 'BND')
				span_comment_msg_1.innerHTML = text_bnd;


			//password inavlid
			if (response == 'PWD')
				span_comment_msg_1.innerHTML = text_pwd;

			//comment already sent
			if (response == 'OK'){
				$('article_comment_form_id').innerHTML = text_sent;
			}
			//user banned
			if (response == 'PND')
				span_comment_msg_1.innerHTML = text_pnd;

			//common error
			if (response == 'ERR')
				span_comment_msg_1.innerHTML = text_err;

			//user is not activated
			if (response == 'ACT')
				span_comment_msg_1.innerHTML = text_act;
		}
	}).request();
}

function show_comments(type, id, page, order) {
	// updating after user post
	if (typeof(id) === "undefined")
	{
		type	= global_show_comments_type;
		id 		= global_show_comments_id;
		order 	= global_show_comments_order;
		if (order == "desc" || order == "DESC")
			page = 1;
		else
			page = "last";
	}

		
	global_show_comments_type = type;
	global_show_comments_id   = id;	
	global_show_comments_page = page;
	global_show_comments_order = order;


	var url = '/bin/ajax_comments.php?type='+type+'&id=' + id + '&page=' + page + '&order=' + order + '&max_number=10';

	new Ajax(url, {
		method: 'get',
		onComplete: function(resp) {
			new_div = new Element('div')
			.setHTML(resp);

			$('comments').replaceWith(new_div);

		}
	}).request();

}

function homepage_img_change(browser) {
	$ES('.homepage_button').each ( function (el) {
		curr_browser = el.id.split("_")[1];
		el.src = '/static/shared/img/icons/ico_home_page_'+curr_browser+'_on.gif';
	});
	if (!browser)
		browser = selected_browser;

	$("image_"+browser).src = '/static/shared/img/icons/ico_home_page_'+browser+'_off.gif';

}

function homepage_browser(browser_name)
{
	new Ajax('/bin/ajax_homepage.php?browser='+browser_name, {
		method: 'get',
		encoding: 'utf-8',
		update: $('MBOX_window'),
		onComplete: function () { homepage_insert(browser_name); }
	}).request();

}

function homepage_get_browser()
{
	if (window.ie)
		return "ie";
	if (window.gecko)
		return "ff";
	if (window.webkit)
		return "safari";
	if (window.opera)
		return "opera";

}
//insert text into MBOX
function homepage_insert(browser_name)
{
	if (browser_name=="firefox") browser_name="ff";
	if (browser_name=="explorer") browser_name="ie";

	selected_browser = browser_name;
	homepage_img_change(browser_name);
}

function bookmark_show(siteURL)
{


	if (window.ie)
	{

		var html_body = document.getElementsByTagName('body').item(0);
		var homepage_link = document.createElement('a');
		html_body.appendChild(homepage_link);
		homepage_link.style.display = 'none';

		homepage_link.style.behavior='url(#default#homepage)';
		homepage_link.setHomePage(siteURL);

	}
	else
	{
		// hide for now - not translated yet
		mbox_open(450,200);
		if (window.gecko)
		{
			homepage_browser('firefox');
		}
		else if (window.webkit || window.webkit419 || window.webkit420)
		{
			homepage_browser('safari');
		}
		else if (window.opera == 'Opera')
		{
			homepage_browser('opera');
		}
/*		else if (browser_name == 'Netscape')
		{
			homepage_browser('netscape');
		}*/
		else {
			homepage_browser('explorer');
		}
	}

}


/* Opens MBOX on the webpage
  @author Matej Balantič <matej@balantic.si>
*/
function mbox_open(MBOX_WIDTH,MBOX_HEIGHT,IS_DRAGGABLE)
{
	flash_show(false);

    if ( typeof(IS_DRAGGABLE) == "undefined" )
                IS_DRAGGABLE = true;

	// create holders if there isn't any
	mbox_create_holders( IS_DRAGGABLE );

	left_s = window.getWidth()/2 - MBOX_WIDTH/2;
	top_s = window.getScrollTop() +(window.getHeight() - MBOX_HEIGHT)/2;

	// push down if it is still above content
	if (top_s < 180)
		top_s = 180;

	$("MBOX_window").setStyles('left:'+left_s+'px; top: '+top_s+'px; display:block;');
	$("MBOX_dragger").setStyles('left:'+left_s+'px; top: '+top_s+'px; display:block;');

	$("MBOX_overlay").setStyles('display:block;');

	$("MBOX_overlay").setStyles({"height": window.getScrollHeight()+'px', "width": window.getScrollWidth()+'px'});
	$("MBOX_window").setStyles({"width": MBOX_WIDTH+'px'});
	$("MBOX_dragger").setStyles({"width": MBOX_WIDTH+'px'});



	new Fx.Style('MBOX_overlay', 'opacity',{duration: 400, transition: Fx.Transitions.sineInOut}).start(0,0.6);
	new Fx.Style('MBOX_window',  'opacity',{duration: 250, transition: Fx.Transitions.sineInOut, onComplete:function(){  } }).start(0,1);
}


/* closes mbox
  @author Matej Balantič <matej@balantic.si>
*/
function mbox_close()
{

	new Fx.Style('MBOX_overlay', 'opacity',{duration: 400, transition: Fx.Transitions.sineInOut, onComplete:function(){ $("MBOX_overlay").setStyles('display:none;'); }}).start(0.6,0);
	new Fx.Style('MBOX_window',  'opacity',{duration: 250, transition: Fx.Transitions.sineInOut, onComplete:function(  )
		    {
			$("MBOX_window").setStyles('display:none;');
			$("MBOX_window").innerHTML = '';
			flash_show(true);

		    } }).start(1,0);
	$("MBOX_dragger").setStyles({"display": 'none'});

}
/**
 * Shows / hides all FLASH & IFRAME objects on the page
 * @param bool $display True when showing, false when hiding
 * @author Matej Balantič <matej@balantic.si>
 */
function flash_show(display, div_name)
{
	if (display)
	{
		stats = 'visible';
		IS_FLASH_SHOWN = true;
	}
	else
	{
		stats = 'hidden';
		IS_FLASH_SHOWN = false;
	}

	if (window.ie)
		flash_container = 'object';
	else
		flash_container = 'embed';

	// don't set style with mootools; doesn't work in IE6	
	$ES(flash_container,div_name).each ( function (el) {
		el.style.visibility = stats;
	});
	
	$ES('iframe', div_name).each ( function (el) {
		el.style.visibility = stats;
	});
}
/* makes mbox draggable & creates mbox holders
  @author Matej
*/
function mbox_create_holders( is_draggable )
{
	if ($('MBOX_overlay'))
	{
                $("MBOX_overlay").remove();
                $("MBOX_dragger").remove();
                $("MBOX_window").remove();
	}

	include_css('/static/shared/css/mbox.css');
	// CREATE MBOX HOLDERS

	var mbox_overlay = new Element('div', {
		'id' : 'MBOX_overlay'
	})
	.injectInside(document.body);

	var mbox_dragger = new Element('div', {
		'id': 'MBOX_dragger'
	})
	.injectInside(document.body);

	var mbox_window = new Element('div', {
		'id': 'MBOX_window'
	})
	.injectInside(document.body);

	var mbox_window_loading = new Element('div', {
		'id': 'MBOX_loading'
	})
	.injectInside(mbox_window);

	var mbox_window_loading_img = new Element('img', {
		'src': "/static/shared/img/ajax-loader-white.gif",
		'alt': 'Loading'
	})
	.injectInside(mbox_window_loading);

	// CREATE DROP
    if ( is_draggable ) {
		var main_content = $('left');

		mbox_dragger.addEvent('mousedown', function(e) {
			e = new Event(e).stop();
			mbox_window.setStyles({
				'opacity':0.7
			});
			drag = mbox_window.makeDraggable({
				container: main_content
			}); // this returns the dragged element
			drag.start(e); // start the event manual
		});

		mbox_window.addEvent('emptydrop', function() 
		{
			mbox_dragger.setStyles({
				'left': this.getCoordinates().left,
				'top': this.getCoordinates().top
			});
			mbox_window.setStyles({'opacity':1});
			drag.detach();
		});
	}
}

/*
 *  * Image pool function - auto change images
 *     @author Matej
*/
 function image_change(div_name, image_array, current_img)
 {
 	var container = $(div_name);
	var all_imgs = image_array.length;
	
	if (all_imgs <= 1)
		return true;

	container.empty();
	myImg = new Element("img", {
		src: image_array[current_img]['url'],
		alt: image_array[current_img]['title'],
		title: image_array[current_img]['title']
	})
	.setStyle("border", 0)
	.setStyle("opacity", 0)
	.injectInside(container);

        myFx = new Fx.Style(myImg, 'opacity' , { duration: 500 }).start(0,1);

	current_img = current_img+1;
	if (current_img>=all_imgs)
	{
		current_img = 0;
	}
	
	return current_img;
}

/*
* univrsally changes selected tab within box
*/
	function tab_toggle(obj, n){
		var liSelected = obj.parentNode;
		var i = 0;	
		while(childNode =liSelected.parentNode.childNodes[i++]){
			if(is_valid_node1(childNode)) 
				childNode.className = (childNode.id!=liSelected.id ?"":"selected_tab");

			childNode=null;
		}
		
		i = 0;
		// now we show content of this div
		while (childNode =liSelected.parentNode.parentNode.childNodes[i++]) {
			if (is_valid_node1(childNode) && childNode.tagName=='DIV'){
				//childNode.setStyle('display',((childNode.id == "tabbed_box_"+n) ? 'block':'none');
				if(childNode.id == "div_"+liSelected.id){			
					childNode.style.display = 'block';
					//childNode.setStyle('display','block');
				}else {
					childNode.style.display = 'none';
					//childNode.setStyle('display','none');		
				}
			}	
			childNode =null;
		}
		liSelected=null;
		obj=null;
	}

/**
* test dom element: true if nodeType is 1 and else ok (node should have id)
*/
	function is_valid_node1(e){
		return (e.nodeType==1 && e.name!='undefined' && e.id!='undefined'&& e.id.length>0);
	}

/*
* adds swf object
*/
	function add_video_object(flv_params, flvplayer_div,width,height){
		so = new SWFObject(flv_params, flvplayer_div, width,height, "7","#FFFFFF");
		so.addParam("wmode", "transparent");
		so.write(flvplayer_div);
	}

	
/**
* adds swf object
*/
	function add_comic_object(flv_params, flvplayer_div,width,height){
		so = new SWFObject(flv_params, flvplayer_div, width,height, "7","#FFFFFF");
		so.addParam("wmode", "transparent");
		so.addVariable('submit', '/lbin/strip_handler.php');
		so.write(flvplayer_div);
	}

/**
* ajax function for voting
*/
function poll_vote(formx,voteResponse,answerx,tnx_div) {
	document.getElementById(voteResponse).style.visibility = 'hidden';

	formx.answer_id.value = answerx;

	formx.send ({
		encoding: 'UTF-8',
		onComplete: function(response)
		{
			document.getElementById(voteResponse).innerHTML = response;
			document.getElementById(voteResponse).style.visibility = 'visible';
		}
	});
}

function printpr() {
	window.print();
	return false;
}

// replace all videos but active with thumbnails
function videoList_forceThumbnails(active) {
	for (i = 0; i < videoList_div.length; i++) {
		if (videoList_div[i] != active) {
			$(videoList_div[i]).innerHTML = videoList_img[i];
		}
	}
}



/*
adds dropping submenus to the main sites menu

parameters:
 identifier - string in main menu that identifies that menu item
 link - link to be opened by the submenu item
 caption - submenu item caption
 
 author: Vedran Aberle Tokic vtokic@novatv.hr
*/
attach_drop_submenu = function( identifier, link, caption )
{
	//find target element
	var startdiv = document.getElementById('navigation');
	var ul = startdiv.getElementsByTagName('div')[1].getElementsByTagName('div')[0].getElementsByTagName('ul')[0];
	var lis = ul.getElementsByTagName('li');
	for(var i in lis){
		if(lis[i].tagName && lis[i].tagName=='LI'){
			var suspect = lis[i].getElementsByTagName('a')[0].innerHTML;
			if(suspect.toLowerCase()==identifier.toLowerCase()){
				var target_li=lis[i];
				var ti=i;
				break;
			}
		}
	}
	
	//try to match 
	if (!target_li) {
		for(var i in lis){
			if(lis[i].tagName && lis[i].tagName=='LI'){
				var suspect = lis[i].getElementsByTagName('a')[0].innerHTML;
				if(suspect.toLowerCase().match(identifier.toLowerCase())){
					var target_li=lis[i];
					var ti=i;
					break;
				}
			}
		}
	}
	
	//create or find dropdown div
	if(target_li){
		if(!(target_li.getElementsByTagName('div')[0] && target_li.getElementsByTagName('div')[0].id=="main_menu_dropdown_"+ti)){
			var dropdown=document.createElement("div")
			dropdown.id="main_menu_dropdown_"+ti;
			dropdown.className="main_menu_dropdown";
			dropdown.style.display='none';
			target_li.appendChild(dropdown);
			target_li.dropdown = dropdown;
			target_li.style.position='relative';
			target_li.onmouseover=function(){
				this.dropdown.style.display='block';
			}
			target_li.onmouseout=function(){
				this.dropdown.style.display='none';
			}
		}else{
			var dropdown=target_li.getElementsByTagName('div')[0];
		}
	}

	//attach element to dropdown
	dropdown.innerHTML+='<div class="dropdown_item"><a href="'+link+'">'+caption+'</a></div>'
	
}

//tochnotaka ajax call
function ajax_return_top_10(div_name,script_name) {
	new Ajax(script_name, {
		method: 'get',
		encoding: 'UTF-8',
		onComplete: function(response)
		{
			$(div_name).innerHTML = response;
			addTop10();
		}
	}).request();
}
