//------------------------- jQuery INIT --------------------------------------------
//jQuery.noConflict();
jQuery(document).ready(function() {

//--------------------- Ajax --------------------
jQuery.ajaxSetup ({
// Disable caching of AJAX responses
cache: false
});
//------------- Home page slider ---------------------
if(jQuery('#slider').length){
	$('#slider').cycle({
		pager:  '#slider-pager',
		pagerAnchorBuilder: function(idx, slide) { 
			return '<p></p>'; 
		},
		timeout: 7000,
		prev: '#slider-prev',
		next: '#slider-next',
		fx: 'fade',
		before: function(currSlideElement, nextSlideElement, options, forwardFlag){
			jQuery('#slide-caption').html(jQuery(nextSlideElement).find('.slide-caption').html());
		}
	});
}

//------------- Lang labels for form ------------
function getLanglabels() {
	jQuery(document).ready(function(){
		jQuery.ajax({
			type: "GET",
			url: "/fileadmin/templates/formhandler/contact/locallang.xml",
			dataType: "xml",
			success: function(xml) {
				var i = 0;
				jQuery(xml).find('label').each(function(){
					name = jQuery(this).attr('index');
					value = jQuery(this).text();
					jQuery("div#langLabelsJS").append('<div class="'+name+'">'+value+'</div>');
					i++;
				});				
			}
		});
	});
}




//------------- Dynamic contact form ------------
function initContactForm() {
	getLanglabels();
	jQuery("#contactForm").validate({
		rules: {
			title: { required: true, minlength: 2 },
			fname: { required: true, minlength: 2 },
			lname: { required: true, minlength: 2},
			inquiry: { required: true, minlength: 2},
			salutation: { required: true},
			tel: { required: true, minlength: 2},
			email: {
				required: true,
				email: true
			}
		},
		onkeyup: true,
		messages: {
			title:"This field is required",
			inquiry:"This field is required",
			salutation:"This field is required",
			tel:"This field is required",			
			fname: {
				required: "Please enter a firstname",
				minlength: "Your firstname must consist of at least 2 characters"
			},
			lname: {
				required: "Please enter a lastname",
				minlength: "Your lastname must consist of at least 2 characters"
			},
			email: "Please enter a valid email address"
		},
		// the errorPlacement has to take the table layout into account
		errorPlacement: function(error, element) {
			jQuery(element).parent().append(error);
		}
	});
}

//---- Categories animation ----	
function initCategoryAnimation() {
	jQuery("div.category").each(function(){
		this.isAnim = false;
		this.iwidth = 229;
		this.iheight = 130;
		this.shtime = 300; // Show hide time in milliseconds
	});
    jQuery("div.category").bind("mouseenter",function(){
		if(!this.isAnim) {
			this.isAnim = true;
			var tmpLink = this;
			//jQuery(this).find("div.cat_img").animate({ width:this.iwidth+"px", height:"0px"}, this.shtime, function() {  // variant 2
			jQuery(this).find("div.cat_img").animate({ width:this.iwidth+"px", marginTop:"-"+this.iheight+"px"}, this.shtime, function() {
				tmpLink.isAnim = false;
			});
		}
    }).bind("mouseleave",function(){
		//jQuery(this).find("div.cat_img").animate({ width:this.iwidth+"px",  height:this.iheight+"px"}, this.shtime); // variant 2
		jQuery(this).find("div.cat_img").animate({ width:this.iwidth+"px",  marginTop:"0px"}, this.shtime);
    });
}

//---- Dynamic right accordean ---------
function initRightAccordean() {
	// Animate 1 level
	jQuery("div.right_menu>ul>li").each(function(){
		jQuery(this).click(function(){
			showAccordean(this);
		});
	});
	// Load page from link in 2 level
	jQuery("div.right_menu li li").each(function(){
		jQuery(this).click(function(){
			jQuery("div.filter a, div.right_menu li li").removeClass("active");
			jQuery(this).addClass("active");
			var linkHref = jQuery(this).find("a").first().attr("href");
			loadBlock(".dynamic_inner", ".dynamic", linkHref);
			//scrollTo(".dynamic"); // If want scroll
			return false;
		});
	});
	// Filter click
	jQuery("div.filter a").click(function(){
		showAccordean(null);
		var linkHref = jQuery(this).attr("href");
		loadBlock(".dynamic_inner", ".dynamic", linkHref);
		//scrollTo(".dynamic"); // If want scroll
		jQuery(this).addClass("active");
		return false;
	});
}
function showAccordean(showObj) {
	shtime = 300;
	jQuery("div.right_menu>ul>li").each(function(){
		if(showObj==this) {
			jQuery(this).find("ul").slideDown(shtime);
			jQuery(this).addClass("active");
		} else {
			jQuery(this).find("ul").slideUp(shtime);
			jQuery(this).removeClass("active");
		}
	});
}

//--------------------- Custom checkbox --------------------
function initCustomCheckBox() {
	jQuery("a.checkbox").click(function(){
		tmpVal = (jQuery(this).find("input").attr("value") == 0) ? 1 : 0 ;
		jQuery(this).find("input").attr("value",tmpVal);
		if(tmpVal == 1) {
			jQuery(this).addClass("checked_1");
			//jQuery("#tel_req").attr("class", '');
			jQuery("#tel_req").html('*');
		}	
		else {
			jQuery(this).removeClass("checked_1");
			//jQuery("#tel_req").attr("class", 'hidden');
			jQuery("#tel_req").html('');
		}	
	});
}

//------------------- Gmaps -----------------------

function GmapsInitialize() {
	if(typeof(google) != "undefined") { // Check if api conected
		var myLatlng = new google.maps.LatLng(48.1457,11.5713); // http://www.getlatlon.com/
		var myOptions = {
		  zoom: 16,
		  center: myLatlng,
		  mapTypeId: google.maps.MapTypeId.ROADMAP
		}
		var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

		var marker = new google.maps.Marker({
			position: myLatlng, 
			map: map,
			title:"FIGGE+SCHUSTER AG"
		});  
	}
 
}

//----------------- Dynamic custom right accordean ---------
function initCustomRightAccordean() {
	// Animate 1 level
	jQuery("div.right_cat a.more").each(function(index){
		jQuery(this).click(function(){
			showCustomAccordean(index);
			return false;
		});
	});
    
    jQuery("div.right_cat .mini_work a.more").each(function(index){
		jQuery(this).click(function(){
            document.location = this.href; 
			return true;
		});
	});
}
function showCustomAccordean(showIndex) {
	shtime = 300;
	jQuery("div.right_cat").each(function(index){
		var parentLink = this;
		if(index==showIndex) {
			jQuery(this).find("div.inner_cat").slideDown(shtime);
			jQuery(this).addClass("showed");
		} else {
			jQuery(this).removeClass("showed");
			jQuery(this).find("div.inner_cat").slideUp(shtime, function() {
				if(jQuery(parentLink).hasClass("active")) jQuery(parentLink).removeClass("active");
			});
			
		}
	});
}
//----------------- end Dynamic custom right accordean ---------


//--------------------- INIT --------------------
	
jQuery.reject(); // IE6 update window
initCategoryAnimation();
initCustomRightAccordean();
initRightAccordean();
initCustomCheckBox();
GmapsInitialize(); // Gmaps


//---- End Jquery init ----
}  );
//---- End Jquery init ----



