function genHex(){
	colors = new Array(14)
	colors[0]="0"
	colors[1]="1"
	colors[2]="2"
	colors[3]="3"
	colors[4]="4"
	colors[5]="5"
	colors[5]="6"
	colors[6]="7"
	colors[7]="8"
	colors[8]="9"
	colors[9]="a"
	colors[10]="b"
	colors[11]="c"
	colors[12]="d"
	colors[13]="e"
	colors[14]="f"
	
	digit = new Array(5)
	color="ffff"
	for (i=0;i<2;i++){
		digit[i]=colors[Math.round(Math.random()*14)]
		color = color+digit[i]
	}
	
	return color;
}




$.fn.pulse = function(end, intime, outtime){
	
	
	$(this).animate({opacity: end}, outtime, function(){
		$(this).animate({opacity: 1}, intime, function(){
			$(this).pulse(end, intime, outtime)
		})
	})
}

jQuery.fn.vibrate = function (conf) {
    var config = jQuery.extend({
        speed:        20, 
        duration:    10000, 
        frequency:    1000, 
        spread:        4
    }, conf);

    return this.each(function () {
        var t = jQuery(this);

        var vibrate = function () {
            var topPos    = Math.floor(Math.random() * config.spread) - ((config.spread - 1) / 2);
            var leftPos    = Math.floor(Math.random() * config.spread) - ((config.spread - 1) / 2);
            var rotate    = Math.floor(Math.random() * config.spread) - ((config.spread - 1) / 2);
			var color = '#'+genHex()
			
            t.css({
                position:            'relative', 
                left:                leftPos + 'px', 
                top:                topPos + 'px', 
                color:                color, 
                WebkitTransform:    'rotate(' + rotate + 'deg)'  // cheers to erik@birdy.nu for the rotation-idea
            });
        };

        var doVibration = function () {
            var vibrationInterval = setInterval(vibrate, config.speed);

            var stopVibration = function () {
                clearInterval(vibrationInterval);
                t.css({
                    position:            'static', 
                    WebkitTransform:    'rotate(0deg)'
                });
            };

            setTimeout(stopVibration, config.duration);
        };

        doVibration();
    });
};


$.fn.clearForm = function() {
  // iterate each matching form
  return this.each(function() {
    // iterate the elements within the form
    $(':input', this).each(function() {
      var type = this.type, tag = this.tagName.toLowerCase();
      if (type == 'text' || type == 'password' || tag == 'textarea')
        this.value = '';
      else if (type == 'checkbox' || type == 'radio')
        this.checked = false;
      else if (tag == 'select')
        this.selectedIndex = -1;
    });
  });
};

function initGlide(){
	featuredcontentglider.init({
		gliderid: "featured-content-glider", //ID of main glider container
		contentclass: "glidecontent", //Shared CSS class name of each glider content
		togglerid: "p-select", //ID of toggler container
		remotecontent: "", //Get gliding contents from external file on server? "filename" or "" to disable
		selected: 0, //Default selected content index (0=1st)
		persiststate: false, //Remember last content shown within browser session (true/false)?
		speed: 1000, //Glide animation duration (in milliseconds)
		direction: "leftright", //set direction of glide: "updown", "downup", "leftright", or "rightleft"
		autorotate: true, //Auto rotate contents (true/false)?
		autorotateconfig: [8000, 0] //if auto rotate enabled, set [milliseconds_btw_rotations, cycles_before_stopping]
		
	})
	
}


function slideSwitch() {
    var $active = $('#arspoetica-glider div.active');

    if ( $active.length == 0 ) $active = $('#arspoetica-glider div:last');

    var $next =  $active.next().length ? $active.next()
        : $('#arspoetica-glider div:first');


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

    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active last-active');
        });
}



function initReferences(){

	if($('body#referenciaink').length > 0){

		$('div.item h3').hover(function(){
			$(this).addClass('over')
		}, function(){
			$(this).removeClass('over')
		
		})
		
		
		$('div.item').each(function(){

			$('.content', $(this)).hide()

			$('h3', this).click(function(){
				
	
				if(!$('div.content', $(this).parent()).parent().hasClass('opened')){
					$('.content').slideUp().parent().removeClass('opened')
					
					$('div.content', $(this).parent()).slideDown().parent().addClass('opened')				

				} else{
					
					$('div.content', $(this).parent()).slideUp().parent().removeClass('opened')				
					
				
				}
				
				

			})

		})

		if(self.document.location.hash.substring(1)){
			$('div.content', $('#'+self.document.location.hash.substring(1))).slideDown().parent().addClass('opened')
		}else if($('body#referenciaink div#content div.item').length == 1){
			//$('body#referenciaink div.content:first').slideDown().parent().addClass('opened')
		}
	}
}



function initProductImages(){
	$('div.thumbs a').each(function(){
		
		$(this).css({opacity: 0.4})
		
		$(this).hover(function(){
			if(!$(this).hasClass('selected')){
				$(this).animate({opacity: 0.7},50)
			}
		},function(){
			if(!$(this).hasClass('selected')){
				$(this).animate({opacity: 0.4},100)
			}
		})
		
		
		$(this).click(function(){

			var large = $(this).attr('href')
			var extra = $(this).attr('rel')
			var holder = $('.image-holder', $(this).parent().parent().parent())
			var title = $(this).attr('title')
			holder.addClass('loading')
			
			var image = $('<a href="'+extra+'" rel="group" title="'+title+'" class=""><img src="'+large+'"/></a>')
			
			image.fancybox();

			$('div.thumbs a', $(this).parent().parent()).css({opacity: 0.4}).removeClass('selected')
			$(this).css({opacity: 1}).addClass('selected')

			holder.empty().append(image)
			$('img', holder).hide().fadeIn(300)

/*			image.css({"opacity": 1})
			image.animate({opacity: 1}, 100, function(){
				holder.removeClass('loading')
			})
			
*/			return false;
		})
	})
	
	$('div.thumbs').each(function(){
		$('a:first', this).trigger('click')	
	})
	
}

$(document).ready(function(){

	initGlide()
	initReferences()
	
	
	
//	initProductImages()

    setInterval( "slideSwitch()", 9000 );

	$('#hirek .thumbs a').fancybox()
	$('a.fancybox').fancybox()
	
	$('a.group-fancybox').fancybox({
			'hideOnContentClick': false
	})

	
});













function msg(t){
	//$('#message').stop()
	$('#message').empty().text(t).show().css({opacity: 1}).fadeOut(1000)
}

function trace() {
	var debug = document.getElementById('debug');
	if (!debug) {
		var debug = document.createElement('div');
		debug.setAttribute('id', 'debug');
		document.body.appendChild(debug);
		
		debug.onclick = function() {
			if (this.closed) {
				this.style.height = '200px';
				this.closed = false;
			} else {
				this.style.height = '2px';
				this.closed = true;
			}
		}
		
	}
	if (debug) {
		var o = '';
		o = 'arguments.length = ' + arguments.length + '<br />';
//		o += arguments;
		for (var i=0; i<arguments.length; i++) {
			o += '<div style="margin-left: 16px;">';
			o += arguments[i];
			for (j in arguments[i]) {
				o += '<div style="margin-left: 16px;">';
				o += '<b>' + j +'</b>&nbsp;=&nbsp;' + String(arguments[i][j]).split('<').join('&lt;').split('>').join('&gt;').split('\"').join('&raquo;');
				o += '</div>';
			}				
			o += '</div>';
		}
		debug.innerHTML = o;
	}
}
