var slide = null;
var imgs = null;
var timer = null;
var shrinkTimer = null;
var slideTimer = null;
var startTimer = null;
var limit = 9;
var focusRank = 0;

var current = null;
var cursor = null;

var focusFlag = false;
var slideAnim = false;
var focusAnim = false;
var blurAnim = false;

var stopFlag = false;
var navFlag = false;
var mOutFlag = false;
var preventSlide = false;

function checkQ() {
    if (/^[0-9]{1,2}$/.exec($("#quantity").val()))
        return true;

    return false;
}

function disable() {
    return false;
}

function adetdegistir(isaret) {
    var adet = parseInt($("#quantity").val());
    if (isNaN(adet)) {
        adet = 0;
    }
    if (isaret == "+") {
        $("#quantity").val(adet + 1);
    }
    else if (isaret == "-") {
        if (adet < 2)
            adet = 2;
        $("#quantity").val(adet - 1);
    }
}

var bindVideoClicks = function() {
    $("#paging-mostviewed a").bind("click", pageMostViewedVideo);
    $("#paging-recent a").bind("click", pageRecentVideo);

    $("#vids .right .tabs li").click(videoTab);
}

var pageMostViewedVideo = function() {
    var rep = $("#paging-mostviewed span");

    var index = parseInt($(this).html());
    rep.replaceWith($.create("a", { href: "#" }).html(rep.html()).bind("click", pageMostViewedVideo));
    $(this).replaceWith('<span class="current">' + $(this).html() + '</span>');
    $("#video-mostviewed .vid").hide();
    $("#video-mostviewed .vid").each(function(i, n) {
        if (i >= (index - 1) * 6 && i < (index - 1) * 6 + 2)
            $(this).show();
    });


    return false;
}

var pageRecentVideo = function() {
    var rep = $("#paging-recent span");

    var index = parseInt($(this).html());
    rep.replaceWith($.create("a", { href: "#" }).html(rep.html()).bind("click", pageRecentVideo));
    $(this).replaceWith('<span class="current">' + $(this).html() + '</span>');
    $("#video-recent .vid").hide();
    $("#video-recent .vid").each(function(i, n) {
        if (i >= (index - 1) * 6 && i < (index - 1) * 6 + 2)
            $(this).show();
    });


    return false;
}

var videoTab = function() {
    var id = /^tab\-(.+)$/.exec($(this).attr("id"))[1];
    $("#vids .right .tabs li").removeClass();
    $(this).addClass("selected");
    $(".vidcontent").hide();
    $(".vidpaging").hide();
    $("#video-" + id).show();
    $("#paging-" + id).show();
}


function playVideo(id) {
    $("#vids .left h3").html($("#vid_" + id + " p.tit").html());
    $("#vids .left p.descr").html($("#vid_" + id + " p.descr").html());
    $("#vids .left p.usr").html($("#vid_" + id + " p.usr").html());

    $.get("/usermedia/play.aspx?video.id=" + id, function(data) {
        flashembed("player",
            {
                src: 'http://www.buldumbuldum.com/content/movies/FlowPlayerDark.swf',
                width: 368,
                height: 276
            },
            {
                config: {
                    autoPlay: false,
                    autoBuffering: true,
                    loop: false,
                    initialScale: 'fit',
                    controlBarBackgroundColor: '0x2e8860',
                    videoFile: data
                }
            }
        );
    });

    return false;
}

function showStockInfo() {
    bindVideoClicks();

    if (typeof (firstVideoPath) != "undefined") {
        flashembed("player",
            {
                src: 'http://www.buldumbuldum.com/content/movies/FlowPlayerDark.swf',
                width: 368,
                height: 276
            },
            {
                config: {
                    autoPlay: false,
                    autoBuffering: true,
                    loop: false,
                    initialScale: 'fit',
                    controlBarBackgroundColor: '0x2e8860',
                    videoFile: firstVideoPath
                }
            }
        );
    }





    $(".stockinfo").hide();
    if ($("#stockinfo_" + $("#colorid").val()).html() != null) {
        $("#stockinfo_" + $("#colorid").val()).show();
        if ($("#stockinfo_" + $("#colorid").val()).is(".outofstock")) {
            $("#form-addtocart").unbind("submit", checkQ);
            $("#form-addtocart").bind("submit", disable);
        }
    }
    else {
        $("#form-addtocart").unbind("submit", disable);
        $("#form-addtocart").bind("submit", checkQ);
    }

    popPrice();
}

//price population
var popPrice = function() {
    $("#opts h3").html($("#colorprice_" + $("#colorid").val()).html());
}