//------------- Page slider ---------------------
function initPageSlider(id) {
	if(id > 0){	
		if( typeof(SlideShowPro) !== 'undefined') { // Check if library connected
			try {
				if(jQuery.browser.msie && jQuery.browser.version.substr(0,1)<7) return;
				SlideShowPro({
				attributes: {
					id: "page_slider",
					width: 497,
					height: 290
				},
				mobile: {
					auto: false,
					poster: "headline",
					flash: false,
					showBackButton: true
				},
				params: {
					bgcolor: "http://217.12.213.109/view.php?id=0",
					allowfullscreen: true
				},
				flashvars: {
					xmlFilePath: "http://slideshow.figge-schuster.de/images.php?album="+id,
					paramXMLPath: "http://slideshow.figge-schuster.de/m/params/glass.xml",
					panZoom: "On",
					navAppearance: "Visible on Rollover",
					transitionStyle: "Dissolve",
					feedbackPreloaderAppearance: "Bar",
					galleryAppearance: "Hidden",
					contentScale: "Crop to Fit All"
				}
				});
			} catch(err) {
			
			}
		}
	}	
}

function loadBlock(fromBlockClass, toBlockClass, urlString) {
	try {
	jQuery(toBlockClass).addClass("loader");
	jQuery(toBlockClass).load(urlString+' '+fromBlockClass,function(){
		jQuery(toBlockClass).removeClass("loader");
		jQuery(toBlockClass).css("height","auto");
	});
   } catch(e) {
   
   }
   return false;
}
//-------------------- ScrollTo func ------------
function scrollTo(toBlockClass) {
	this.scrollTime = 500;
	var destination = jQuery(toBlockClass).offset().top;
	jQuery("html:not(:animated)").animate({ scrollTop: destination-20}, this.scrollTime );
	if(!jQuery.browser.opera) jQuery("body:not(:animated)").animate({ scrollTop: destination-20}, this.scrollTime );
}


//-------------------------------------------------------- For LightBox --------------------------------------
function initLightBox() {
	// Check if library added
	if(typeof(hs) !== 'undefined') {
		hs.graphicsDir = 'fileadmin/templates/main/js/highslide/graphics/';
		hs.align = 'center';
		hs.transitions = ['expand', 'crossfade'];
		hs.fadeInOut = true;
		hs.outlineType = 'drop-shadow';
		hs.headingEval = 'this.a.title';
		//hs.numberPosition = 'heading';
		//hs.useBox = true;
		hs.width = 600;
		hs.height = 400;
		hs.showCredits = false;
		hs.dimmingOpacity = 0.8;
		hs.showCredits = false;
	}
}
initLightBox();

//------------------------------------------ eliminate IE6 background image flicker -----------------------------------------------------
try {
  document.execCommand("BackgroundImageCache", false, true);
} catch(ex) {}

function obscureAddMid() {
  document.write('@');
}

function obscureAddEnd() {
  document.write('.');
}

