$(document).ready(function () {
	$('.clickFix li').click(function(e){
		window.location.href = ($(e.currentTarget).attr('url'));
		return false;
		//alert(e.currentTarget.url);
	});
    /*function which hides background changer container to the left upon images loaded. */
	$(window).load(function () {
        $("#bg_changer").css("left", -10 - $("#bg_changer").width());
    });

    /*function gives style to select element in contact page*/
	$("#select1").sSelect();

    /*function gives style to scrollbars in about us page */
	$('.scrollPane').jScrollPane();

    /*activates tooltip plugin on elements with class .tooltip */
	$('.tooltip').tooltip({
        showURL: false
    });

	/*these functions hide all paragraphs inside boxes and leaves only first of them open. and hide some part of the image.*/
	/*
	$(".homeContentBox").addClass("closed");
    $(".homeContentBox .grey_box p").hide();
    $(".homeContentBox .grey_box").each(function () {
        $(this).find('p:first').show();
    });*/
	/*function which slides up the content of home boxes and hides them. */
	/*
    $(".homeContentBox .toggleBox").click(function () {
        $(this).parents(".homeContentBox").toggleClass("closed");
        if ($(this).parents(".homeContentBox").hasClass("closed")) {
            $(this).parents(".homeContentBox").find('p').hide();
            $(this).parents(".homeContentBox").find('p:first').show();
        } else {
            $(this).parents(".homeContentBox").find('p').slideDown();
        }
        return false; 
    });*/
	
	/*function which hides and shows the models sub menu */
	/*sifr rollback is used to rollback flash and apply sifr again */
    $('#primary-nav li').hover(function () {
        if ($(this).find(".scroll-pane").length > 0) {
            clearTimeout($(this).data('timeout'));
            sIFR.rollback("#primary-nav ul li#li-models > span");
            sIFR.rollback("#primary-nav ul li#li-models > p");
            $(this).addClass("active");
            $(this).find(".scroll-pane").show();
        }
    }, function () {
        if ($(this).find(".scroll-pane").length > 0) {
            var myobj = $(this);
            var t = setTimeout(function () {
                sIFR.rollback("#primary-nav ul li#li-models > span");
                sIFR.rollback("#primary-nav ul li#li-models > p");
                $(myobj).removeClass("active");
                $(myobj).find(".scroll-pane").hide();
            }, 600);
            $(this).data('timeout', t);
        }
    });
    
	$("input[type=text]").focus(function () {

        if (this.value == this.defaultValue) {
            this.value = "";
        }
    });
	
    $("input[type=text]").blur(function () {
        if (this.value == "") {
            this.value = this.defaultValue;
        }
    });

    $("#bg_changer-lnk").click(function () {
        $("#bg_changer").show();
        slideBgChanger();
        return false;
    });

    /*animation effect for background changer wrapper*/
	function slideBgChanger() {
        var $lefty = $("#bg_changer");
        $lefty.animate({
            left: parseInt($lefty.css('left'), 10) == 0 ? -10 - $lefty.outerWidth() : 0
        });
    }

    $(".showcase-list li>p").hide();

    /*functions for showcase page*/
	if ($('.grayscale').length > 0) {

        /* we use window load, because firstly images should be loaded. */
		$(window).load(function () {
            $(".grayscale").pixastic("desaturate");
        });
        /*function to invert images on hover and show paragraph and vica versa. */
		/*Pixastic revert and desaturate functions are used. */
		$('.showcase-list li').hover(function () {
            $(this).find('p').slideDown();
            var imgId;
            if ($.browser.msie) {
                imgId = document.getElementById($(this).find('img').attr('id'));
            } else {
                imgId = document.getElementById($(this).find('canvas').attr('id'));
            }
            Pixastic.revert(imgId);
        }, function () {
            $(this).find('p').stop(true, true).hide();
            $(this).find('img').pixastic("desaturate");
        })
    }
	
	
    $("a.fancybox").fancybox({
        'transitionIn': 'none',
        'transitionOut': 'none',
		'titleFormat':	function(title, currentArray, currentIndex, currentOpts) {
							return '<span id="fancybox-title-over">Image ' // <-- here
								+ (currentIndex + 1) + ' / ' + currentArray.length + ' ' + title
								+ '</span>';
						},
		'titlePosition': 'inside'
    });
	
	/*function to place labels over inputs in contact page*/
	$('#contact-form label').labelOver('over');	
	
    /*this function sets height to background image wrapper so that unnecessary background image part is hidden.*/
	$("#background_img_wrap").height($("#wrapper").outerHeight());

    /*function which updates background image wrapper height upon window resize*/
	$(window).resize(function () {
        $("#background_img_wrap").height($("#wrapper").height());
    });		
});

