jQuery(document).ready(function() {

	// random position
	var ads = jQuery('.azAdContainer.rotate');
	if (ads.length > 1) {
		adsNew = ads.clone().sort(function(){return 0.5 - Math.random();});
		for (var i = 0; i < ads.length; i += 1) {
			jQuery(ads[i]).parent().html(jQuery(adsNew[i]));
		}
	}
	// toggle short-long-text
	jQuery('.tx-smsfoehraz-pi2 .moreLessInfo a').live('click',function(){
		var p = jQuery(this).parents('.azAdContent');
		p.find('.moreLessInfo a').toggleClass('dn');
		p.find('.leftCol').toggleClass('dn');
		return false;
	});

	// premium galleries
	jQuery('.tx-smsfoehraz-pi2 .switch_prev').live('click',function(){pi2_slideGallery('prev',jQuery(this).parent().attr('id').replace('curr_',''));});
	jQuery('.tx-smsfoehraz-pi2 .switch_next').live('click',function(){pi2_slideGallery('next',jQuery(this).parent().attr('id').replace('curr_',''));});
});

function getRandom(min,max) {
	if( min > max ) {return( -1 );}
	if( min == max ) {return( min );}
	return min + parseInt( Math.random() * ( max-min+1 ));
};

//image-gallery at premium-entries
function pi2_slideGallery(direction,id){
	var entry = parseInt(id.split(':')[0]);
	var current = parseInt(id.split(':')[1]);
	var count = parseInt(jQuery('#azAdEntry_' + entry + ' .img_wrapper img').length);
	var next_img_to_show;
	var info;
	if (direction=='prev') {
		next_img_to_show = (current-1);
		info = current;
		if (next_img_to_show < 0) {
			next_img_to_show = (count-1);
			info = count;
		}
	}
	if (direction=='next') {
		next_img_to_show = (parseInt(current)+1);
		info = (parseInt(current)+2);
		if (next_img_to_show >= count) {
			next_img_to_show = 0;
			info = 1;
		}
	}

	jQuery('.tx-smsfoehraz-pi2 img[id=image_' + entry + ':' + current + ']').hide();
	jQuery('.tx-smsfoehraz-pi2 img[id=image_' + entry + ':' + next_img_to_show + ']').show();
	jQuery('.tx-smsfoehraz-pi2 [id=curr_' + id + ']').attr('id','curr_' + entry + ':' + next_img_to_show);
	jQuery('.tx-smsfoehraz-pi2 [id=azAdEntry_' + entry + '] .switch_info').text('Bild ' + info + ' von ' + count);
};
