function showReviewForm(){

    $.ajaxSetup({
        cache: true
    });

    if(cimriMember != 'null' ){
        $("#reviews, #reviewPreview").hide();
        $("#reviewDialog").load("/member/reviewForm.jsp?itemId=" + selectedItem.itemId,
            function(){
                window.location.hash = "reviewDialog";
                $("#reviewDialog").show("fast",
                    function(){
                        $.scrollTo('#reviewDialog');
                    }
                    );
            }
            );
    } else {

        $("#newLoginDialog").load("/member/newLoginDialog.jsp?itemId=" + selectedItem.itemId+"&ref="+window.location+"#reviewDialog");

        jQuery.blockUI({
            css: {
                top:  '10px',
                margin: '0px 0px 0px 0pt',
                padding: '0px',
                position: 'fixed',
                color: 'none',
                width: '618px',
                color: 'rgb(0, 0, 0)',
                cursor: 'default',
                border: '0px solid #044600',
                background: 'none'
            } ,
            message: $('#newLoginDialog')

        });
    }

    $.ajaxSetup({
        cache: false
    });

    return false

}

function reviewFormSuccessHandler(response){

    if(rvStatus != "PREVIEW"){
        $("#reviewDialog").hide();
    }

    $('#reviewPreview').html(response);
    var msg = $('#reviewPreview');
    var width = $(document).width();

    jQuery.blockUI({
        message: $('#reviewPreview'),
        css: {
            width: '970px',
            left: width/2 - (msg.width() / 2)
        }
    });
}

function shareOnSocialSites(url,title,type){

    if(type=="facebook"){
        window.open("http://www.facebook.com/sharer.php?u=" + encodeURIComponent(url) + "&t=" + encodeURIComponent(title), "sharer", "toolbar=0,status=0,width=626,height=436");
    } else if(type=="friendfeed"){
        var e = document.createElement('script');
        e.setAttribute('type', 'text/javascript');
        e.setAttribute('src', 'http://friendfeed.com/share/bookmarklet/javascript?url=' + url + '&amp;title=' + title);
        document.body.appendChild(e);
    } else if(type=="twitter"){
        window.open("http://twitter.com/home?status=" + encodeURIComponent(url), "sharer", "toolbar=0,status=0,width=800,height=600");
    } else if(type=="delicious"){
        window.open("http://del.icio.us/post?url=" + encodeURIComponent(url), "sharer", "toolbar=0,status=0,width=900,height=600");
    }

}


$.metadata.setType("attr", "validate");

