// prefs
var now = new Date();
fixDate(now);
now.setTime(now.getTime() + 365 * 24 * 60 * 60 * 1000);
var siteUrl = ''; //window.location.protocol + '//' + window.location.host;
var prefsCookie = 'bebikoPrefs';
var prefsPath = "/";



// instrukcje wykonywane po załadowaniu dokumentu
startList = function() {
// timeline
	if($('#timeline') && $('#timeline').length){
		initTimeline();
		setTimeline();
	}

// osadzanie flashy
	embedFlashElement();
	
// przełączanie produktów
	if ($('#products') && $('#products').length){
		initProductsSwitch();
	}
	
	if ($('#sendToFriendForm') && $('#sendToFriendForm').length){
		initSendToFriend()
	}

// powiększanie fontów w artykule
	if ($('#article') && $('#article').length){
		initFontSize();
	}

// zarządzanie polami input i submit
	if($(':text') && $(':text').length){ $(':text').each(function(){ manageInputValue(this) }); }
	if($(':submit') && $(':submit').length){ $(':submit').attr('value', ''); }

// zakładki na stronie głównej
	if($('div.colHeader ul li:not(:first-child) a') && $('div.colHeader ul li:not(:first-child) a').length){
		initMainPageTabs();
	}

// box z produktami w sidebarze
	if ($('#sdbProductsFlash') && $('#sdbProductsFlash').length){
		var flashvars = {};
		var params = {};
		params.wmode = "transparent";
		var attributes = {};
		// swfobject.embedSWF("images/swf/boxes/products_small.swf", "sdbProductsFlash", "201", "202", "9.0.0", false, flashvars, params, attributes);
		swfobject.embedSWF("/themes/bebiko/images/swf/boxes/products_small.swf", "sdbProductsFlash", "201", "202", "9.0.0", false, flashvars, params, attributes);
	}

// strona główna header
	if ($('#hdrBoxL01') && $('#hdrBoxL01').length){
		var flashvars = {};
		var params = {};
		params.wmode = "transparent";
		var attributes = {};
		// swfobject.embedSWF("images/swf/headers/main.swf", "hdrBoxL01Flash", "762", "208", "9.0.0", false, flashvars, params, attributes);
		swfobject.embedSWF("/themes/bebiko/images/swf/headers/main.swf", "hdrBoxL01Flash", "762", "208", "9.0.0", false, flashvars, params, attributes);
	}

// fancybox
	if($("div.photoBorder a") && $("div.photoBorder a").length){ $("div.photoBorder a").fancybox({ 'titlePosition' : 'inside' }); }

// lightbox me
	if($('a.showPopUp') && $('a.showPopUp').length){
		$('a.showPopUp').click(function() {
			var id = $(this).attr('href').match(/#.*/);
			 $(id[0]).lightbox_me({ centered: true });
			return false;
		});
	}
	if($('a#wyslijTeczke') && $('a#wyslijTeczke').length){
		lightboxMe('a#wyslijTeczke', '#sendToFriendForm');
	}
	if($('a.sendArticle') && $('a.sendArticle').length){
		lightboxMe('a.sendArticle', '#sendToFriendForm');
	}
// registration form
	if($('#RegistrationForm_Registration') && $('#RegistrationForm_Registration').length){
		$('#RegistrationForm_Registration_InfoSource').bind('change', function(){
			if($(this).val() == 'inne'){ $('#regInfoSource').slideDown('fast'); }
			else { $('#regInfoSource').slideUp('fast'); }
		})

		$('input[name=FirstBorn]').bind('click', function(){
			if($('#RegistrationForm_Registration_FirstBorn_Nie:checked').length){ $('#regFirstBorn').slideDown('fast'); }
			else { $('#regFirstBorn').slideUp('fast'); }
		})
	}
	
// zamów poradnik - first born
	if($('#PoradnikForm_Poradnik') && $('#PoradnikForm_Poradnik').length){
		$('input[name=FirstBorn]').bind('click', function(){
			if($('#RegistrationForm_Registration_FirstBorn_Nie:checked').length){ $('#regFirstBorn').slideDown('fast'); }
			else { $('#regFirstBorn').slideUp('fast'); }
		})
	}

// bezpośrednie linki do galerii
	if($('#gallery') && $('#gallery').length){
		initGalleryDirectLink();
	}
// sliders
	if($('#sliderLogin') && $('#sliderLogin').length){
		setSlider('#sliderLogin');
	}
	if($('#sliderFolder') && $('#sliderFolder').length){
		setSlider('#sliderFolder');
	}
	

// ie6 fix
	if (document.all && document.getElementById) {
	  prepareMenu('menu');
	}

}
window.onload = startList;



// zarządzanie tekstami popowiedzi w polach input
function manageInputValue (iField){
  var iField = $(iField);
  iField.inputContent = '';
  if ( iField.attr('value') && iField.attr('value').length ){ 
    iField.bind("focus", function(e){
      if (!iField.inputContent){ 
        iField.inputContent = iField.attr('value');
        iField.attr('value','');
      }
      else {
        if (iField.attr('value') == iField.inputContent){
          iField.attr('value','');
        }
      }
    });
    iField.bind("blur", function(e){
      if (!iField.attr('value').length){
        iField.attr('value', iField.inputContent);
      }
    });
  }
}
// fix dla IE6 i rozwijanego menu
function prepareMenu(id){
  var sfEls = document.getElementById(id).getElementsByTagName("LI");
  for (var i = 0; i < sfEls.length; i++) {
  	sfEls[i].onmouseover = function() {
  		this.className += " over";
  	}
  	sfEls[i].onmouseout = function() {
  		this.className = this.className.replace(new RegExp(" over\\b"), "");
    }
  }
}
// przełączanie stron z produktami
function contentSwitch(className, id){
	$('div.' + className).removeClass('invisible').addClass('invisible');
	$('#pr' + id).removeClass('invisible');
	window.location.hash = '#' + id;
}
// powiększanie fontów w artykule
function initFontSize(){
	getFontSize();
	$("#fontSize li a.fs1").bind( "click", function(){ setFontSize(1); } )
	$("#fontSize li a.fs2").bind( "click", function(){ setFontSize(2); } )
	$("#fontSize li a.fs3").bind( "click", function(){ setFontSize(3); } )
}
function setFontSize(size){
	if (size == 1){ var fSize = "1.2em"; }
	if (size == 2){ var fSize = "1.4em"; }
	if (size == 3){ var fSize = "1.6em"; }
	$('#article').css({'font-size' : fSize, 'line-height' : '1.35em'});
	setPrefsValue("fontSize", size);
}
function getFontSize(){
	var value = getPrefsValue("fontSize");
	if (!value){ value = 0; }
	setFontSize(value);
	return value;
}
// timeline
function initTimeline(){
	$('#timeline li a').click(function() {
		if($(this).parent().parent().attr('class') == $(this).parent().attr('id')){
			$(this).parent().parent().removeClass($(this).parent().attr('id'));
			tmlShowContent(0);
			setPrefsValue("timeline", 0);
		}
		else {
			$(this).parent().parent().removeClass().addClass($(this).parent().attr('id'));
			tmlShowContent($(this).parent().attr('id'));
			setPrefsValue("timeline", $(this).parent().attr('id'));
		}
		if($('#articlesList') && $('#articlesList').length){
			return true;
		}
		return false;
	});
	var tmlState = getPrefsValue("timeline");
	if (tmlState == null){ tmlState = 0; }
	if (tmlState == false){ tmlState = 0; }
	tmlShowContent(tmlState);
}
function setTimeline(){
	var id = getPrefsValue("timeline");
	if(id){ $('#' + id + ' a').trigger('click'); }
}
function tmlShowContent(id){
	// strona główna
	if($('#contentStart') && $('#contentStart').length){
		if(id != 0){
			$('.item').removeClass('invisible').addClass('invisible');
			$('#col01pane01 .' + id + ':lt(2)').removeClass('invisible');
			$('#col01pane02 .' + id + ':lt(2)').removeClass('invisible');
			$('#col02pane01 .' + id + ':lt(2)').removeClass('invisible');
			$('#col02pane02 .' + id + ':lt(2)').removeClass('invisible');
		}
		else {
			$('.item').removeClass('invisible').addClass('invisible');
			$('#col01pane01 .item:lt(2)').removeClass('invisible');
			$('#col01pane02 .item:lt(2)').removeClass('invisible');
			$('#col02pane01 .item:lt(2)').removeClass('invisible');
			$('#col02pane02 .item:lt(2)').removeClass('invisible');
		}
	}
	// dział
	if($('#section') && $('#section').length){
		$('#section li').removeClass('selected');
		if(id){
			$('#section li.' + id).addClass('selected');
		}
	}
	// lista artykułów
	if($('#articlesList') && $('#articlesList').length){
		$('#articlesList div.item').removeClass('selected');
		if(id){
			$('#articlesList div.' + id).addClass('selected');
		}
	}
}
// zakładki na stronie głównej
function initMainPageTabs(){
	$('div.colHeader ul li:not(:first-child) a').each(function(){
		var colId  = '#' + $(this).parent().parent().parent().parent().attr('id');
		var paneId = '#' + $(this).attr('name');
		var listId = '#' + $(this).parent().parent().attr('id');
		$(this).bind('click', function(){
			$(listId + ' li').removeClass('on');
			$(this).parent().toggleClass('on');
			$(colId + ' div.colBody > div').removeClass('on');
			$(paneId).addClass('on');
		})
	})
}
// przełączanie produktów
function initProductsSwitch(){
	if(window.location.hash){
		var productId = window.location.hash.substring(1, window.location.hash.length);
		if (productId != 'Bebiko1' && productId != "Bebiko1ha"){
			contentSwitch('page', productId);
		}
	}
	$('#ml02 li a').click(function(){
		var id = $(this).attr('href').match(/\d$/);
		setProductSwf(id);
	});
	var flashvars = {};
	flashvars.product = productId;
	var params = {};
	params.allowscriptaccess = "always";
	params.wmode = "transparent";
	var attributes = {};
	// swfobject.embedSWF("images/swf/headers/products.swf", "hdrFlash", "762", "319", "9.0.0", false, flashvars, params, attributes);
	swfobject.embedSWF("/themes/bebiko/images/swf/headers/products.swf", "hdrFlash", "762", "319", "9.0.0", false, flashvars, params, attributes);
}
// link bezpośredni do galeriii
function initGalleryDirectLink(){
	if(window.location.hash){
		var photoId = window.location.hash.substring(window.location.hash.indexOf('showPhoto=') + 10, window.location.hash.length);
		$('#' + photoId).trigger('click');
	}
}
// cookies
function fixDate(date){
  var base = new Date(0);
  var skew = base.getTime();
  if (skew > 0) {
    date.setTime(date.getTime() - skew);
  }
}
function getCookieValue(offset){
	var endstr = document.cookie.indexOf (';', offset);
		if (endstr == -1) {
			endstr = document.cookie.length;
		}
	return unescape(document.cookie.substring(offset, endstr));
}
function getCookie(name){
	var arg  = name + '=';
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen) {
		var j = i + alen;
		if (document.cookie.substring(i, j) == arg) {
			return getCookieValue(j);
		}
		i = document.cookie.indexOf(' ', i) + 1;
		if (i == 0) break;
	}
}
function setCookie(name, value, expires){
	var argv = setCookie.arguments;
	var argc = setCookie.arguments.length;
	var path = prefsPath;
	var domain = (argc > 4) ? argv[4] : null;
	var secure = (argc > 5) ? argv[5] : false;
	document.cookie = name + '=' + escape(value) +
		((expires == null) ? '' : ('; expires=' + expires.toGMTString())) +
		(('; path=' + path)) +
		((domain  == null) ? '' : ('; domain=' + domain)) +
		((secure  == true) ? '; secure' : '');
}
function deleteCookie(name){
 var cval = getCookie(name);
 document.cookie = name + '=' + cval + '; expires=Thu, 01-Jan-00 00:00:01 GMT; path=' + prefsPath;
}
// prefs
function getPrefsValue(key){
	var theCookie = getCookie(prefsCookie);

	if (theCookie) {
		thePrefs = theCookie.split('|');
		for (i=0; i<thePrefs.length; i++) {
			aPref = thePrefs[i].split('=');
			if (aPref[0] == key) {
				return aPref[1];
			}
		}
	}
	return null;
}
function setPrefsValue(key, value){
	theCookie = getCookie(prefsCookie);
	newPrefs = '';
	if (theCookie) {
		thePrefs = theCookie.split('|');
		for (i=0; i<thePrefs.length; i++) {
			aPref = thePrefs[i].split('=');
			if ((aPref[0].length > 0) && (aPref[0] != key)) {
				newPrefs = newPrefs + ((newPrefs.length > 0) ? '|' : '') + thePrefs[i];
			}
		}
		deleteCookie(prefsCookie);
	}
	newPrefs = newPrefs + ((newPrefs.length > 0) ? '|' : '') + key + '=' + value;
	setCookie(prefsCookie, newPrefs, now, prefsPath);
}
// flash
function setProductSwf(id){
	getFlashMovie("hdrFlash").showProduct(id);
}
function getFlashMovie(movieName) {
	var isIE = navigator.appName.indexOf("Microsoft") != -1;
	return (isIE) ? window[movieName] : document[movieName];
}  
function embedFlashElement(){
	if($('.flashElement') && $('.flashElement').length){
		$('.flashElement').each(function(){
			var settings = $(this).attr('title').split('|');
			var flashvars = {};
			var params = {};
			params.allowscriptaccess = "always";
			params.wmode = "transparent";
			var attributes = {};
			swfobject.embedSWF("images/swf/" + settings[0], $(this).attr('id'), settings[1], settings[2], "9.0.0", false, flashvars, params, attributes);
			// swfobject.embedSWF("/themes/bebiko/images/swf/" + settings[0], $(this).attr('id'), settings[1], settings[2], "9.0.0", false, flashvars, params, attributes);
		})
	
	}
}
// sliders
function setSlider(id){
	$(id).hover(
		function(){ $(this).stop().animate({right: '0'}, 'slow'); },
		function(){ $(this).stop().animate({right: '-195px'}, 'slow'); }
	);
}
// lightboxMe
function lightboxMe(initSelector, receiveSelector){
	$(initSelector).click(function() {
		$('#articleID').attr('value', $(this).attr('id'));
		$(receiveSelector).lightbox_me({
			centered: true
		});
		return false;
	});
}
function initSendToFriend(){
	if(window.location.hash){
		var url = window.location.hash.substring(1, window.location.hash.length);
		if (url == "SendToFriendForm_SendToFriendForm"){
				$('#sendToFriendForm').lightbox_me({
					centered: true
				});
		}
	}
}