/*
  Amika CMS, Copyright (c) 2007, Selensis Software. All rights reserved.
  This CMS is not freeware. Please see the license agreement for details.
*/

var win_dialog;
var win_tp_display;
var win_pic;
var win_pic_big;
var win_article;
var win_stats;
var win_mail;
var win_text;
var win_vd;
var win_smiley;
var win_sidebar;
var win_video;
var win_tree;
var autolink_box;
var html_code;

var ua = navigator.userAgent;
var opera = /opera [56789]|opera\/[56789]/i.test(ua);
var ie = !opera && /msie [56789]/i.test(ua);
var moz = !opera && /mozilla\/[56789]/i.test(ua);

function keyDown(e)
{
	if (ie)
		var keyCode=event.keyCode;
	else
		var keyCode=e.which;
	if (keyCode==27) window.close();
}

function setEscClose()
{
	document.onkeydown = keyDown;
}

function insert_smiley(control_id, txt)
{
	if (window.opener && !window.opener.closed)
    window.opener.document.getElementById(control_id).value += txt;
	window.close();
}

function show_window(tp, par_1, par_2, par_3, par_4)
{
  // assign default values to missing parameters
  par_1 = par_1 || '';
  par_2 = par_2 || '';
  par_3 = par_3 || '';
  par_4 = par_4 || '';                             

  url = par_1;
  resizable = 'resizable,';
  scrollbars = 'scrollbars=1,';

  switch(tp)
  {
    case 'dialog':
      w = 500; h = 600; // when changing w, also change it in dialogs.php JavaResizeDialog()
      x = (screen.width - w) / 2; y = (screen.height - h) / 2;
      resizable = '';
      scrollbars = '';
      break;

    case 'article_tree': // same parameters as dialog, but different tp, becasue we need it to open in a different window than 'win_dialog'
      w = 500; h = 600;
      x = (screen.width - w) / 2; y = (screen.height - h) / 2;
      resizable = '';
      scrollbars = '';
      break;      
      
    case 'text':
      w = 800; h = 600;
      x = (screen.width - w) / 2; y = (screen.height - h) / 2;
      break;

    case 'shopping_cart':
      w = 800; h = 600;
      x = (screen.width - w) / 2; y = (screen.height - h) / 2;
      break;

    case 'sidebar':
      w = 540; h = 700;
      x = (screen.width - w) / 2; y = (screen.height - h) / 2;
      resizable = '';
      scrollbars = '';
      break;

    case 'preview':
      w = 600; h = 200;
      x = 0; y = 0;
      url = 'window_preview.php?id='+par_1;
      break;

    case 'smiley':
      w = 580; h = 260;
      x = (screen.width - w) / 2; y = (screen.height - h) / 2;
      url = 'window_smilies.php?id='+par_1;
      break;

    case 'virtual_download':
      w = 200; h = 100;
      x = 10; y = 10;
      url = 'dispatch.php?id='+par_1;
      break;

    case 'picture':
      w = 1010; h = 680;
      x = (screen.width - w) / 2; y = 5;
      url = 'window_picture.php?id='+par_1;
      break;

    case 'picture_big':
      w = screen.availWidth; h = screen.availHeight;
      x = 0; y = 0;
      url = 'window_picture_big.php?'+par_2+'='+par_1;
      break;

    case 'article':
      w = 1000; h = 680;
      x = (screen.width - w) / 2; y = (screen.height - h) / 2;
      url = 'dialog_article.php?page=' + par_1 + '&id=' + par_2 + '&cat=' + par_3;
      break;

    case 'sidebar_item':
      w = 750; h = 600;
      x = (screen.width - w) / 2; y = (screen.height - h) / 2;
      url = 'dialog_sidebar_item.php?page=' + par_1 + '&id=' + par_2;
      break;

    case 'tp_display':  // temporary pictures
      w = 700; h = 400;
      x = (screen.width - w) / 2; y = (screen.height - h) / 2;
      if (par_1)
        var sp = '?sp=1';
      else
        var sp = '';
      url = 'dialog_tp_display.php' + sp;
      break;

    case 'tp_upload_picture':
      w = 440; h = 200;
      x = (screen.width - w) / 2; y = (screen.height - h) / 2;
      url = 'dialog_tp_upload.php';
      scrollbars = '';
      resizable = '';
      break;

    case 'tp_upload_video':
      w = 440; h = 400;
      x = (screen.width - w) / 2; y = (screen.height - h) / 2;
      url = 'dialog_tp_upload.php?video=1';
      scrollbars = '';
      resizable = '';
      break;

    default:
      return true;
  }
 
  wnd = window.open(url,'wnd_'+tp,resizable+scrollbars+'height='+h+',innerHeight='+h+',width='+w+',innerWidth='+w+',left='+x+',screenX='+x+',top='+y+',screenY='+y);
  wnd.window.focus();
  return false;
}

