var PATH_TO_ROOT = "/"; //"../../" w makiecie, "/" na lotos.3net.pl

/*mootools*/

/* equal-height */
jQuery(document).ready(function(){
	if(jQuery("#contentInner").height() + jQuery("#contentT").height() < jQuery("#sidebar").height()) {
		jQuery("#contentInner").height(jQuery("#sidebar").height() - jQuery("#contentT").height());
	}
});

/*video and audio player*/
mediaPlayerizeElement = function(el, type) {
  if(type == "mp3") {
    var height = 20;
  } else {
    var height = 320;
  }
  var url = jQuery(el).find('a:first-child').attr("href");
  var rand = Math.round(Math.random() * 1000);
  var parentDiv = jQuery(el).parent();
  if(parentDiv.hasClass("file" == false)) {
    //console.log("Zle wywolanie media playera");
  } else {
    var mplayid = "mediaplayer"+rand+"div";
    jQuery(el).before('<div id="'+mplayid+'" class="'+type+'"></div>');
    var s1 = new SWFObject(PATH_TO_ROOT+"flash/mediaplayer.swf","mediaplayer"+rand+"swf","426", height,"7");
    s1.addVariable("screencolor","0xEFEFEF"); s1.addParam("allowfullscreen","true"); s1.addVariable("type", type); s1.addVariable("width","426"); s1.addVariable("height", height); s1.addVariable("file",url);
    if (typeof customVideoSplash != "undefined") { s1.addVariable("image", customVideoSplash); }
    else {
      if (String(window.location).indexOf("lotostrada") != -1) {
        s1.addVariable("image", PATH_TO_ROOT+"images/lotostrada/lotostrada_player_logo.png");
        } else {
        s1.addVariable("image", PATH_TO_ROOT+"images/strapon.png");
        }
    }
    s1.write(mplayid);
  }
}
jQuery('#content .flvLink').each(function (i, el) {
  mediaPlayerizeElement(el, "flv");
});
jQuery('#content .mp3Link').each(function (i, el) {
  mediaPlayerizeElement(el, "mp3");
});

/*h3 to sectionTitles*/
jQuery('#content .section').each(function(i){
  var childs = this.childNodes;
  var childsBucket = new Array();
  for(var i=childs.length-1; i>=0; i--) {
    childsBucket.push(childs[i]);
    if(childs[i].nodeName.toLowerCase() == "h3") {
      childsBucket.reverse();
      jQuery(childsBucket).insertAfter(this).wrapAll('<div class="section"></div>');
      childsBucket = new Array();
    }
  }
  childsBucket = new Array();
});
jQuery('#content .section > h3').each(function(i){
  jQuery(this).wrap('<div class="sectionTitle"></div>');
});

/*menu drag 'n' drop*/
injectIntoShifter = function(el) {
  jQuery(el).prepend('<div class="shifter"><div class="shiftUp"></div><div class="shiftDown"></div></div>');
}
/*jQuery('#menu .sortable, #content .sortable').each(function(i, el){
  injectIntoShifter(el);
});*/
jQuery('#menu .sortable').each(function(i, el){
  injectIntoShifter(el);
});
jQuery('#menu .sortable').css('cursor', 'move');

if (jQuery('#menu').sortable)
{
	jQuery('#menu').sortable({
		handle: 'h3',
		axis: 'y',
		update: function() {
			updateSorting('#menu');
			saveItems(jQuery('#menu .sortable'));
		}
	});
}

function show(dump)
{
	if (console && console.log)
	{
		console.log(dump);
	}
}

cookieSet = function(name, value, days, currentLocation)
{
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
		var expires = '; expires=' + date.toGMTString();
	}
	else
	{
		var expires = '';
	}

	if (typeof currentLocation != 'undefined' && currentLocation && false)
	{
		document.cookie = name + '=' + value + expires + '; path=' + document.location.pathname;
	}
	else
	{
		document.cookie = name + '=' + value + expires + '; path=/';
	}
}

