/*
	Cross-Browser Split 0.3
	By Steven Levithan <http://stevenlevithan.com>
	MIT license
	Provides a consistent cross-browser, ECMA-262 v3 compliant split method
*/
String.prototype._$$split = String.prototype._$$split || String.prototype.split;

String.prototype.split = function (s /* separator */, limit) {
	// if separator is not a regex, use the native split method
	if (!(s instanceof RegExp))
		return String.prototype._$$split.apply(this, arguments);

	var	flags = (s.global ? "g" : "") + (s.ignoreCase ? "i" : "") + (s.multiline ? "m" : ""),
		s2 = new RegExp("^" + s.source + "$", flags),
		output = [],
		origLastIndex = s.lastIndex,
		lastLastIndex = 0,
		i = 0, match, lastLength;

	/* behavior for limit: if it's...
	- undefined: no limit
	- NaN or zero: return an empty array
	- a positive number: use limit after dropping any decimal
	- a negative number: no limit
	- other: type-convert, then use the above rules
	*/
	if (limit === undefined || +limit < 0) {
		limit = false;
	} else {
		limit = Math.floor(+limit);
		if (!limit)
			return [];
	}

	if (s.global)
		s.lastIndex = 0;
	else
		s = new RegExp(s.source, "g" + flags);

	while ((!limit || i++ <= limit) && (match = s.exec(this))) {
		var emptyMatch = !match[0].length;

		// Fix IE's infinite-loop-resistant but incorrect lastIndex
		if (emptyMatch && s.lastIndex > match.index)
			s.lastIndex--;

		if (s.lastIndex > lastLastIndex) {
			// Fix browsers whose exec methods don't consistently return undefined for non-participating capturing groups
			if (match.length > 1) {
				match[0].replace(s2, function () {
					for (var j = 1; j < arguments.length - 2; j++) {
						if (arguments[j] === undefined)
							match[j] = undefined;
					}
				});
			}

			output = output.concat(this.slice(lastLastIndex, match.index));
			if (1 < match.length && match.index < this.length)
				output = output.concat(match.slice(1));
			lastLength = match[0].length; // only needed if s.lastIndex === this.length
			lastLastIndex = s.lastIndex;
		}

		if (emptyMatch)
			s.lastIndex++; // avoid an infinite loop
	}

	// since this uses test(), output must be generated before restoring lastIndex
	output = lastLastIndex === this.length ?
		(s.test("") && !lastLength ? output : output.concat("")) :
		(limit ? output : output.concat(this.slice(lastLastIndex)));
	s.lastIndex = origLastIndex; // only needed if s.global, else we're working with a copy of the regex
	return output;
};
/* END Cross-Browser Split 0.3 */

/**
 * @author J2media
 * @copyright 2008-2010
 */
function slideSwitch() {
    
    if ($('#flsh img').length == 0) { return; }
    
    if ($('#flsh img').length == 1) { 
    	$('#flsh img:first').clone().appendTo('#flsh');
    }

    var $active = $('#flsh img.active');
    //if ( $active.length == 0 ) $active = $('#flsh img:first');
    var $next =  $active.next().length ? $active.next() : $('#flsh img:first');

    $active.addClass('last-active').removeClass('active');

    var ww = $(window).width();
    var wh = $(window).height();
    var nw = $(window).width();
    var nh = (wh/ww)*nw;
    var res = 100;
    var nresw = res;
    var nresh = (wh/ww)*nresw;
    var randomcorner=Math.floor(Math.random()*4);
    if (randomcorner==0) {
    	$next.css({ top: 'auto', right: 0, bottom: 0, left: 'auto' });	    	
    } else if (randomcorner==1) {
    	$next.css({ top: 'auto', right: 'auto', bottom: 0, left: 0 });
    } else if (randomcorner==2) {
    	$next.css({ top: 0, right: 'auto', bottom: 'auto', left: 0 });
    } else if (randomcorner==3) {
    	$next.css({ top: 0, right: 0, bottom: 'auto', left: 'auto' });
    	//opts.cssBefore = { display: 'block', zIndex: 1, opacity: 0, , width: nw+200, height: nh+200 };
    }
    $next.css({opacity: '0.0', width: (nw+nresw)+'px', height: (nh+nresh)+'px'})
      	.addClass('active')
       	//.prependTo('#flsh')
       	.animate({opacity: 1.0}, 1000, 'linear')
       	.animate({width: nw, height: nh}, 5000, 'linear', function() {
       			$active.removeClass('active last-active');
       			setTimeout( "slideSwitch()", 100 );
       	});
}

