﻿// script for images preload 
function preload(){
    if (document.images){
        var imgsrc = preload.arguments; arr=new Array(imgsrc.length);
        for(var j=0; j<imgsrc.length; j++){arr[j]=new Image;arr[j].src=imgsrc[j];}
    }
}

// show/hide the textarea title 
function hidetx(myname){$('#'+myname).fadeOut("slow");}
function showtx(myname){$('#'+myname).fadeTo("slow", 0.65);}

//** show/hide any page element 
function hideme(myname){
	$('#'+myname).animate({marginTop: '-170px', opacity: 0}, 400).hide(400);}
function showme(myname){
	$('#'+myname).show().animate({marginTop: '150px', opacity: 0.7}, 400);}



/***** jQuery scripts *****/ 
$(document).ready(function(){
    
	fancyImg(); //** Fancybox for Portfolio images
	portdes(); //** call the portfolio thumb's-description

	//** AJAX general settings
    $.ajaxSetup({
        cache: false,
        error: function(){
            $("#mynote").html("<p>Sorry, I cannot complete the action at the moment. Connection error.</p><p>Извините, не могу завершить действие - ошибка соединения.</p><div id='ok' onClick=\"hideme('bignote');\">ok</div>");
            $.fancybox.hideActivity();
            showme("bignote");
        }
    });
    
    //** launch the provs and testims cycles
	$('.provs').cycle({delay: 0, timeout: 8000, speed: 2000});
	$('.testims').cycle({delay: 0, timeout: 10000, speed: 2000});

    //** slow image opacity (home-page link to Portfolio)
    $(".prt-sample img").fadeTo("slow", 0.5); 
    $(".prt-sample img").hover(function(){
        $(this).fadeTo("slow", 1.0); 
    },function(){
        $(this).fadeTo("slow", 0.5); 
    });

});
/***** end of jQuery scripts *****/


//** show the portfolio thumb's-description
function portdes(){
    $(".port-thumb").mouseenter(function(){
        var desc = $(this).find(".description");
        desc.hover(function(){
            $(this).animate({marginTop:"-200px", marginLeft:"-115px", height:"205px"}, 600);
        }, function(){
            $(this).animate({marginTop:"-200px", marginLeft:"180px", height:"35px"}, 400);
        });
    })
}


/************ ---------- common part --------- ***************/

//**** send form thru AJAX
function sendMe(myform){
	mydata = $('#'+myform).serializeArray();
    $('#'+myform).ajaxSubmit({
        data: mydata,
        type: "post",
        beforeSubmit: function() {
            $("#mynote").html("<div id='loading'></div>");
            showme("bignote");
        },
        success: function(a){ // result
            $("#mynote").html(a + "<div id='ok' onClick=\"hideme('bignote');\">ok</div>");
        }
    });
    return false;
}
// clean up the form
function cleanMyForm(myform){
    var allels = document.forms[myform].elements; 
    for(var i=0; i < allels.length; i++){allels[i].value = "";}
}


//**** Fancybox
// for subpages
function fancyPage(){
	$(".d-fancy, .subpage").fancybox({
		'speedIn' : 400, 'speedOut' : 200,
		'padding' : 0, 'margin' : 10,
		'autoScale' : false,
		'overlayOpacity' : 0.5,
		'scrolling' : 'no',
		'titleShow' : false,
		'showNavArrows' : false,
		'hideOnOverlayClick' : false,
	});
}
// for images
function fancyImg(){
	function formatTitle(title){
		return '<div id="p-title" style="padding:10px;text-align:center;">' + (title && title.length ? title : '') + '</div>';
    }
    $("a.group").fancybox({
        'transitionIn' : 'elastic', 'transitionOut' : 'elastic', 'speedIn' : 600, 'speedOut' : 400,
        'changeSpeed' : 400, 'changeFade' : 400, 'padding' : 7, 'margin' : 10,
        'autoScale' : false, 'overlayOpacity' : 0.5, 'titlePosition' : 'over', 'titleFormat' : formatTitle,
        'onComplete' : function(){
            $("#fancybox-title").delay(2000).slideUp('fast');
            $("#fancybox-wrap").hover(function(){
                $("#fancybox-title").slideDown('fast');
            }, function(){
                $("#fancybox-title").slideUp('fast');
            });
        },
    });
}

//**** On-page functions
// page scroll
function scrollto(container){
	destination = $("#" + container).offset().top;
	$("html:not(:animated)"+( ! $.browser.opera ? ",body:not(:animated)" : ""))
        .animate({scrollTop: destination}, 800);
    return false;
}

// reload the page part with fading
function reload(me, myurl, fast){
    $.ajax({
        url: myurl,
        beforeSend: function(){
            $.fancybox.showActivity();
        },
        success: function(a){
            if (fast === undefined){ // reload with effects
                $.browser.msie ? $('#'+me).hide('slow') : $('#'+me).fadeOut('slow');
                window.setTimeout(function (){$("#"+me).html(a);} , 600);
                $.browser.msie ? $('#'+me).show('slow') : $('#'+me).fadeIn('slow');
            } else $("#"+me).html(a); // or do it faster
            $.fancybox.hideActivity();
        }
    });
    return false;
}

// go to next page
function nextpage(container, current, next){
	scrollto(container); // scroll up
    reload(current, next); // replace content
}

// return invisible content
function hiddenReq(myurl){
    $.ajax({
        url: myurl,
        beforeSend: function(){
            $.fancybox.showActivity();
        },
        success: function(a){
            $("#hiddenRespond").html(a);
            $.fancybox.hideActivity();
        },
    });
    return false;
}