/*function which activates slider plugin*/
$(function () {		
	//buildScrollPane();

	// Add 'scrollVert' functionality for scroll boxe
	jQuery.fn.cycle.transitions.scrollVert = function(jcont, jslides, opts) {
		jcont.css('overflow','hidden');
		opts.before.push(function(curr, next, opts, fwd) {
			jQuery(this).show();
			var currH = curr.offsetHeight, nextH = next.offsetHeight;
			opts.cssBefore = fwd ? { top: -nextH } : { top: nextH };
			opts.animIn.top = 0;
			opts.animOut.top = fwd ? currH : -currH;
			jslides.not(curr).css(opts.cssBefore);
		});
		opts.cssFirst = { top: 0 };
		opts.cssAfter = { display: 'none' }
	};
	
	$('#scrollNews ul').cycle({ 
	//fx: 'scrollVert',
	speed: 650,
	rev: true,
	timeout: 10000,
	next:   '#scrollNews ol li.next', 
	prev:   '#scrollNews ol li.previous'
	});
});

/* plugin for labels to be placed over input fields in contact page */

jQuery.fn.labelOver = function (overClass) {
    return this.each(function () {
        var label = jQuery(this);
        var f = label.attr('for');
        if (f) {
            var input = jQuery('#' + f);
            this.hide = function () {
                label.css({
                    textIndent: -10000
                })
            }
            this.show = function () {
                if (input.val() == '') label.css({
                    textIndent: 0
                })
            }
            // handlers
            input.focus(this.hide);
            input.blur(this.show);
            label.addClass(overClass).click(function () {
                input.focus()
            });
            if (input.val() != '') this.hide();
        }
    })
}


/*function which pauses the music. Music is coming from a youtube video which is embedded to the bottom of page. function pauses youtube video too.*/
function soundOff(e) {
    $('.icon-sound').toggleClass("soundoff");
	ytplayer = document.getElementById("myytplayer");
	if ($('.icon-sound').hasClass("soundoff"))
	ytplayer.pauseVideo();
	else
	ytplayer.playVideo();
};

/*function for fullscreen effect*/
function fullScreen() {
    window.open($(location).attr('href'), "mywindow", "status=0,toolbar=0,location=0,menubar=0,resizable=1,width=" + screen.availWidth + ",height=" + screen.availHeight);
    window.close();
};

/*embedding youtube video by using swfobject*/
var params = { allowScriptAccess: "always" };
var atts = { id: "myytplayer" };
//swfobject.embedSWF("http://www.youtube.com/v/2qtg39ddNYE?enablejsapi=1&playerapiid=ytplayer&autoplay=1","mysound", "1", "1", "8", null, null, params, atts);

function resize(img, build)
{
	var maxh = 70;
	var maxw = 150;				
	if(img.height > maxh || img.width > maxw) //Si l'image doit être redimentionnée
	{
		var diffHeight = img.height - maxh;
		var diffWidth = img.width - maxw;
		
		if(diffHeight > diffWidth) //Redimentionné pour fiter en hauteur
		{
			var H = maxh;
			var W = Math.round((img.width/img.height)*H);
		}
		else //Redimentionner pour fiter en largeur
		{
			var W = maxw;
			var H = Math.round((img.height/img.width)*W);
		}
		img.style.width = W+"px";
		img.style.height = H+"px";
	}
	
	if(build)
	{
		buildScrollPane();
	}
}

function buildScrollPane()
{
	var conveyor = $(".scroll-pane");
	conveyor.each(function(index){
		itemLi = $("li", $(this));
		if(itemLi.length>0)
		{
			var largeurT = 0;
			$(itemLi).each(function(){
				if($(this).width()==0)
					largeur=330;
				else
					var largeur = $(this).outerWidth(true);
				largeurT += largeur;
			});
			//alert("= "+largeurT);
		}
		$('ul', $(this)).css("width", largeurT);
		max = ((parseInt($('ul', $(this)).css("width")))-(parseInt($('.scroll-content', $(this)).css("width"))));
		var sliderOpts = {
			max: ((parseInt($('ul', $(this)).css("width")))-(parseInt($('.scroll-content', $(this)).css("width")))),
			slide: function (e, ui) {
					$('ul', $(".scroll-pane")[index]).css("left", "-" + ui.value + "px");
					e.stopPropagation();
				}	
		};
		if(max>0)
		{
			$("#slider", $(this)).slider(sliderOpts);
		}
	});
}