// blog_picture_window is used as an identifier in converting quotes when editing articles, that's why it's a separate function, and not inside show_window
function blog_picture_window(id, number, video_width, video_height)
{
  number = number || '';
  video_width = video_width || 0;
  video_width = video_width || 0;

  if (video_width)
  {
	  var w = video_width + 40;
	  var h = video_height + 40;
	  var x = (screen.width - w) / 2;
    var y = (screen.height - h) / 2;
    if (win_video && win_video.open) win_video.close();
    win_video = window.open('window_video.php?article_id='+id+'&video_id='+number+'&video_width='+video_width+'&video_height='+video_height,'win_pic','resizable,height='+h+',innerHeight='+h+',width='+w+',innerWidth='+w+',left='+x+',screenX='+x+',top='+y+',screenY='+y);
    win_video.window.focus();
    return false;
  }

	var w = 980;
	var h = 680;
	var x = (screen.width - w) / 2;
  var y = 5;
  if (win_pic && win_pic.open) win_pic.close();
  win_pic = window.open('window_picture.php?article_id='+id+'&number='+number,'win_pic','resizable,height='+h+',innerHeight='+h+',width='+w+',innerWidth='+w+',left='+x+',screenX='+x+',top='+y+',screenY='+y);
  win_pic.window.focus();
  return false;
}

function insert_picture_to_editor(picture_code)
{
	if (window.opener.opener && !window.opener.opener.closed)
	{
		window.opener.opener.tinyMCE.execInstanceCommand('article_text', 'mceInsertContent', false, picture_code);
		window.opener.close();
		window.close();
	}
}

function insert_link_to_editor(page, id, title, editor_id)
{
  editor_id = editor_id || 'article_text';
  window.tinyMCE.execInstanceCommand(editor_id, 'mceInsertContent', false, "<a href='" + page + "?id=" + id + "'>" + title + "</a>");
  return false;
}

function insert_tree_to_editor(page, as_tree, editor_id)
{
  editor_id = editor_id || 'article_text';

  var code = document.getElementById('article_tree').innerHTML;
  if (!code)
    return false;

  code = code.replace(/<a[^,]+, ([0-9]+),[^>]+>(.*) \([0-9]+\)</gi, '<a href="' + page + '?id=$1">$2<');

  if (!as_tree)
  {
    code = code.replace(/<ul>/gi, '');
    code = code.replace(/<\/ul>/gi, '');
    code = code.replace(/<li>/gi, '');
    code = code.replace(/<\/li>/gi, '');
    code = code.replace(/\n/gi, '');
    code = "<p class='ns_no_indent'>\n" + code.replace(/<\/a>/gi, "</a><br />\n") + "</p>";
  }

  window.tinyMCE.execInstanceCommand(editor_id, 'mceInsertContent', false, code);
  return false;
}

function add_summary_picture(filename)
{
  window.opener.document.getElementById('summary_image').style.backgroundImage = 'url('+filename+')';
  window.opener.document.getElementById('summary_picture').value = filename;
  window.close();
  return false;
}

function remove_summary_picture()
{
  document.getElementById('summary_image').style.backgroundImage = 'none';
  document.getElementById('summary_picture').value = '';
}

function add_to_shopping_cart(article_id, product_id, sidebar, cannot_add_msg, none_msg)
{
  var sh_language = document.getElementById((sidebar ? 'sh_sidebar_language' : 'sh_contents_language')).value;
  var sh_quantity = document.getElementById((sidebar ? 'sh_sidebar_quantity' : 'sh_contents_quantity')).value;
  var sh_stock = document.getElementById((sidebar ? 'sh_sidebar_stock_'+sh_language : 'sh_contents_stock_'+sh_language)).value;
              
  if (parseInt(sh_stock) <= 0)
  {
    alert(none_msg);
    return false;
  }
                
  if (parseInt(sh_quantity) > parseInt(sh_stock))
  {
    alert(cannot_add_msg.replace(/%s/gi, sh_stock));
    return false;
  }
  
  document.getElementById((sidebar ? 'sh_sidebar_stock_'+sh_language : 'sh_contents_stock_'+sh_language)).value = sh_stock - sh_quantity;
  
  return show_window('dialog', 'dialog_shopping_add.php?article_id='+article_id+'&product_id='+product_id+'&quantity='+sh_quantity+
    '&language='+sh_language);
}

function cc_selected_color()
{
  var num = -1;
  for (var i = 0; i < 20; i++)
    if (document.getElementById('bc_'+i).checked)
    {
      num = document.getElementById('bc_'+i).value;
      break;
    }
  return num;
}