$().ready(function() {

    showStockInfo();

    slide = $("#slide");
    imgs = $("#slide img");

    setFocusRank();
    startSlide();

    imgs.hoverIntent(config).click(function() {
        var start = /[0-9]+/.exec($(this).attr("class"));
        start = start - 1;
        return popitup('/popup/bbgaleri.aspx?urun.id=' + productID + '#' + start, 850, 690, 1);
    });
    $("#next").bind("click", navNext).hoverIntent(config2);
    $("#prev").bind("click", navPrev).hoverIntent(config2);

    slide.hoverIntent(config2);


    $(".uimg").click(function() {
        var start = /[0-9]+/.exec($(this).attr("class"));
        start = start - 1;
        return popitup('/popup/bbgaleri.aspx?urun.id=' + productID + '#' + start, 850, 690, 1);
    })

    if (typeof (currentVid) != "undefined" && currentVid != null && currentVid != "") {
        playVideo(currentVid);
    }

    bindForward();
    bindBack();
});

var slideMOver = function() {
    preventSlide = true;
    stopFlag = true;
    clearTimeout(timer);
    clearTimeout(shrinkTimer);
}

var slideMOut = function() {
    preventSlide = false;
    stopFlag = false;
    if (!mOutFlag)
        shrink();
}

var setFocusRank = function() {
    if (imgs.length == 1)
        focusRank = 0;
    else if (imgs.length == 2)
        focusRank = 1;
    else if (imgs.length > limit)
        focusRank = 3;
    else
        focusRank = Math.floor(imgs.length / 2) - 1;
}

var startSlide = function() {
    cursor = $(imgs[focusRank]);
    grow();
}

var navNext = function() {
    $("#next").unbind("click", navNext);
    $("#prev").unbind("click", navPrev);
    clearTimeout(timer);
    clearTimeout(shrinkTimer);
    stopFlag = true;
    navFlag = true;
    shrink();
    slideNext();
}

var navPrev = function() {
    $("#next").unbind("click", navNext);
    $("#prev").unbind("click", navPrev);
    clearTimeout(timer);
    clearTimeout(shrinkTimer);
    stopFlag = true;
    navFlag = true;
    shrink();
    slidePrev();
}

var slideNext = function() {
    if (imgs.length > 2) {
        var left = parseInt(/[\-0-9]+/.exec(slide.css("left"))) - 75;
        var padLeft = parseInt(/[\-0-9]+/.exec(slide.css("padding-left"))) + 75;
        slideAnim = true;
        slide.animate({ left: left }, { duration: 200, easing: "easeInOutQuad", complete: function() {
            if (imgs.length > 1) {
                var item = $(imgs[0]);
                item.remove();
                slide.css("padding-left", padLeft);
                item.hoverIntent(config).click(function() {
                    var start = /[0-9]+/.exec($(this).attr("class"));
                    start = start - 1;
                    return popitup('/popup/bbgaleri.aspx?urun.id=' + productID + '#' + start, 850, 690, 1);
                }).appendTo(slide);
                imgs = $("#slide img");
            }

            $("#next").bind("click", navNext);
            $("#prev").bind("click", navPrev);

            slideAnim = false;
            navFlag = false;
            cursor = $(imgs[focusRank]);
            timer = setTimeout("grow()", 200);
        }
        });
    }
    else {
        focusRank++;
        focusRank = focusRank >= imgs.length ? 0 : focusRank;
        cursor = $(imgs[focusRank]);
        timer = setTimeout("grow()", 200);
    }
}

var slidePrev = function() {
    var left = parseInt(/[\-0-9]+/.exec(slide.css("left"))) + 75;
    var padLeft = parseInt(/[\-0-9]+/.exec(slide.css("padding-left"))) - 75;
    slideAnim = true;
    if (imgs.length > 1) {
        var item = $(imgs[imgs.length - 1]);
        item.remove();
        slide.css("padding-left", padLeft);
        item.hoverIntent(config).click(function() {
            var start = /[0-9]+/.exec($(this).attr("class"));
            start = start - 1;
            return popitup('/popup/bbgaleri.aspx?urun.id=' + productID + '#' + start, 850, 690, 1);
        }).prependTo(slide);
        imgs = $("#slide img");
    }
    slide.animate({ left: left }, { duration: 300, easing: "easeInOutQuad", complete: function() {
        $("#next").bind("click", navNext);
        $("#prev").bind("click", navPrev);

        slideAnim = false;
        navFlag = false;
        cursor = $(imgs[focusRank]);
        timer = setTimeout("grow()", 200);
    }
    });
}

