﻿var ajaxService = null;
var redirect = '';
var pageName = '';
$(document).ready(function() {
    ajaxService = CIO.WebSite.Support.AjaxService;
	if($('input[name="loginalready"]').val()=='True') {
	    if(!$('#vote').hasClass('Disable')) {
	        $('#vote').bind('click', function() {
                ajaxService.Vote($('input[name="cultureinfo"]').val(),
                                    $('input[name="designid"]').val(),
                                    vote_callback);            
            });	
        }
    }
	
    $(".LightBoxCaller").addLightBoxControl({
        closeBtnText: "X",
        themeClass: "PopupStyle1"
    });
       
    $('#permalink').bind('keypress', function(evn) {
        if (evn.keyCode == 13 || window.event.keyCode == 13) {
            return false;
        }
        return true;
    });
});

function removeAllErrorMessage() {
    $('p.ErrorMsg').each(function() {
        $(this).parent().removeClass('Error');
        $(this).text('');
    });
}

function openConfirm(error) {
    closePopup('Confirm.aspx?' + Math.random());
    return false;
}

function sendMails() {
    removeAllErrorMessage();
    var emails = '';    
    $('input[name="FriendEmail"]').each(function(){
        emails = emails + getValue($(this)) + ';';
    });
    ajaxService.sendMails($('input[name="cultureinfo"]').val(),
                            $('input[name="designid"]').val(),
                            getValue('#yourEmail'),
                            emails.substr(0, emails.length - 1),
                             $('input[name="url"]').val(),
                            sendMail_callback);
}

function sendNewPassword() {
    removeAllErrorMessage();
    ajaxService.SendNewPassword($('input[name="cultureinfo"]').val(),
                                getValue('#email'),
                                sendnewpassword_callback);
}

function sendnewpassword_callback(res) {
    if (res.value.length == 0) { // sucess
        openConfirm();
    } else {
        bindingError(res.value);
    }
}

function getValue(id) {
    return $(id).val() == $(id).attr('rel') ? '' : $(id).val();
}

function refreshCapcha() {
    if($('.ImgVerify').length == 0) return;
    var src = $('.ImgVerify').attr('src');
    if (src.indexOf('?') >= 0) {
        src = src.substring(0, src.indexOf('?'))
    }
    src += "?" + Math.random();
    $('.ImgVerify').attr('src', src);
    $('#captcha').val('');
}

function bindingError(obj) {
    for(var i = 0; i < obj.length; i++) {
        $('p.' + obj[i].PropertyName + 'Error').text(obj[i].ErrorMessage);
        var p = $('p.' + obj[i].PropertyName + 'Error').parent();
        if(!$(p).hasClass('Error')) {
            $(p).addClass('Error');
        }
        refreshCapcha();        
    }
}

function vote_callback(res) {
    if (res.value.length == 0) { // sucess
        redirect = location.href + '&vote=' + Math.random()* 100000;
        statisticVoteConfirm();
        openConfirm();
    } else {
        openConfirm(res.value[0].ErrorMessage);
    }
}

function signup_callback(res) {
    if (res.value.length == 0) { // sucess
        openConfirm();
    } else {
        bindingError(res.value);
    }
}

function sendMail_callback(res) {
    if (res.value.length == 0) { // sucess
        statisticSendMailConfirm();
        openConfirm();
    } else {
        bindingError(res.value);
    }
}

function signin_callback(res) {
    if(res.value && res.value.length) {
        if (res.value[0].PropertyName != 'General') {
            bindingError(res.value);
        } else {
            if(res.value[1]) {
                redirect = location.href;
                redirect += '&vote=' + Math.random()* 100000;
                statisticVoteConfirm();
            }
            $('input[name="loginalready"]').val('true');   
            if(!$('#vote').hasClass('Disable')) {
                $('#vote').addClass('Disable');
                $('#vote').unbind("click");
                $('#vote').attr('href', 'javascript: void(0);');
            }            
            openConfirm();            
        }
    }
}

function statisticSignInInit() {
    if($('input[name="loginalready"]').val()=='True') return false;
    s.linkTrackVars='pageName,hier1,channel,prop1,prop2,prop3,prop4';
    s.linkTrackEvents='';
	s.pageName='MDC-vote form-log in';
	s.hier1='MDC|gallery|medal|vote form';
	s.channel='MDC';
	s.prop2='MDC:gallery';
	s.prop3='MDC:gallery:medal';
	s.prop4='MDC:gallery:medal:vote form';
	return true;
}

function statisticVoteConfirm() {
    s.linkTrackVars = 'eVar15,events,prop1,prop7,prop8,prop9';
    s.linkTrackEvents = 'event14';
    s.prop7 = 'Voted';
    s.events = 'event14';
    s.prop8 = pageName;
    s.eVar15 = pageName;      
    s.tl($('#vote'),'o','MDC-vote');
}

function statisticSendMailInit() {
    s.linkTrackVars='eVar15,events,prop1,prop7,prop8,prop9';
    s.linkTrackEvents='event14';
    s.prop7='Send to a friend initiated';
    s.events='event14';
    s.prop8 = pageName;
    s.eVar15 = pageName;    
}

function statisticSendMailConfirm() {
    s.linkTrackVars = 'eVar15,events,prop1,prop7,prop8,prop9';
    s.linkTrackEvents = 'event14';
    s.prop7 = 'Send to a friend completed';
    s.events = 'event14';
    s.prop8 = pageName;
    s.eVar15 = pageName;
    s.tl($('#sendMail'),'o','MDC-send-complete')
}

function statisticSharing(id, dm) {
    s.linkTrackVars = 'eVar15,events,prop1,prop7,prop8,prop9';
    s.linkTrackEvents = 'event14';
    s.prop7 = 'Shared';
    s.prop9 = dm;
    s.events = 'event14';
    s.prop8 = pageName;
    s.eVar15 = pageName;
}

function sharing(url) {
    var u = encodeURIComponent($('#permalink').val());
    var t = encodeURIComponent(document.title);
    do {
        url = url.replace('$url', u);
    } while (url.indexOf('$url') >= 0);
    do {
        url = url.replace('$title', t);
    } while (url.indexOf('$title') >= 0);
    window.open(url);
}

function shareFacebook() {
    sharing('http://www.facebook.com/share.php?u=$url&t=$title');
}

function shareTwitter() {
    sharing('http://twitter.com/home?status=$title:+$url');
}

function shareDelicious() {
    sharing('http://delicious.com/save?url=$url&title=$title');
}

function shareMySpace() {
    sharing('http://www.myspace.com/Modules/PostTo/Pages/?t=$title&c=$title&u=$url&l=');
}

function shareDigg() {
    sharing('http://digg.com/submit?url=$url&type=2&title=$title');
}

function shareStumbleupon() {
    sharing('http://www.stumbleupon.com/submit?url=$url&title=$title');
}

function shareRenren () {
    sharing('http://share.renren.com/share/buttonshare.do?link=$url&title=$title');
}

function shareKaixin001 () {
    sharing('http://www.kaixin001.com/repaste/share.php?rurl=$url&rcontent=$title&rtitle=$title');
}

function shareOrkut () {
    sharing('http://promote.orkut.com/preview?nt=orkut.com&tt=$title&du=$url');
}