$(function(){
	var inputs = [];
	//inputs.push($('#frm1').serialize());
	inputs.push('width='+$(window).width());
	inputs.push('height='+$(window).height());
	$.ajax({
		dataType: 'json',
		data: inputs.join('&'),
		url: '/,'+cpage+'/photo/slides',
		async: 'true',
		type: 'post',
		error: function() {
		},
		success: function(items,element) {
			$.each(items,function() {
				$('<img src="'+this.url+'" />').css({
					display: 'block',
					opacity: 0.0,
					width: $(window).width(),
					height: $(window).height(),
				}).appendTo($("#flsh"));
			});
			if ($("#flsh").attr('disabled') == "true") {
				//$("#flsh img").animate({opacity:1},500);
				$("#flsh img").css({opacity: 1});
			} else {
				setTimeout( "slideSwitch()", 1000 );
			}
/*			$('#flsh').cycle({ 
			    fx:     'scalefade', 
			    speed:   8000, 
			    timeout: 0, 
			    next:   '#flsh'
			});			
*/
		}
	});

	var hiderunning,hiderunning2;
	function showDIV(item) { $(item).css('display','block'); }
	function hideDIV(item) { $(item).css('display','none'); }

	function mnushow(set,item) {
	  clearTimeout(hiderunning);
	  mnuhiderun(set,'all');
		if ( item!=0 && $(set+'_'+item) ) {
	    $(set+'_'+item).css('display','block');		      
	    holder = $(set);
	    element = $('a',holder);
	    for (var i = 0; i < element.length; i++) { 
	      if (element[i].className=='h'+item+'off') {
	        element[i].className='h'+item+'on';
	  } } } }
	function mnuhide(set,item) {
	    hiderunning = setTimeout('mnuhiderun(\''+set+'\',\''+item+'\')',500);
	}
	function mnuhiderun(set,item) {
		if (item=='all') {
			holder = $(set);
			element = $('div',holder);
			for (var i = 0; i < element.length; i++) { if (element[i].className=='mnuItm') element[i].style.display='none'; }
			element = $('a',holder);
			for (var i = 0; i < element.length; i++) { 
				element[i].className='h'+(i+1)+'off';
		}; }
		else if ( item!=0 && $(set+'_'+item) ) {
			$(set+'_'+item).style.display='none';		      
			holder = $(set);
			element = $('a',holder);
			for (var i = 0; i < element.length; i++) { 
				if (element[i].className=='h'+item+'off' || element[i].className=='h'+item+'on') {
					element[i].className='h'+item+'off';
		}; }; };
	};
		
	$('.mnu0 li.l0').mouseover(function(eO){
		$('.mnu0 ul').css('display','none');
		$('ul',this).css('display','block');
	});
	
	var initial_height = 0;
	var initial_min_height = 0;
	$('#hndl1').css('cursor','pointer').click(function(){
		if ($(this).attr('src') === '/gfx/btn_hndl1-off.png') {
			initial_height = $(this).parent().css('height');
			$(this).parent().animate({'height':'0px'}, 500);
			$(this).attr('src','/gfx/btn_hndl1-on.png');
		} else {
			$(this).parent().animate({'height':initial_height}, 500);
			$(this).attr('src','/gfx/btn_hndl1-off.png');
		}
	});

	var height_bxo1_init = $(window).height()-115-72-80-68;
	var height_bxo2_init = $(window).height()-115-72-80-133;
	height_bxo1 = Math.floor(height_bxo1_init/220);
	if (height_bxo1 == 0) height_bxo1 = height_bxo1_init;
	else height_bxo1 = height_bxo1*220;
	height_bxoi1 = height_bxo1_init;
	height_bxo2 = Math.floor(height_bxo2_init/187); 
	if (height_bxo2 == 0) height_bxo2 = height_bxo2_init;
	else height_bxo2 = height_bxo2*187;
	$('.bxo1').css('height',(height_bxo1+50)+'px');
	$('.bxo1s').css('height',(height_bxo1)+'px');
	$('.bxo2').css('height',(height_bxo2+50)+'px');
	$('.bxo2s').css('height',(height_bxo2)+'px');
	$('.bxoi1').css('height',(height_bxoi1+50)+'px');
	$('.bxoi1s').css('height',(height_bxoi1)+'px');
	var height_aqua_init = $(window).height()-115-72-30-30;
	if (height_aqua_init < 536) {
		$('.aqua-itmh').css('height',(height_aqua_init)+'px');
	}
	var height_rez_init = $(window).height()-115-72-80-68;
	if (height_rez_init < 446) {
		$('.bxor').css('height',(height_rez_init+50)+'px');
		$('.bxor .rez').css('padding','0 4px 0 16px');
		$('.bxorh').css('height',(height_rez_init)+'px');
		$('.bxor .btnres').css({display: 'block'}).html('powiększ');
	}
	
	$('.aqua-nav').click(function(e){
		e.preventDefault();
		$('#'+this.id+'-itm').show().siblings().hide();
	});
	
	$('.ear .hndl').click(function(e) {
		e.preventDefault();
		if ($(this).attr('active') == "true") {
			$(this).attr('active',false).parent().animate({ left: '-390px' }, {
				duration: 1000,
				complete: function(){
					$(this).css('z-index',parseInt($(this).css('z-index'))-1)
					$(this).siblings().each(function(){
						$(this).css('z-index',parseInt($(this).css('z-index'))+1);
					});
			}});
		} else {
			$(this).parent().siblings().each(function(){
				$(this).css('z-index',$(this).css('z-index')-1);
			});
			$(this).attr('active',true).parent().css('z-index',parseInt($(this).parent().css('z-index'))+1).animate({ left: '0px' }, 1000);
			fbon = true;
		}
	});

	var height_art_init = $(window).height()-115-72-28;
	if (height_art_init < 300) {
		$('.cnt03').css('height',height_art_init-3);
	} else if (height_art_init < 1000) {
		$('.cnt03').css('height','300px');
	} else {
		$('.cnt03').css('height','30%');
	}

	$('.scrlp,.arth').jScrollPane({
		showArrows: true,
		horizontalGutter: 10
	});

});