var grow = function() {
    if (!navFlag) {
        focusAnim = true;
        var img = cursor;

        img.animate({ width: 154, height: 154, top: 0 }, { duration: 190, easing: "easeInOutQuad", complete: function() {

            focusFlag = true; focusAnim = false;
            if (!stopFlag && imgs.length > 1)
                timer = setTimeout("shrink()", 1300);
        }
        });
    }
}


var shrink = function() {
    blurAnim = true;

    var img = cursor;

    img.animate({ width: 64, height: 64, top: 45 }, { duration: 190, easing: "easeInOutQuad", complete: function() {
        mOutFlag = false;
        blurAnim = false;
        focusFlag = false;
        if (!stopFlag && !preventSlide)
            timer = setTimeout("slideNext()", 200);
    }
    });
}


var mOver = function() {
    stopFlag = true;
    clearTimeout(timer);
    clearTimeout(shrinkTimer);
    if ($(this).attr("id") != cursor.attr("id")) {
        shrink();
        cursor = $(this);
        grow();
    }
}

var mOut = function() {
    stopFlag = false;
    mOutFlag = true;
    shrink();
}

var config = {
    sensitivity: 7, // number = sensitivity threshold (must be 1 or higher)    
    interval: 190, // number = milliseconds for onMouseOver polling interval    
    over: mOver, // function = onMouseOver callback (REQUIRED)    
    timeout: 190, // number = milliseconds delay before onMouseOut    
    out: mOut // function = onMouseOut callback (REQUIRED)    
};

var config2 = {
    sensitivity: 7, // number = sensitivity threshold (must be 1 or higher)    
    interval: 200, // number = milliseconds for onMouseOver polling interval    
    over: slideMOver, // function = onMouseOver callback (REQUIRED)    
    timeout: 200, // number = milliseconds delay before onMouseOut    
    out: slideMOut // function = onMouseOut callback (REQUIRED)    
};

//gallery scroller
var bindForward = function() {
    $("#userfotonext").bind("click", goForward);
}

var bindBack = function() {
    $("#userfotoprev").bind("click", goBack);
}

var goForward = function() {
    var scroller = $("#userfotoscroller");
    var left = parseInt(/[\-0-9]+/.exec(scroller.css("left")));
    if (left > (-1) * (scrollerSize - 8 * 70)) {
        $("#userfotonext").unbind("click", goForward);
        left -= 70;
        scroller.animate({ left: left }, { duration: 300, easing: "easeInOutQuad", complete: bindForward });
    }
    return false;
}

var goBack = function() {
    var scroller = $("#userfotoscroller");
    var left = parseInt(/[\-0-9]+/.exec(scroller.css("left")));
    if (left < 0) {
        $("#userfotoprev").unbind("click", goBack);
        left += 70;
        scroller.animate({ left: left }, { duration: 300, easing: "easeInOutQuad", complete: bindBack });
    }
    return false;
}

var picUpload = function() {
    tb_show(null, '#TB_inline?height=376&width=564&inlineId=picupload', false);
    return false;
}

var picUpload2 = function(adres) {
    var h = 370;
    var w = 560;
    newwindow = window.open(adres, '_blank', 'toolbar=0,scrollbars=0,location=0,status=0,menubar=0,resizable=0,titlebar=0,height=' + h + ',width=' + w);
    if (window.focus) { newwindow.focus() }
    return false;
}

var videoUpload = function() {
    tb_show(null, '#TB_inline?height=376&width=564&inlineId=videoupload', false);
    return false;
}

var videoUpload2 = function(adres) {
    var h = 370;
    var w = 560;
    newwindow = window.open(adres, '_blank', 'toolbar=0,scrollbars=0,location=0,status=0,menubar=0,resizable=0,titlebar=0,height=' + h + ',width=' + w);
    if (window.focus) { newwindow.focus() }
    return false;
}

var loginpls = function() {
    tb_show(null, '#TB_inline?height=376&width=564&inlineId=loginpls', false);
    return false;
}




//
$(document).ready(function() {
    BindGallery();
});

function BindGallery() {
    $(".ext-desc img").click(function() {
        var secili = $(this).attr("src");
        var arr = $(".ext-desc img");
        var items = "val=";
        var index = 0;
        var seciliindex = 0;

        jQuery.each(arr, function() {
            index = index + 1;
            if ($(this).attr("src") == secili)
                seciliindex = index;
            items += $(this).attr("src");
            items += "|";
        });
        seciliindex = seciliindex - 1;

        return popitup('/popup/bbeditorgaleri.aspx?urun.id=' + productID + '&' + items + '&secili=' + secili + '#' + seciliindex, 850, 690, 1);
    });
}