﻿$(function () {
    var semt = $(".semt");
    semt.bind('change', function () {
        window.open(this.value, "_self");
    });
    semt.parents(".Select:first").find("span").html(semt.find("option:selected").html());
    setMainContentHeight();
});

function setMainContentHeight() {
    $('.MainContent').css('min-height', $.htmlheight() - ($('#HeaderWrapper').height() + $('#FooterWrapper').height() + 30));
}

function onDefaultLoad() {
    var $qa = $('.QuestionArea');
    $('.StartBtn, .Continue', $qa).click(function () {
        var $p = $(this).parents('li.QuestionStart');
        var index = $('li.QuestionStart', $qa).index($p);
        if (index > 0 ? $('.Params li.Selected', $p).length > 0 : true) {
            $('ul:first', '.QuestionArea').animate({ marginLeft: ((index + 1) * -616) })
        }
        else { alert("Lütfen seçim yapınız."); }

        return false;
    });
    $('.Params li', $qa).click(function () {
        var $t = $(this);
        var $he = $('input[name="{0}"]'.format($t.parent().attr('t')), $qa);
        var val = $he.val();

        if ($t.hasClass('Selected')) {
            $t.removeClass('Selected');
            val = val.remove($t.attr('id'));
        }
        else {
            $t.addClass('Selected');
            val = val.add($t.attr('id'));
        }
        $he.val(val);
    });
    $('.EndButton', $qa).click(function () {
        getPermissions(function (session, fromLogin) {
            var resultAction = function (result) {
                $('input[name="access_token"]', $qa).val(session.accessToken);
                $('input[name="fbid"]', $qa).val(session.userID);
                $qa.submit();
            }

            if (fromLogin) {
                FB.api('/me/?fields=id,first_name', function (result) {
                    FB.api('/me/feed/', 'post',
                    {
                        message: '{0} bu yılbaşı nerede eğleneceğini Yılbaşında İstanbul’da ile keşfetti. Peki ya sen?'.format(result.first_name),
                        name: '2012 Yılbaşı, İstanbul, Etkinlik, Program, Parti, Konser, Restoran, Otel',
                        picture: 'http://www.yilbasindaistanbulda.com/2012/Media/Images/FacebookPostImage.jpg',
                        link: 'http://www.yilbasindaistanbulda.com/',
                        caption: '2012 yılbaşında İstanbuldaki restoran, performans hall ve otellerde gerçekleşecek yeni yıl etkinlikleri ve programları Yılbaşında İstanbulda sitesinde.',
                        description: 'Tarzınıza uygun yılbaşı etkinliklerini www.yilbasindaistanbulda.com sitesinden hemen keşfedin.'
                    }, resultAction);
                });
            }
            else resultAction();
        });
        return false;
    });
    $('li img', '.FoursquareTicket ul').hover(function () {
        var $bi = $('img.BigImg', '.FoursquareTicket');
        $bi.attr('src', $(this).attr('big'));
        $bi.attr('alt', $(this).attr('alt'));
    });
    $('a.TicketBtn', '.FoursquareTicket').click(function () {
        openBiletKazan($(this).attr('href'));
        return false;
    });
}
function onBiletKazanLoad() {

    $('.DetaildBtn', 'ul.TicketList:first').click(function () {
        var $p = $(this).parent();
        openBiletKazan($(this).attr('href'));

        return false;
    });
}
function onResultLoad() {
    $('.ShareFriendsBtn').click(function () {
        var $t = $(this).parent();
        initFB();
        FB.ui({
            method: 'feed',
            name: $t.attr('title'),
            link: $t.attr('href'),
            picture: $t.attr('img'),
            caption: $t.attr('subtitle')
        });
        return false;
    });
}
function openBiletKazan(val) {
    getPermissions(function () {
        FB.api('/me?fields=id,name', function (result) {
            openLightBox(function () {
                var $lw = $('.LightBoxTicketWrap');

                $('option[value="{0}"]'.format(val), $lw).attr('selected', 'selected');

                $('.ProfileName img', $lw).attr('src', 'http://graph.facebook.com/{0}/picture?type=square'.format(result.id));
                $('.ProfileName strong', $lw).text(result.name);
                $('input[name="fbid"]', $lw).val(result.id);

                $('.SentContactForm', $lw).click(function () {
                    var $f = $(this).parents('form.LightBoxTicket');
                    sendForm($f.serialize(), '/2012/ajax.aspx', closeLightBox);
                    return false;
                });
            });
        });
    });
}
function openLightBox(opened) {
    if ($('.LightBoxTicketWrap').length == 0) {
        $.get('/2012/ajax.aspx', function (result) {
            $('body').append(result);
            $('.LightBoxTicketBg').show().height($.htmlheight());
            $('.LightBoxTicketWrap').show();
            $('.CloseBtn', '.LightBoxTicketWrap').click(closeLightBox);
            $('.LightBoxTicketBg').click(closeLightBox);
            if ($.isFunction(opened)) opened();
        });
    }
}
function closeLightBox() {
    $('.LightBoxTicketBg, .LightBoxTicketWrap').remove();
    return false;
}

function getPermissions(allowed) {
    initFB();
    FB.getLoginStatus(function (status) {
        if (status.authResponse) {
            allowed(status.authResponse, false);
        } else {
            FB.login(function (response) {
                if (response.authResponse) {
                    allowed(response.authResponse, true);
                }
            }, { scope: neededPerms });
        }
    });
}
function initFB() {
    FB.init({
        appId: appID,
        channelUrl: 'http://www.yilbasindaistanbulda.com/2012/channel.aspx',
        cookie: true,
        status: true,
        xfbml: true,
        oauth: true
    });
}

