var ANA = {
    swfId: 'flashContent',
    lock: true,
    sleep: 700,
    target: {
        areaList: '#areaListBlock div.area ul li a',
        girlsList: '#girlsListBlock ul li a',
        DL: false,
        amc: false,
        navi: '#naviArea ul li a'
    },
    current: false
};

if (!!window.opera && !window.console) {
    console = { log: opera.postError };
}

function URB_debugPrint(msg) {
//    console.log(msg);
}

function unlockButton() {
    if (jQuery().getLockFlag()) {
        jQuery().unlockFlag();
    }
}

function learnAmcMobile() {
    if (jQuery().getCurrentMenu() == 'amc') {
        smartScroll.start('#naviArea');
        URB_debugPrint('amc / scroll only');
    } else {
        jQuery('#amc').click();
    }
}

function soundOffApper() {
    URB_debugPrint('soundOffApper');
    jQuery().changeSoundIconOff();
}

function soundOnApper() {
    URB_debugPrint('soundOnApper');
    jQuery().changeSoundIconOn();
}

jQuery.fn.initAnaWithFlash = function(){
    URB_debugPrint('with Flash');
    
    ANA.effectLocked = function(){
        if (ANA.target[ANA.current]) {
//            jQuery(ANA.target[ANA.current]).animate({ opacity: 0.3 }, 200);
        }
    };

    ANA.effectUnlocked = function(){
        if (ANA.target[ANA.current]) {
//            jQuery(ANA.target[ANA.current]).animate({ opacity: 1.0 }, 1500);
        }
    };
    
    ANA.effectSound = function(){
        jQuery('#soundToggle a').toggle();
    };
    
    jQuery().initNavigation();
    
    jQuery(ANA.target['areaList']).click(function(){
        jQuery().sendCallBackChapter(this.id);
    });
    jQuery(ANA.target['girlsList']).click(function(){
        jQuery().sendCallBackGirl(this.id);
    });
    jQuery('#segConditionForm').submit(function(){
        jQuery().sendCallBackPause();
    });
    jQuery('#logo').click(function(){
        jQuery().sendCallBackLogoClick();
    });
    jQuery('#amcBlock a[target="_blank"]').click(function(){
        jQuery().sendCallBackLogoClick();
    });
    jQuery('#footSection map area').click(function(){
        jQuery().sendCallBackLogoClick();
    });
};

jQuery.fn.initAnaWithoutFlash = function(){
    URB_debugPrint('No Flash');
    
    ANA.effectLocked = function(){};
    ANA.effectUnlocked = function(){};
    ANA.effectChangeVisual = function(target, areaId){
//        jQuery(target).fadeOut('normal', function(){
//            jQuery(target).fadeIn('normal');
//        });
    };
    
    jQuery().initNavigation();
    
    jQuery(ANA.target['areaList']).click(function(){
        jQuery().changeVisual(this.id);
    });
    jQuery(ANA.target['girlsList']).click(function(){
        jQuery().changeVisual(this.id);
    });
    jQuery('#soundToggle').hide();
};

jQuery.fn.initNavigation = function(){
    jQuery(ANA.target['navi']).click(function(){
        var menuBlock = '#' + this.id + 'Block';
        jQuery().menuClick(this.id);

        if (jQuery().getCurrentMenu() == this.id) {
            jQuery(menuBlock).slideUp('slow');
            jQuery().setCurrentMenu(false);
        } else if (jQuery().getCurrentMenu()) {
            jQuery().menuOut(jQuery().getCurrentMenu());
            jQuery('#' + jQuery().getCurrentMenu() + 'Block').slideUp('slow', function(){
                jQuery(menuBlock).slideDown('slow',function(){
                    smartScroll.start('#naviArea');
                });
            });
            jQuery().setCurrentMenu(this.id);
            ANA.effectUnlocked();
        } else {
            jQuery(menuBlock).slideDown('slow',function(){
                smartScroll.start('#naviArea');
            });
            jQuery().setCurrentMenu(this.id);
            ANA.effectUnlocked();
        }
    });

    jQuery('#naviArea ul li a').hover(function(){
        if (jQuery().getCurrentMenu() != this.id) {
            jQuery().menuOver(this.id);
        }
    }, function(){
        if (jQuery().getCurrentMenu() != this.id) {
            jQuery().menuOut(this.id);
        }
    });
};

jQuery.fn.getCurrentMenu = function(){
    return ANA.current;
};

jQuery.fn.setCurrentMenu = function(currentMenu){
    ANA.current = currentMenu;
    return this;
};

jQuery.fn.menuOver = function(menuId){
    jQuery('#' + menuId + 'MenuClick').hide();
    jQuery('#' + menuId + 'MenuOut').hide();
    jQuery('#' + menuId + 'MenuOver').show();
};

jQuery.fn.menuOut = function(menuId){
    jQuery('#' + menuId + 'MenuClick').hide();
    jQuery('#' + menuId + 'MenuOver').hide();
    jQuery('#' + menuId + 'MenuOut').show();
};

jQuery.fn.menuClick = function(menuId){
    jQuery('#' + menuId + 'MenuOut').hide();
    jQuery('#' + menuId + 'MenuOver').hide();
    jQuery('#' + menuId + 'MenuClick').show();
};