function cc_set_color(id, set_effect)
{
  document.getElementById('ecard_display').style.backgroundImage = 'none';
  document.getElementById('ecard_display').style.backgroundColor = '#'+id;

  if (!set_effect) return;

  var effect = 0;
  if (document.getElementById('re_1').checked)
    effect = 1;
  if (document.getElementById('re_2').checked)
    effect = 2;
  cc_set_effect(id, effect);
}

function cc_set_effect(color, effect)
{
  if (color == -1)
    color = cc_selected_color();
  if (color == -1)
    return;

  if (effect == 0)
    cc_set_color(color, false);
  if (effect == 1)
     document.getElementById('ecard_display').style.backgroundImage = document.getElementById('cct_'+color).style.backgroundImage;
  if (effect == 2)
     document.getElementById('ecard_display').style.backgroundImage = document.getElementById('ccg_'+color).style.backgroundImage;

}

function cc_set_pattern(id)
{
  document.getElementById('ecard_display').style.backgroundColor = '#FFFFFF';
  document.getElementById('ecard_display').style.backgroundImage = document.getElementById('ecp_'+id).style.backgroundImage;
}

function cc_set_text_color(id)
{
	document.getElementById('ecard_display').style.color = '#'+id;
	document.getElementById('ecard_message_box').style.borderTopColor = '#'+id;
}

function cc_message_change()
{
	document.getElementById('ecard_message_box').innerHTML = document.getElementById('ccb_message').value.replace(/\n/g, '<br/>');
}

function change_ecard_language(section)
{
  location.href = 'ecards?sec=' + section + '&clan=' +  document.getElementById('ecard_lan_combo').options[document.getElementById('ecard_lan_combo').selectedIndex].value;
}

function show_ax(id)
{
  var blocks = document.getElementsByTagName("div");
  var i;
	for(i = 0; i < blocks.length; i++)
	{
	    var block = blocks.item(i);
	    if(block.className != "section")
	        continue;
			block.style.display = "none";
	}

	document.getElementById(id).style.display = 'block';
	return false;
}

function hide_element(id)
{
	document.getElementById(id).style.display = 'none';
	return false;
}

function show_element(id)
{
	document.getElementById(id).style.display = 'block';
	return false;
}

function show_art_input(val)
{
	if (val)
	{
		document.getElementById('input_online').style.display = 'none';
		document.getElementById('input_art').style.display = 'block';
	}
	else
	{
		document.getElementById('input_online').style.display = 'block';
		document.getElementById('input_art').style.display = 'none';
	}
}

function round_number(rnum)
{
	var rlength = 0; // number of decimal places to round to
	if (rnum > 8191 && rnum < 10485)
  {
		rnum = rnum - 5000;
		return (Math.round(rnum * Math.pow(10, rlength)) / Math.pow(10, rlength)) + 5000;
	}
  else
		return Math.round(rnum * Math.pow(10, rlength)) / Math.pow(10, rlength);
}

function adjust_dimensions(width_value)
{
	if (!document.getElementById('constrain_proportions').checked) return;

	var ori_w = document.getElementById('ori_w').value;
	var ori_h = document.getElementById('ori_h').value;

	if (width_value)
		document.getElementById('h').value = round_number(document.getElementById('w').value * (ori_h / ori_w));
	else
		document.getElementById('w').value = round_number(document.getElementById('h').value * (ori_w / ori_h));
}

function set_comment_actions(value)
{
	for (var i=0; i< document.comment_list.length; i++)
		if (document.comment_list[i].value == value)
			document.comment_list[i].checked = true;
	return false;
}

function check_comments(value)
{
	for (var i=0; i< document.comment_list.length; i++)
		if (document.comment_list[i].type == 'checkbox')
			document.comment_list[i].checked = value;
	return false;
}


function resize_box_heights()
{
  // Resizes boxes containing gallery pictures.
  // This function finds the tallest box and resizes all boxes to the same height. 
  // This way the boxes are arrayed properly with variable length captions.
  var blocks = document.getElementsByTagName("div");
  var i;
  var h = 100;
  var block;
  var w = 0;

	for(i = 0; i < blocks.length; i++)
  {
    block = blocks.item(i);
    if (block.className == "ns_float_image" && block.offsetHeight > h)
      h = block.offsetHeight;   
  }

	for(i = 0; i < blocks.length; i++)
  {
    block = blocks.item(i);
    if(block.className == "ns_float_image")
      block.style.height = h + 'px';
  }
}

function highlight(id, value)
{
  document.getElementById(id).style.backgroundPosition = (value ? "bottom" : "top");
}