$(document).ready(function () {

    /*this function sets height to background image wrapper so that unnecessary background image part is hidden.*/
	$("#background_img_wrap").height($("#wrapper").height());

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

    /*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 .entry-content").hide();

    $(".homeContentBox .toggleBox").click(function () {
        $(this).parents(".homeContentBox").toggleClass("closed");
        if ($(this).parents(".homeContentBox").hasClass("closed")) {
            $(this).parents(".homeContentBox").find(".entry-content").hide();
				//$(this).parents(".homeContentBox").css("height", "");
        } else {
            $(this).parents(".homeContentBox").find(".entry-content").slideDown('fast', function(){
					/*if (parseInt($(this).parents(".box_wrap").height())>parseInt($(this).parents(".homeContentBox").height()))
					{
					$(".homeContentBox").css("height", parseInt($(this).parents(".box_wrap").height()));
					}*/																												 
				});
        }
        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 () {
        var myid = $(this).attr("id");
		  if ($(this).find(".scroll-pane").length > 0) {
            clearTimeout($(this).data('timeout'));
            if (sifr_enabled)
			{
				sIFR.rollback("#primary-nav #"+myid+" > span");
            sIFR.rollback("#primary-nav #"+myid+" > p");
            primaryNavSifrAOver(myid);
            primaryNavSifrPOver(myid);
			}
            $(this).addClass("active");
            $(this).find(".scroll-pane").show();
        }
    }, function () {
        var myid = $(this).attr("id");
		  if ($(this).find(".scroll-pane").length > 0) {
            var myobj = $(this);
            var t = setTimeout(function () {
                if (sifr_enabled)
				{
					 sIFR.rollback("#primary-nav #"+myid+" > span");
                sIFR.rollback("#primary-nav #"+myid+" > p");
                primaryNavSifrAOn(myid);
                primaryNavSifrPOn(myid);
				}
				$(myobj).removeClass("active");
                $(myobj).find(".scroll-pane").hide();
            }, 600);
            $(this).data('timeout', t);
        }
    });

    /*functions which extends search bar right part to the end of the window if resolution is higher than 998px*/
	searchBoxRight();
    function searchBoxRight() {
        var a = ($(window).width() - $("#wrapper").width()) / 2;
        if (a > 0) {
            $(".search-box-right").css({
                width: a,
                "display": "block"
            });
        }
    }

    
	$("input[type=text]").focus(function () {

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

    /* function to change background image of template*/
	$("#bg_changer a").click(function () {
        var tg = $(this).attr("href");
        $("#background-img").hide();
        $("#background-img").attr("src", tg);
        $("#background-img").fadeIn("slow");
        slideBgChanger();
        return false;
    });

    $("#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[href*='http://www.vimeo.com/']").each(function() {
			$(this).addClass("fancyVimeo").addClass("playicon").removeClass('lightbox');
		});
		$("a.fancybox[href*='http://www.youtube.com/']").each(function() {
			$(this).addClass("fancyVideo").addClass("playicon").removeClass('lightbox');
		});
		$("a.lightbox[href$='.swf']",parent).each(function() {
			$(this).addClass("playicon");
		});
	
    $("a.fancybox").fancybox({
        'transitionIn': 'none',
        'transitionOut': 'none'
    });
	 
	 $("a.fancyVideo").each(function() {
		$(this).fancybox({
				'padding'		: 0,
				'autoScale'		: false,
				'transitionIn'	: 'none',
				'transitionOut'	: 'none',
				'title'			: this.title,
				'width'		: 680,
				'height'		: 495,
				'href'			: this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
				'type'			: 'swf',
				'swf'			: {
						 'wmode'		: 'transparent',
					'allowfullscreen'	: 'true'
				}
			});
	});
	$("a.fancyVimeo").each(function() {
		$(this).fancybox({
			'padding'		: 0,
			'autoScale'		: false,
			'transitionIn'	: 'none',
			'transitionOut' : 'none',
			'title'			: this.title,
			'width'			: 600,
			'height'			: 400,
			'href'			: this.href.replace(new RegExp("([0-9])","i"),'moogaloop.swf?clip_id=$1'),
			'type'			: 'swf'
		});
	});

	/*function to place labels over inputs in contact page*/
	$('#contact-form label').labelOver('over');
	$('#commentform label').labelOver('over');
	
	$('#commentform').submit(function(){				  
		
		var myform = $("#commentform").validate({
			errorPlacement: function (error, element) {
				error.appendTo();
			}
		}).form();
		if (myform)
		{
			$(this).submit();
		}
		return false;
	});
	
	
	$('#contact-form').submit(function(){				  
		
		var myform = $("#contact-form").validate({
			errorPlacement: function (error, element) {
				error.appendTo();
			}
		}).form();
		
		if (myform)
		{
			var action = $(this).attr('action');
			$.post(action, { 
				name: $('#name').val(),
				email: $('#email').val(),
				message: $('#message').val(),
				contactto : $('#contact_to').val()
			},
				function(data){
					$('#contact-form #submit').attr('disabled','');
					$('.response').remove();
                                        var datastr = "";

                                        if (data==1) {datastr = "Your message has been sent!";}
                                        if (data==2) {datastr = "There has been an error. Please try again.";}
                                        if (data==3) {datastr = "Error: not valid email.";}

                                        $('#contact-form').before('<p class="response">'+datastr+'</p>');
					$('.response').slideDown();
					if(data==1)
                                            {
                                                $('#contact-form').slideUp();
                                            }
				}
			);
		}
		return false;
	});
	
	
});

/*function which activates slider plugin in models sub menu*/

$(function () {
    //vars
    //create slider
    //$(".sliderBtn").slider(sliderOpts);
    $(".sliderBtn").each(function () {
        var conveyor = $(this).parent().parent().find(".scroll-content ul"),
            item = $(this).parent().parent().find(".scroll-content ul li");
        conveyor.css("width", item.length * 330);
        //330 is the width of the li item under models sub menu.
        //config
        var sliderOpts = {
            max: (item.length * 330) - parseInt($(this).parent().parent().find(".scroll-content").css("width")),
            slide: function (e, ui) {
                conveyor.css("left", "-" + ui.value + "px");
            }
        };
        $(this).slider(sliderOpts);
    });
});

/* 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" };
if (musicId != "")
    {
        swfobject.embedSWF("http://www.youtube.com/v/"+musicId+"?enablejsapi=1&playerapiid=ytplayer&autoplay=1","mysound", "1", "1", "8", null, null, params, atts);
    }
//