jQuery.fn.sendCallBackChapter = function(chapterId){
    if (jQuery().getLockFlag()) {
        return;
    }
    
    jQuery().lockFlag();

    try {
        jQuery().getSwf(ANA.swfId).callBackChapter(chapterId);
    } catch (e) {
        return;
    }
    
    smartScroll.start('#pagetop');
    URB_debugPrint('chapter ' + chapterId);
};

jQuery.fn.sendCallBackGirl = function(girlId){
    if (jQuery().getLockFlag()) {
        return;
    }

    jQuery().lockFlag();

    try {
        jQuery().getSwf(ANA.swfId).callBackGirl(girlId);
    } catch (e) {
        return;
    }

    smartScroll.start('#pagetop');
    URB_debugPrint('girl ' + girlId);
};

jQuery.fn.sendCallBackSoundOn = function(){
    try {
        jQuery().getSwf(ANA.swfId).callBackSoundOn();
    } catch (e) {
        return;
    }
    
    ANA.effectSound();
    URB_debugPrint('sound on');
};

jQuery.fn.sendCallBackSoundOff = function(){
    try {
        jQuery().getSwf(ANA.swfId).callBackSoundOff();
    } catch (e) {
        return;
    }
    
    ANA.effectSound();
    URB_debugPrint('sound off');
};

jQuery.fn.sendCallBackPause = function(){
    try {
        jQuery().getSwf(ANA.swfId).callBackModule();
    } catch (e) {
        return;
    }
    
    jQuery().changeSoundIconOff();
    URB_debugPrint('pause');
};

jQuery.fn.sendCallBackLogoClick = function(){
    try {
        jQuery().getSwf(ANA.swfId).callBackLogo();
    } catch (e) {
        return;
    }
    
    jQuery().changeSoundIconOff();
    URB_debugPrint('logo click');
};

jQuery.fn.changeSoundIconOn = function(){
    if (jQuery('#soundToggle a:visible img').attr('alt') == 'SOUND ON') {
        URB_debugPrint('sound icon on');
        ANA.effectSound();
    }
};

jQuery.fn.changeSoundIconOff = function(){
    if (jQuery('#soundToggle a:visible img').attr('alt') == 'SOUND OFF') {
        URB_debugPrint('sound icon off');
        ANA.effectSound();
    }
};

jQuery.fn.lockFlag = function(){
    ANA.lock = true;
    ANA.effectLocked();
    setTimeout(function(){
        if (jQuery().getLockFlag()) {
            jQuery().unlockFlag();
        }
    }, ANA.sleep * 3);
    URB_debugPrint('Lock!');
};

jQuery.fn.unlockFlag = function(){
    ANA.effectUnlocked();
    setTimeout(function(){
        if (jQuery().getLockFlag()) {
            ANA.lock = false;
            URB_debugPrint('Unlock!');
        }
    }, ANA.sleep);
};

jQuery.fn.getLockFlag = function(){
    return ANA.lock;
};

jQuery.fn.changeVisual = function(contentId){
    var target = ['Main', 'Amc', 'Search'];
    var areaId = contentId.substring(1, 3);
    URB_debugPrint('change visual: ' + areaId);
    
    for (var i = 0; i < target.length; i++) {
        ANA.effectChangeVisual('#visual' + target[i], areaId);
    }
    smartScroll.start('#pagetop');
};

jQuery.fn.getSwf = function(swfId){
    if (navigator.appName.indexOf("Microsoft") != -1) {
        return window[swfId];
    } else {
        return document[swfId];
    }
};

jQuery(function(){
    if (jQuery().getSwf(ANA.swfId)) {
        jQuery().initAnaWithFlash();
    } else {
        jQuery().initAnaWithoutFlash();
    }
});

var smartScroll = {};
smartScroll.scrolling = false;
smartScroll.start = function (targetId,easing,completeFunc) {
	smartScroll.scrolling = true;
	var element = jQuery(targetId)[0];
	var easing = easing || 0.14;
	var interval = 30;
	var st = jQuery(document).scrollTop();
	var sl = jQuery(document).scrollLeft();
	var ww = jQuery(window).width();
	var wh = jQuery(window).height();
	var dw = jQuery(document).width();
	var dh = jQuery(document).height();
	var tX = 0; var tY = 0;
	var myTimer; var moveX; var moveY;
	while(element){
	   tX += element.offsetLeft;
	   tY += element.offsetTop;
	   element = element.offsetParent;
	}
	if (dw > (tX + ww)) {
		var toX = tX - sl;
	} else {
		var toX = dw - sl - ww;
	}
	if (dh > (tY + wh)) {
		var toY = tY - st;
	} else {
		var toY = dh - st - wh;
	}
	function windowScroll () {	
		if (0 > toX) {
			moveX = Math.floor(toX*easing);
		} else {
			moveX = Math.ceil(toX*easing);
		}		
		if (0 > toY) {
			moveY = Math.floor(toY*easing);
		} else {
			moveY = Math.ceil(toY*easing);
		}		
		sl += moveX;
		st += moveY;
		window.scrollTo(sl, st);
		toX -= moveX;
		toY -= moveY;
		myTimer = setTimeout(windowScroll,interval);
		if (moveX == 0 && moveY == 0) {
			smartScroll.scrolling = false;
			clearTimeout(myTimer);
			if (completeFunc) {
				if (typeof(completeFunc) == 'function') {
					completeFunc();
				} else if (typeof(completeFunc) == 'string') {
					eval(completeFunc+'()');
				}
			}
			
		}
	}
	windowScroll();	
};
