var jQ = jQuery.noConflict();

jQ(document).ready(function(){
	
	jQ('#content h1.csc-firstHeader').replaceStr();

	if(jQ('#menuL0 ul li').css('display') != 'none'){

		jQ('#menuL0 ul li').hover(function(){
			jQ(this).css('cursor','pointer');
			jQ('.glimmer',this).css("left","-349px");
			jQ('.glimmer',this).animate({ 
        			left: "130px"
      			}, 1500 );
		});
	}
	
	jQ('#menuL0 ul li a').updateAndCreateMenu();
	jQ('#menuL1 ul li a').updateAndCreateMenu();
	
	/* LIGHTBOX/SLIDESHOW */	
	//first remove unwanted extra css 
	jQ( "#content dl.csc-textpic-image, #content .csc-textpic-imagewrap" ).removeAttr("style");
	jQ( "#content dl.csc-textpic-image, #content .csc-textpic-imagewrap").addClass('imgContainer');
	
	jQ( "#content .imgContainer img" ).each(function(i){
		
		var hght = jQ(this).attr("height");
		var wdth = jQ(this).attr("width");
		
		if ( wdth > hght ){
			
			var newWdth = (wdth/hght)*219;
			newWdth = newWdth.toFixed(0);
						
			jQ(this).attr({
				"width":newWdth,
				"height":219
			});
			
		} else if ( wdth < hght ){
			var newWdth = wdth / (hght/219);
			newWdth = newWdth.toFixed(0);
						
			jQ(this).attr({
				"width":newWdth,
				"height":"219"
			});
		} else {
			jQ(this).attr({
				"width":"350",
				"height":"219"
			});
		}
		
		jQ("#content .imgContainer").append( jQ(this) );
	});
	//clean up old markup
	jQ("#content .imgContainer div").remove();
	//wrap images for light box
	jQ("#content .imgContainer img").wrap('<div class="item"><a href="" rel="prettyPhoto[gallery]"></a></div>');
	//link to large image for light box
	jQ("#content .imgContainer a").each(function(i){
		var imgsrc = jQ( this ).children("img").attr("src");
	
		var pos = imgsrc.lastIndexOf("/");
		var imagename = imgsrc.substr(pos + 1);
	
		var posDash = imagename.lastIndexOf("_");
	
		var lenDash = imagename.length - 7;
	
		if(posDash == lenDash){		
			imagename = imagename.substr(0,posDash) + ".jpg";
		} 
	
		jQ( this ).attr({"href":imgsrc});
	});
	jQ("#content .imgContainer").addClass("items");
	jQ("#content .csc-textpic").scrollable({
		// items are auto-scrolled in 2 sec interval 
        	interval: 4000, 
         
        	// when last item is encountered go back to first item 
        	loop: true,  
         
        	// make animation a little slower than the default 
        	speed: 400, 

        	size: 1
        });
        
        jQ(".items a[rel^='prettyPhoto']").prettyPhoto({
		animationSpeed: 'fast', 
		padding: 30,
		opacity: 0.8, 
		showTitle: true, 
		allowresize: false, 
		counter_separator_label: '/', /* The separator for the gallery counter 1 "of" 2 */
		theme: 'dark_rounded' 
	});
});

jQ.fn.cycleImg = function(reset){
	this.each(function(){ 
		var c = this;
		if(reset){
			jQ('img',c).eq(0).hide();
			jQ('img',c).eq(1).show();
		}else{
			jQ('img',c).eq(0).show();
			jQ('img',c).eq(1).hide();
		}
	});
	return this;	
}

jQ.fn.replaceStr = function(){
									
	this.each(function(){ 
		var c = this;
		
		var str = jQ(this,c).text();		
		jQ(this,c).html(str.replace(/;/g,"<br/>"));
	});
	return this;	
}

jQ.fn.updateAndCreateMenu = function(){
	var iter = 0;
	var id = jQ('#menuL0 .active').attr("id");
	
	//append block to 9 else
	if ( jQ(this).parent().parent().parent().attr('id') == 'menuL1') {
		if ( jQ(this).length < 8){
			var totalA = jQ(this).length;
			var i= totalA;
		
			for (i=totalA;i < 8;i++){
				jQ('#menuL1 li:last').after('<li class="G_'+id+'" >&nbsp;</li>');
			}
		} 
		jQ('#menuL1 li').eq(3).after('<li id="'+id+'-spacer" class="G_'+id+'" ></li>');
	} 
	
	this.each(function(){
		var c = this;
		jQ(this,c).replaceStr();
		
		var href = jQ(this,c).attr("href");
		jQ(this,c).parent().find('div').click(function (){
			document.location.href= 'http://www.verver-standbouw.nl/'+href;
		});
		
		if ( jQ(this,c).parent().parent().parent().attr('id') == 'menuL1') {
			jQ(this,c).parent().addClass('G_'+id);
		}
	});
}