$(document).ready(function() {

    $.ajaxSetup({
        cache: false
    });
    var lastVisitedItemId = document.getElementById('itemId').value;
    $.ajax({
        type: "POST",
        url: "/MemberServlet/lastVisitedItem",
        data: "itemId=" + lastVisitedItemId,
        success: function(response){

        }
    });

    var listpage = "/"+selectedItem.category.catName4SEO;
    comparedCat = selectedItem.category.categoryId;


    $("#priceOptions").change(function(){
        var priceOption = this.options[this.selectedIndex].value
        if(selectedItem.merchantItems.length>0) {
            $.each(selectedItem.merchantItems,function(v) {
                var td = $('#ptablerow' + selectedItem.merchantItems[v].sid + ' td:eq(2)');
                var prc = selectedItem.merchantItems[v][priceOption];
                $(td).empty().append((prc == null || prc == ',00 TL' || prc == "")  ? '-' : prc);
            });
        }
    });

    readCompareCookies();
    // compare function
    $('#compare').click(function(event) {
        var compareUrl = 'C' + comparedCat + "_" + selectedItem.itemId;
        var anyChecked = false;

        $(".ccomp").each(function(){
            if($(this).attr('checked')){
                var itemId = this.value;
                compareUrl += "_" + itemId;
                if(!compareItemIds.contains(itemId)){
                    compareItemIds[compareItemIds.length] = itemId;
                }
                anyChecked = true;
            }
        });

        if(!anyChecked){

            $(".ccomp").each(function(){
                var itemId = this.value;
                if(!compareItemIds.contains(itemId)){
                    compareItemIds[compareItemIds.length] = itemId;
                }
            });
        }

        populateCompareCookies();
        window.location = anyChecked ? compareUrl : this.href;
        createCookie("compareReferer", window.location);
        return false;
    });
    
    $('#fiyat-alarm-btn').click(function(){
        if(cimriMember != 'null' ){
            $("#priceAlarmDialog").load("/member/fiyatAlarm.jsp?itemId=" + selectedItem.itemId +"&minPrice=" + selectedItem.minPrice);

            jQuery.blockUI({
                css: {
                    border: 'none',
                    width: '407px'
                },
                message: $('#priceAlarmDialog')
            });
        }else {
            //window.location.href="/MemberServlet?ref=" + window.location;
            $("#newLoginDialog").load("/member/newLoginDialog.jsp?itemId=" + selectedItem.itemId);

            jQuery.blockUI({
                css: {
                    top:  '10px',
                    margin: '0px 0px 0px 0pt',
                    padding: '0px',
                    position: 'fixed',
                    color: 'none',
                    width: '618px',
                    color: 'rgb(0, 0, 0)',
                    cursor: 'default',
                    border: '0px solid #044600',
                    background: 'none'
                } ,
                message: $('#newLoginDialog')

            });
        }
        return false
    });

    $('#yorum-alarm-btn').click(function(){
        if(cimriMember != 'null' ){
            $("#reviewAlarmDialog").load("/member/reviewAlarm.jsp?itemId=" + selectedItem.itemId);

            jQuery.blockUI({
                css: {
                    border: 'none',
                    width: '407px'
                },
                message: $('#reviewAlarmDialog')
            });
            $('.blockOverlay').attr('title','Click to unblock').click($.unblockUI);
        }else {
            //window.location.href="/MemberServlet?ref=" + window.location;

            $("#newLoginDialog").load("/member/newLoginDialog.jsp?itemId=" + selectedItem.itemId);

            jQuery.blockUI({
                css: {
                    top:  '10px',
                    margin: '0px 0px 0px 0pt',
                    padding: '0px',
                    position: 'fixed',
                    color: 'none',
                    width: '618px',
                    color: 'rgb(0, 0, 0)',
                    cursor: 'default',
                    border: '0px solid #044600',
                    background: 'none'
                } ,
                message: $('#newLoginDialog')

            });
        }
        return false
    });

    $.growlUI = function(message) {
        var $m = $('<div class="growlUI"></div>');
        $m.append('<h1>'+message+'</h1>');

        $.blockUI({
            message: $m,
            fadeIn: 700,
            fadeOut: 1000,
            timeout: 3000,
            showOverlay: false,
            css: {
                width: '320px',
                heigth: '300px'
            }
        });
    };

    $('#compare-box-btn').click(function(){
        $.ajax({
            type: "POST",
            url: "/MemberServlet/saveToMyList",
            data: "itemId=" + $(this).attr("itemId"),
            success: function(response){
                $.growlUI('Karşılaştırma Sepetinize Eklenmiştir');
            }
        });
    });


    $('#recommend-item-btn').click(function(){
        if(cimriMember != 'null' ){
            $("#recommendDialog").load("/member/recommendItem.jsp?itemId=" + selectedItem.itemId);

            jQuery.blockUI({
                css: {
                    border: 'none',
                    width: '407px'
                },
                message: $('#recommendDialog')
            });
            $('.blockOverlay').attr('title','Click to unblock').click($.unblockUI);
        }else {
            //window.location.href="/MemberServlet?ref=" + window.location;

            $("#newLoginDialog").load("/member/newLoginDialog.jsp?itemId=" + selectedItem.itemId);

            jQuery.blockUI({
                css: {
                    top:  '10px',
                    margin: '0px 0px 0px 0pt',
                    padding: '0px',
                    position: 'fixed',
                    color: 'none',
                    width: '618px',
                    color: 'rgb(0, 0, 0)',
                    cursor: 'default',
                    border: '0px solid #044600',
                    background: 'none'
                } ,
                message: $('#newLoginDialog')

            });

            $('.blockOverlay').attr('title','Click to unblock').click($.unblockUI);
        }
        return false
    });

    $('#recommend-item-btn2').click(function(){
        if(cimriMember != 'null' ){
            $("#recommendDialog").load("/member/recommendItem.jsp?itemId=" + selectedItem.itemId);

            jQuery.blockUI({
                css: {
                    border: 'none',
                    width: '407px'
                },
                message: $('#recommendDialog')
            });
            $('.blockOverlay').attr('title','Click to unblock').click($.unblockUI);
        }else {
            //window.location.href="/MemberServlet?ref=" + window.location;

            $("#newLoginDialog").load("/member/newLoginDialog.jsp?itemId=" + selectedItem.itemId+"&loginOnProductPage="+selectedItem.title4SEO);

            jQuery.blockUI({
                css: {
                    top:  '10px',
                    margin: '0px 0px 0px 0pt',
                    padding: '0px',
                    position: 'fixed',
                    color: 'none',
                    width: '618px',
                    color: 'rgb(0, 0, 0)',
                    cursor: 'default',
                    border: '0px solid #044600',
                    background: 'none'
                } ,
                message: $('#newLoginDialog')

            });
            $('.blockOverlay').attr('title','Click to unblock').click($.unblockUI);
        }
        return false
    });


    if(window.location.hash === "#reviewDialog"){
        showReviewForm();
    }

    $('#review-btn').click(showReviewForm);

    if(imgcount > 4){
        $('#aimagecont').jCarouselLite({
            visible: 4,
            auto: 3000,
            btnNext: "#rightArrow",
            btnPrev: "#leftArrow"
        });
    }

    $("#llistpage").click(function(){
        window.location = listpage
        return false;
    });

    /*        $('.lredirect').click(function(){
            openInNewWindow($(this).attr("href"));
            $.ajax({ type: "GET", async: false, url: "/ClickServlet?sid=" + $(this).attr('sid') });
            pageTracker._trackPageview('/outgoing/' + $(this).attr("murl") + "/" + $(this).attr("href"));
            $("#adword").load("/includes/adword.jsp");
            //window.location.replace(selectedItem.path4SEO + "?ad=1");
            return false;
        });*/

    $('.ladwords').live("click",function(ev, ob){
        window.location.replace(selectedItem.path4SEO + "?ad=1");
        return true;
    });


    $("#preview-close-review-btn").live("click", function(){
        if(rvStatus != "PREVIEW") {
            $('#reviews').hide().load("/includes/reviews.jsp?itemId=" + selectedItem.itemId + "&refresh=true").show();
            window.location.hash = "";
        }
        $.unblockUI();
    });

    $("#preview-draft-review-btn").live("click", function(){
        $('#reviewForm #reviewStatus').val("DRAFT");
        $.unblockUI();

        if($('#reviewForm').valid()){
            $('#reviewForm').ajaxSubmit({
                async: false,
                success: function(response){
                    $.growlUI('Yorumunuz kaydedildi');
                }
            });
        }

        $("#reviewDialog").hide();
        $('#reviews').hide().load("/includes/reviews.jsp?itemId=" + selectedItem.itemId + "&refresh=true").show();
        window.location.hash = "";
    });

    $("#preview-publish-review-btn").live("click", function(){
        $('#reviewForm #reviewStatus').val("PUBLISHED");
        $.unblockUI();
        $('#reviewForm').submit();

        if($('#reviewForm').valid()){
            $('#reviewForm').ajaxSubmit({
                async: false,
                success: function(response){
                    $.growlUI('Yorumunuz kaydedildi');
                }
            });
        }

        $("#reviewDialog").hide();
        $('#reviews').hide().load("/includes/reviews.jsp?itemId=" + selectedItem.itemId + "&refresh=true").show();
        window.location.hash = "";
    });

    //setupZoom();

    $("a#moreDetails").click(function(){
        $("#allOf").toggle();
        $("a#moreDetails").remove();
    });
    //jQuery(document.body).imageZoom();


    /** Vitrin urunlere highlight*/
    $('.showcased-item-div').effect('highlight', {}, 5000);
    setInterval ("$('.showcased-item-div').effect('highlight', {}, 5000)", 5000 );

    $('#likeButton').click(function(){
        $.ajax({
            type: "POST",
            url: "/MemberServlet/likeProcess",
            /*type positive(p) veya negative(n) olabilir.*/
            data: "itemId=" + $(this).attr("itemId") +"&type=p",
            success: function(response){
                var $m;
                if(response != "-1"){
                    if(!isNaN(response))
                    $('#likeCount').text(response);                   
                    $.blockUI({
                        message: 'Teşekkürler',
                        fadeIn: 700,
                        fadeOut: 1000,
                        timeout: 3000,
                        showOverlay: false,
                        css: {
                           
                            top: '474px',
                            left: '557px',
                            right: '10px',
                            width: '320px',
                            heigth: '300px'
                        }
                    });
                }
                else{                  
                    
                    $.blockUI({
                        message: 'Bu ürünü daha önce beğendiniz.',
                        fadeIn: 700,
                        fadeOut: 1000,
                        timeout: 3000,
                        centerY:0,
                        showOverlay: false,
                        css: {
                           
                            top: '474px',
                            left: '557px',
                            right: '10px',
                            width: '320px',
                            heigth: '300px'
                        }
                    });
                }
            }
        });

    })


    if(selectedItem.brandPopup != ''){
        $.blockUI({
            message: $('#brandPopup'),
            css: {
                top:  ($(window).height() - 500) /2 + 'px',
                left: ($(window).width() - 500) /2 + 'px',
                width : $('#brandPopup').width(),
                height : $('#brandPopup').height()
            }
            
        });

        $('.blockOverlay').click($.unblockUI);
    }


});

