/* 
 * 
 * @author zniko07
 */

//init
var cur_img;
var total_img;

$(document).ready(function () { setupFirstImg();  });

/*
 * setup carousel
 */

stepcarousel.setup({
    galleryid: 'mygallery', //id of carousel DIV
    beltclass: 'belt', //class of inner 'belt' DIV containing all the panel DIVs
    panelclass: 'panel',
    autostep: {
        enable:true,
        moveby:1,
        pause:5000
    },
    panelbehavior: {
        speed:500,
        wraparound:true,
        persist:true
    },
    defaultbuttons: {
        enable: false
    },
    statusvars: ['statusA', 'statusB', 'statusC'], //register 3 variables that contain current panel (start), current panel (last), and total panels
    contenttype: ['inline'] //content setting ['inline'] or ['external', 'path_to_external_file']
})

/*
 * setup main img
 */

cur_elt = "a";
var hide_img_height = show_img_height = hide_img_width = show_img_width = place_x = place_y = 0;

setupFirstImg = function(){
    var img = new Image();
	total_img = urls_gd.length;
    img.src = site+"/images/"+urls_gd[0];
    img.onload = function(){
        $("#a").html("<img id='img_a' src='"+site+"/images/"+urls_gd[0]+"' alt='' />");
        show_img_height = $("#img_a").height();
        show_img_width = $("#img_a").width();
        place_x = eval($(window).width() / 2) - eval(show_img_width / 2 ) - 140;
        place_y = 290;//eval($(window).height() / 2) - eval(show_img_height / 2 ) - 40;
        $("#a").animate({
            opacity: 1,
            top: place_y,
            left: place_x
        },
        500,
        "linear");
    }
}

changePix = function (num){
    var url_to_show = site + "/images/" + urls_gd[num - 1];
	cur_img = num;
    cur_elt = cur_elt=="a" ? "b" : "a";
    tohide_elt = cur_elt=="a" ? "b" : "a";
    img = new Image();
    img.src =  site + "/images/" + urls_gd[num - 1];
	if(img.width==0){
    img.onload = function(){
        $("#"+cur_elt).html("<img id='img_"+cur_elt+"' src='"+ site + "/images/" + urls_gd[num - 1] + "' />");
        hideMe();
        showMe();
    }
	}else{
	        $("#"+cur_elt).html("<img id='img_"+cur_elt+"' src='"+ site + "/images/" + urls_gd[num - 1] + "' />");
        hideMe();
        showMe();
	}
}

hideMe = function(){
    $('#a').stop();
    $('#b').stop();
    $("#"+tohide_elt).animate({
        opacity: 0
    },
    100,
    "linear");
}
showMe = function(){
    show_img_height = $("#img_"+cur_elt).height();
    show_img_width = $("#img_"+cur_elt).width();
    place_x = eval($(window).width() / 2) - eval(show_img_width / 2 ) - 140;
    place_y = 290;//eval($(window).height() / 2) - eval(show_img_height / 2 ) - 40;

    $("#"+cur_elt).animate({
        opacity: 1,
        top: place_y,
        left: place_x
    },
    200,
    "linear");
}

function handle(delta) {
        if (delta < 0){
		if(document.body.scrollTop<200)
			window.scrollBy(0,50);
		else
			stepcarousel.stepBy("mygallery", 1);
		}else{
		if(document.body.scrollTop>20)
			window.scrollBy(0,-50);
		else
			stepcarousel.stepBy("mygallery", -1);
		}
}

function wheel(event){
        var delta = 0;
        if (!event) /* For IE. */
                event = window.event;
        if (event.wheelDelta) { /* IE/Opera. */
                delta = event.wheelDelta/120;
                /** In Opera 9, delta differs in sign as compared to IE.
                 */
                if (window.opera)
                        delta = -delta;
        } else if (event.detail) { /** Mozilla case. */
                /** In Mozilla, sign of delta is different than in IE.
                 * Also, delta is multiple of 3.
                 */
                delta = -event.detail/3;
        }
        /** If delta is nonzero, handle it.
         * Basically, delta is now positive if wheel was scrolled up,
         * and negative, if wheel was scrolled down.
         */
        if (delta)
                handle(delta);
        /** Prevent default actions caused by mouse wheel.
         * That might be ugly, but we handle scrolls somehow
         * anyway, so don't bother here..
         */
        if (event.preventDefault)
                event.preventDefault();
	event.returnValue = false;
}

/** Initialization code. 
 * If you use your own event management code, change it as required.
 */
if (window.addEventListener)
        /** DOMMouseScroll is for mozilla. */
        window.addEventListener('DOMMouseScroll', wheel, false);
/** IE/Opera. */
window.onmousewheel = document.onmousewheel = wheel;