cookieGet = function(name)
{
	var nameEQ = name + '=';
	var ca = document.cookie.split(';');

	for (var i=0 ; i < ca.length ; i++)
	{
		var c = ca[i];
		while (c.charAt(0) == ' ')
		{
			c = c.substring(1, c.length);
		}
		if (c.indexOf(nameEQ) == 0)
		{
			return c.substring(nameEQ.length, c.length);
		}
	}

	return '';
}

function getSections()
{
	var query = '?do=getSections';

	jQuery.ajax(
		{
   			type: "POST",
   			url: "/json.php?do=getSections",
   			data: "",
   			success: function(ret)
   			{
	   			if (ret == 'cookie')
				{
					var sections = cookieGet('SECTIONS').split('|');
				}
				else
				{
					var sections = ret.split('|');
				}

				if (sections.length > 0)
				{

					jQuery('#section_' + sections[0]).insertBefore(
						jQuery('#' + jQuery('#menu .sortable')[0].parentNode.id)
					);

					for (var i = 1 ; i < sections.length ; i++)
					{
						jQuery('#section_' + sections[i]).insertAfter(
							jQuery('#section_' + sections[i-1])
						);
					}
				}
			}
		}
	);
}

saveItems = function(elements)
{
	var currentPosition = new Array();

	for (var i = 0 ; i < elements.length - 1 ; i++)
	{
		currentPosition[i] = elements[i].parentNode.id.replace(/section_/g, '');
	}

	var sections = (currentPosition.length > 0) ? currentPosition.join('|') : '';

	cookieSet('SECTIONS', sections, 356, true);

	jQuery.ajax(
		{
   			type: "POST",
   			url: "/json.php?do=saveSections&sections=" + sections,
   			data: "",
   			success: function(ret)
   			{

			}
		}
	);
}

moveItem = function(el, dir) {
  el = jQuery(el);
	if (dir < 0)  el.insertBefore(el.prev());
	if (dir > 0)  el.insertAfter(el.next());
	updateSorting(el.parent());
	saveItems(jQuery('#menu .sortable'));
}
updateSorting = function(elements) {
  var elements = jQuery(elements);
  if(elements) {
    elements.data("childs", elements.find('> *:not(.ui-sortable-helper)').length);
    elements.find('.shiftUp').each(function(i){
      if(i == 0) jQuery(this).css('visibility', 'hidden');
      else jQuery(this).css('visibility', '');
      this.onclick = function(){moveItem(this.parentNode.parentNode.parentNode, -1)}}
    );
    elements.find('.shiftDown').each(function(i){
      if(i == jQuery(this).parent().parent().parent().parent().data("childs") - 1) {
        jQuery(this).css('visibility', 'hidden');
      }
      else jQuery(this).css('visibility', '');
      this.onclick = function(){moveItem(this.parentNode.parentNode.parentNode, 1)}}
    );
  }
}
updateSorting('#menu');
//updateSorting('#content');


/*contacts*/
function applyContactBehavior() {
  jQuery('#contacts li:not(.active)').addClass("inactive");
  jQuery('#contacts .expand').each(function(i, el){
    el.onclick = function() {
      var contactLi = jQuery(this.parentNode.parentNode.parentNode);
      if(contactLi.hasClass("active")) {
        contactLi.addClass("inactive").removeClass("active");
        return true;
      }
      else {
        jQuery('#contacts li.active').addClass("inactive").removeClass("active");
      }
      contactLi.addClass("active").removeClass("inactive");
      contactLi.find('a').each(function(i){
        if(jQuery(this).width() > 143) {
          var addr = jQuery(this).text();
          var pos = addr.indexOf("lotos");
          if(addr.indexOf("grupalotos") > -1) {
            var str1 = addr.slice(0, pos); var str2 = addr.slice(pos, addr.length);
          }
          else {
            var str1 = addr.slice(0, pos+5); var str2 = addr.slice(pos+5, addr.length);
          }
          jQuery(this).html(str1+"<br />"+str2);
        }
      });
    }
  });
}
applyContactBehavior();
function dodajKontakt(url) {
  var ajax = new sack();
  var request=url;
  ajax.requestFile = request;
  ajax.method = 'POST';
  ajax.onCompletion =  function(){
  		if(ajax.response.indexOf('#KOM')!=-1) {
  			// alert(ajax.response);
  		}
  		else {
  			document.getElementById('div_twoje_kontakty').innerHTML=ajax.response;
  			applyContactBehavior();
  		}
  	};
  ajax.runAJAX();
}

