
$.fn.clearForm = function() {
    return this.each(function() {

        var type = this.type, tag = this.tagName.toLowerCase();

        if (tag == 'form')

        return $(':input',this).clearForm();

        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 slidectrl(action) {
	if(action=='next'){
		$('.portfolio-slideshow').cycle('next');
	} else {
		$('.portfolio-slideshow').cycle('prev');
	}
}


$(function () {
  $('#footer .menu li').each(function () {
    // options
    var distance = 10;
    var time = 250;
    var hideDelay = 500;

    var hideDelayTimer = null;

    // tracker
    var beingShown = false;
    var shown = false;
    
    var trigger = $('a', this);
    var popup = $('a span', this);//.css('opacity', 0);

    // set the mouseover and mouseout on both element
    $([trigger.get(0), popup.get(0)]).mouseover(function () {
      // stops the hide event if we move from the trigger to the popup element
      if (hideDelayTimer) clearTimeout(hideDelayTimer);

      // don't trigger the animation again if we're being shown, or already visible
      if (beingShown || shown) {
        return;
      } else {
        beingShown = true;
		
		
		var center = -((popup.outerWidth()/2)-(trigger.outerWidth()/2));
		
        // reset position of popup box
        popup.css({
          top: -30,
          left: center,
          display: 'block' // brings the popup back in to view
        })

        // (we're using chaining on the popup) now animate it's opacity and position

        // extract: ,opacity: 0.8
        .animate({
          top: '-=' + distance + 'px'
        }, time, 'swing', function() {
          // once the animation is complete, set the tracker variables
          beingShown = false;
          shown = true;
        });
      }
    }).mouseout(function () {
      // reset the timer if we get fired again - avoids double animations
      if (hideDelayTimer) clearTimeout(hideDelayTimer);
      
      // store the timer so that it can be cleared in the mouseover if required
      // extract: ,opacity: 0
      hideDelayTimer = setTimeout(function () {
        hideDelayTimer = null;
        popup.animate({
          top: '-=' + distance + 'px'
        }, time, 'swing', function () {
          // once the animate is complete, set the tracker variables
          shown = false;
          // hide the popup entirely after the effect (opacity alone doesn't do the job)
          popup.css('display', 'none');
        });
      }, hideDelay);
    });
  });
});

$(document).ready(function(){
        var newWidth;
	/*Adjust height segments */
	$(window).load(function() {
		$(".section").each( function(){
			$(this).css({height: $(window).height()});
		});
                slideMenu1.build('sm178', 360, 10, 15,1);
                slideMenu2.build('sm180', 360, 10, 15,1);
	});

	/*portfolio*/
	$('.portfolio-slideshow').cycle({
		fx: 'scrollHorz',
		timeout: 0,
                cleartypeNoBg: true
	});
        $('#slideshow').append('<div id="ctrl-left"></div><div id="ctrl-right"></div>');

        $('#ctrl-left').click(function(){
            slidectrl('prev');
        })

        $('#ctrl-right').click(function(){
            slidectrl('next');
        })
       
        $('#ctrl-left').hover(function(){
		$(this).stop().animate({'opacity':'1.0'},100);
	}, function(){
		$(this).stop().animate({'opacity':'0.4'});
	});

	$('#ctrl-right').hover(function(){
		$(this).stop().animate({'opacity':'1.0'},100);
	}, function(){
		$(this).stop().animate({'opacity':'0.4'});
	});

	$(window).resize(function() {
		$(".section").each( function(){
			$(this).css({height: $(window).height()});
		});
	});

        $.featureList(
            $("#menu-tjanstemeny li a"),
            $(".subarticles ul li"), {
                    start_item : 0
            }
        );
        $.featureList(
            $("#facilities-menu li a"),
            $("#facilities-content .showcase-container li"), {
                    start_item : 0
            }
        );
            
        $("#tipsa").hide();

        $(".contact_form  a.tipsa").click(function(e){
            e.preventDefault();
            $("#kontakta").fadeOut(200);
            $("#tipsa").fadeIn(200)
        });

        $(".contact_form  a.kontakta").click(function(e){
            e.preventDefault();
            $("#kontakta").fadeIn(200);
            $("#tipsa").fadeOut(200)
        });

        $(".show").show();
        $(".hide").hide();
	jQuery("#newslist a").live("click", function(e){
		//Cancel the link behavior
		e.preventDefault();
      	});

        	jQuery(".showcase").each( function(){
			jQuery(this).awShowcase(
				{
					width:					600,
					height:					400,
					auto:					true,
					interval:				5000,
					continuous:				true,
					loading:				true,
					arrows:					true,
					buttons:				true,
					btn_numbers:                            false,
					keybord_keys:                           true,
					mousetrace:				false,
					pauseonover:                            true,
					transition:				'fade', /* vslide/hslide/fade */
					transition_speed:                       500,
					show_caption:                           'onhover', /* onload/onhover/show */
					thumbnails:				false,
					thumbnails_position:                    'outside-last', /* outside-last/outside-first/inside-last/inside-first */
					thumbnails_direction:                   'horizontal', /* vertical/horizontal */
					thumbnails_slidex:                      0 /* 0 = auto / 1 = slide one thumbnail / 2 = slide two thumbnails / etc. */
			})
		   }
	);
            
        $('#kontakt #skicka_kontakt').click(function () {
				var data = $('#kontakt').serialize();
                                var sendUrl = $('#kontakt').attr('action');
				//start the ajax
				$.ajax({
					//this is the php file that processes the data and send mail
					url: sendUrl,

					//POST method is used
					type: "POST",

					//pass the data
					data: data,

					//Do not cache the page
					cache: false,

					//success
					success: function (html) {
						//if process.php returned 1/true (send mail success)
						if (html=='1') {
							$('#kontakta h3').html('Tack för meddelandet. Vi återkommer så snart vi kan!');
                                                        $('#kontakt').clearForm();
						} else {
							$('#kontakta h3').html('Meddelandet kunde inte sändas. Kontakta oss på info@modellab.se');
                                                        alert('Kontrollera att du har fyllt i en korrekt e-postadress.');
						}
                                            }
				});
				return false;
			});

         $('#tipsa #skicka_tips').click(function () {
                var data = $('#tips').serialize();
                var sendUrl = $('#tips').attr('action');
                //start the ajax
                $.ajax({
                        //this is the php file that processes the data and send mail
                        url: sendUrl,

                        //POST method is used
                        type: "POST",

                        //pass the data
                        data: data,

                        //Do not cache the page
                        cache: false,

                        //success
                        success: function (html) {
                                //if process.php returned 1/true (send mail success)
                                if (html=='1') {
                                        $('#tipsa h3').html('Tack för tipset! Skicka gärna fler tips.');
                                        $('#tips').clearForm();
                                } else {
                                        $('#tipsa h3').html('Tipset kunde inte sändas. Kontakta oss på info@modellab.se');
                                }
                            }
                });
                return false;
	});

        /* SERVICES MENU */

        $("div.submenu").hide();

	$("div#services-menu div#menu-toggle").click(function() { //When trigger is clicked...

		//Following events are applied to the subnav itself (moving subnav up and down)
		$(this).parent().find("div.submenu").slideDown('fast').show(); //Drop down the subnav on click

		$(this).parent().hover(function() {
		}, function(){
			$(this).parent().find("div.submenu").slideUp('fast'); //When the mouse hovers out of the subnav, move it back up
		});

		//Following events are applied to the trigger (Hover events for the trigger)
		}).hover(function() {
			$(this).addClass("subhover"); //On hover over, add class "subhover"
		}, function(){	//On Hover Out
			$(this).removeClass("subhover"); //On hover out, remove class "subhover"

	});
        
});