/*slownik*/
if(jQuery('#dict').length > 0)
{
	function sendDisplayInfo(title)
	{
		jQuery.post(
			'/server.php?akcja=slownik_wyswietlenie',
			{ tytul : title, profil : jQuery('#dictProfile').attr('value') }
		);
	}

	var currentActiveDict = undefined;
	var myFile = document.location.toString();
	if (myFile.match('#')) // the URL contains an anchor
	{
		// click the navigation item corresponding to the anchor
		var myAnchor = '#' + decodeURIComponent(myFile.split('#')[1]);
		jQuery('#dict dd a').each(function() {
			if(jQuery(this).attr('href') == myAnchor)
			{
				jQuery(this.parentNode).addClass("active");
				currentActiveDict = jQuery(this.parentNode);
				sendDisplayInfo(myAnchor);
			}
		});
	}

	jQuery('#dict dd a').each(function(i, el){
		el.onclick = function() {
			if(jQuery(this.parentNode).hasClass("active")) {
				jQuery(this.parentNode).removeClass("active");
				return true;
			}
			if(currentActiveDict == undefined) {
				jQuery('#dict dd').each(function(i, el) {
					if(jQuery(el).hasClass("active"))
						currentActiveDict = jQuery(el);
				});
			}
			if(currentActiveDict != undefined)
				currentActiveDict.removeClass("active");
			jQuery(this.parentNode).addClass("active");
			currentActiveDict = jQuery(this.parentNode);
			sendDisplayInfo(jQuery(this).attr('href'));
			return false;
		}
	});
	jQuery('#dict dd .tip').each(function(i, el) {
		el.onclick = function()  {
			jQuery(this.parentNode).removeClass("active");
			return true;
		}
	});
}

/*closables*/
/*jQuery('#content .closable').each(function(i, el){
  jQuery(el).prepend('<a title="Zamknij" href="#"><img src="'+PATH_TO_ROOT+'images/icon_close1.gif" alt="Zamknij" class="close" /></a>');
});*/


/*sIFR*/
if(typeof sIFR == "function"){
/*Mateusz edit 5.9.2008*/
var pageAddress = String(window.location);
  if (pageAddress.indexOf("/ru/") != -1) {
  //wersja rosyjska
  	if(jQuery("body").hasClass("konsumencki")) {
  	  sIFR.replaceElement(named({sSelector:"#content .sectionTitle h3", sFlashSrc:PATH_TO_ROOT+"flash/sifrMyriadPro.swf", sColor:"#FFFFFF", sBgColor:"#2F3139", sWmode:"transparent"}));
  		sIFR.replaceElement(named({sSelector:".boxUpload .sectionTitle h3", sFlashSrc:PATH_TO_ROOT+"flash/sifrMyriadPro.swf", sColor:"#FFFFFF", sBgColor:"#2F3139"}));
  		sIFR.replaceElement(named({sSelector:".userInfo .desc h4", sFlashSrc:PATH_TO_ROOT+"flash/sifrMyriadPro.swf", sColor:"#FFFFFF", sBgColor:"#2F3139", sWmode:"transparent"}));
  		sIFR.replaceElement(named({sSelector:"#dict table h3", sFlashSrc:PATH_TO_ROOT+"flash/sifrMyriadPro.swf", sColor:"#FFFFFF", sBgColor:"#2F3139", sWmode:"transparent"}));
  		sIFR.replaceElement(named({sSelector:".appHead", sFlashSrc:PATH_TO_ROOT+"flash/sifrMyriadPro.swf", sColor:"#FFFFFF", sBgColor:"#f2f2f2"}));
  		sIFR.replaceElement(named({sSelector:".appHeadGrey", sFlashSrc:PATH_TO_ROOT+"flash/sifrMyriadPro.swf", sColor:"#FFFFFF", sBgColor:"#f2f2f2"}));
  		sIFR.replaceElement(named({sSelector:".posName h2", sFlashSrc:PATH_TO_ROOT+"flash/sifrMyriadPro.swf", sColor:"#FFFFFF", sBgColor:"#FFFFFF", sWmode:"transparent"}));
  		sIFR.replaceElement(named({sSelector:".abName", sFlashSrc:PATH_TO_ROOT+"flash/sifrMyriadPro.swf", sColor:"#FFFFFF", sBgColor:"#FFFFFF", sWmode:"transparent"}));
  		sIFR.replaceElement(named({sSelector:".stHeader", sFlashSrc:PATH_TO_ROOT+"flash/sifrMyriadPro.swf", sColor:"#FFFFFF", sWmode:"transparent"}));
    } else {
  		sIFR.replaceElement(named({sSelector:"#content .sectionTitle h3", sFlashSrc:PATH_TO_ROOT+"flash/sifrMyriadPro.swf", sColor:"#494949", sBgColor:"#FFFFFF", sWmode:"transparent"}));
  		sIFR.replaceElement(named({sSelector:".boxUpload .sectionTitle h3", sFlashSrc:PATH_TO_ROOT+"flash/sifrMyriadPro.swf", sColor:"#494949", sBgColor:"#FFFFFF"}));
  		sIFR.replaceElement(named({sSelector:".userInfo .desc h4", sFlashSrc:PATH_TO_ROOT+"flash/sifrMyriadPro.swf", sColor:"#656565", sBgColor:"#F5F5F5", sWmode:"transparent"}));
  		sIFR.replaceElement(named({sSelector:"#dict table dt", sFlashSrc:PATH_TO_ROOT+"flash/sifrMyriadPro.swf", sColor:"#8F8F8F", sBgColor:"#FFFFFF", sWmode:"transparent"}));
  		sIFR.replaceElement(named({sSelector:".appHead", sFlashSrc:PATH_TO_ROOT+"flash/sifrMyriadPro.swf", sColor:"#495692", sBgColor:"#f2f2f2"}));
  		sIFR.replaceElement(named({sSelector:".appHeadGrey", sFlashSrc:PATH_TO_ROOT+"flash/sifrMyriadPro.swf", sColor:"#636363", sBgColor:"#f2f2f2"}));
  		sIFR.replaceElement(named({sSelector:".posName h2", sFlashSrc:PATH_TO_ROOT+"flash/sifrMyriadPro.swf", sColor:"#495692", sBgColor:"#FFFFFF", sWmode:"transparent"}));
  		sIFR.replaceElement(named({sSelector:".abName", sFlashSrc:PATH_TO_ROOT+"flash/sifrMyriadPro.swf", sColor:"#494949", sBgColor:"#FFFFFF", sWmode:"transparent"}));
  		sIFR.replaceElement(named({sSelector:".stHeader", sFlashSrc:PATH_TO_ROOT+"flash/sifrMyriadPro.swf", sColor:"#494949", sWmode:"transparent"}));
    }

  } else {
  	if(jQuery("body").hasClass("konsumencki")) {
  		//profil konsumencki
  		sIFR.replaceElement(named({sSelector:"#content .sectionTitle h3", sFlashSrc:PATH_TO_ROOT+"flash/humnst777cnpl.swf", sColor:"#FFFFFF", sBgColor:"#2F3139", sWmode:"transparent"}));
  		sIFR.replaceElement(named({sSelector:".boxUpload .sectionTitle h3", sFlashSrc:PATH_TO_ROOT+"flash/humnst777cnpl.swf", sColor:"#FFFFFF", sBgColor:"#2F3139"}));
  		sIFR.replaceElement(named({sSelector:".userInfo .desc h4", sFlashSrc:PATH_TO_ROOT+"flash/humnst777cnpl.swf", sColor:"#FFFFFF", sBgColor:"#2F3139", sWmode:"transparent"}));
  		sIFR.replaceElement(named({sSelector:"#dict table h3", sFlashSrc:PATH_TO_ROOT+"flash/humnst777cnpl.swf", sColor:"#FFFFFF", sBgColor:"#2F3139", sWmode:"transparent"}));
  		sIFR.replaceElement(named({sSelector:".appHead", sFlashSrc:PATH_TO_ROOT+"flash/humnst777cnpl.swf", sColor:"#FFFFFF", sBgColor:"#f2f2f2"}));
  		sIFR.replaceElement(named({sSelector:".appHeadGrey", sFlashSrc:PATH_TO_ROOT+"flash/humnst777cnpl.swf", sColor:"#FFFFFF", sBgColor:"#f2f2f2"}));
  		sIFR.replaceElement(named({sSelector:".posName h2", sFlashSrc:PATH_TO_ROOT+"flash/humnst777cnpl.swf", sColor:"#FFFFFF", sBgColor:"#FFFFFF", sWmode:"transparent"}));
  		sIFR.replaceElement(named({sSelector:".abName", sFlashSrc:PATH_TO_ROOT+"flash/humnst777cnpl.swf", sColor:"#FFFFFF", sBgColor:"#FFFFFF", sWmode:"transparent"}));
  		sIFR.replaceElement(named({sSelector:".stHeader", sFlashSrc:PATH_TO_ROOT+"flash/humnst777cnpl.swf", sColor:"#FFFFFF", sWmode:"transparent"}));
  	}
  	else {
  		//pozostale profile
  		sIFR.replaceElement(named({sSelector:"#content .sectionTitle h3", sFlashSrc:PATH_TO_ROOT+"flash/humnst777cnpl.swf", sColor:"#494949", sBgColor:"#FFFFFF", sWmode:"transparent"}));
  		sIFR.replaceElement(named({sSelector:".boxUpload .sectionTitle h3", sFlashSrc:PATH_TO_ROOT+"flash/humnst777cnpl.swf", sColor:"#494949", sBgColor:"#FFFFFF"}));
  		sIFR.replaceElement(named({sSelector:".userInfo .desc h4", sFlashSrc:PATH_TO_ROOT+"flash/humnst777cnpl.swf", sColor:"#656565", sBgColor:"#F5F5F5", sWmode:"transparent"}));
  		sIFR.replaceElement(named({sSelector:"#dict table dt", sFlashSrc:PATH_TO_ROOT+"flash/humnst777cnpl.swf", sColor:"#8F8F8F", sBgColor:"#FFFFFF", sWmode:"transparent"}));
  		sIFR.replaceElement(named({sSelector:".appHead", sFlashSrc:PATH_TO_ROOT+"flash/humnst777cnpl.swf", sColor:"#495692", sBgColor:"#f2f2f2"}));
  		sIFR.replaceElement(named({sSelector:".appHeadGrey", sFlashSrc:PATH_TO_ROOT+"flash/humnst777cnpl.swf", sColor:"#636363", sBgColor:"#f2f2f2"}));
  		sIFR.replaceElement(named({sSelector:".posName h2", sFlashSrc:PATH_TO_ROOT+"flash/humnst777cnpl.swf", sColor:"#495692", sBgColor:"#FFFFFF", sWmode:"transparent"}));
  		sIFR.replaceElement(named({sSelector:".abName", sFlashSrc:PATH_TO_ROOT+"flash/humnst777cnpl.swf", sColor:"#494949", sBgColor:"#FFFFFF", sWmode:"transparent"}));
  		sIFR.replaceElement(named({sSelector:".stHeader", sFlashSrc:PATH_TO_ROOT+"flash/humnst777cnpl.swf", sColor:"#494949", sWmode:"transparent"}));
  	}
  }
};

sIFR.replaceElement(named({sSelector:".raportH", sFlashSrc:PATH_TO_ROOT+"flash/humnst777cnpl.swf", sColor:"#494949", sWmode:"transparent"}));

jQuery(".moreForm a").click(function () {
	jQuery(".addit-opt").toggle();
	jQuery(this).toggleClass("visible");
	return false;
});
