/*
    Copyright (C) 2009 ColorCyberWay
    All rights reserved
    script for all pages
*/

( function() {
    
$( document ).ready( function() {
    
    if ( $.browser.msie ) {
        $( "body" ).addClass( "ie" );
    }
    
    // $.ifixpng( baseUrl + "assets/image/s.gif" );
    // $( ".pngfix" ).ifixpng();
    
    initQuotation();
    
});


var quotationTimer = null;
var quotationDelay = 10000;
var quotationData = null;
var quotationIndex = 0;
var striking = false;
var strikeTimer = null;
var strikeCount = 0;
var framenum = 7;
var quotationImg = "quotation-head.jpg";
var cursorImg = "s.cur";

function initQuotation() {
    $( ".quotation-content" ).bind( "selectstart", function( e ) {
        e.preventDefault();
        return false;
    });
    
    var headImg = new Image();
    headImg.src = baseUrl + "assets/image/" + quotationImg;
    
    if ( !headImg.complete ) {
        $( headImg ).bind( "error load onreadystatechange", onImgLoaded );
    } else {
        onImgLoaded();
    }
    
    function onImgLoaded() {
        $( this ).unbind();
        
        $( ".quotation-icon" ).frameplayer({
            img: baseUrl + "assets/image/" + quotationImg,
            width: 45,
            height: 50,
            framerate: 15,
            framenum: 7,
            autoPlay: false
        }).bind( "step", function() {
            if ( $.browser.msie ) {
                $( this ).css( "cursor", "url(" + baseUrl + "assets/image/" + cursorImg + "), crosshair");
            }
        });
        
        $( ".quotation-icon" ).bind( "mouseover mousemove", function( e ) {
            $( ".quotation-cursor" ).css({
                "display": "block",
                "top": ( e.pageY + 5 ) + "px",
                "left": ( e.pageX - 20 ) + "px"
            });
            
            if ( $.browser.msie ) {
                $( this ).css( "cursor", "url(" + baseUrl + "assets/image/" + cursorImg + "), crosshair");
            }
        }).mouseout( function( e ) {
            $( ".quotation-cursor" ).css({
                "display": "none"
            });
        }).mousedown( function( e ) {
            $( ".quotation-cursor" ).css({
                "background-position": "left bottom"
            });
            
            strikeHead( striking );
        }).mouseup( function( e ) {
            $( ".quotation-cursor" ).css({
                "background-position": "right top"
            });
        });
        
        getQuotation();
    }
}

function getQuotation() {
    window.callback = function( json ) {
        quotationData = json;
        showQuotation();
    };
    
    $.ajax({
        url: "http://mycolorway.com/index.php/twitter/jsonp/callback",
        dataType: "script",
        cache: true
    });
}

function showQuotation() {
    var quotation = quotationData[quotationIndex];
    var icon = $( ".quotation-icon" );
    icon.frameplayer( "options", {
        vPosition: ( 1 - quotation.id ) * icon.data( "settings" ).height + "px",
        framenum: framenum,
        currentFrame: 0,
        reverse: false
    }).frameplayer( "play" );
    
    icon.one( "complete", false, function() {
        $( ".quotation-content" )
            .find( "span:first" )
            .html( quotation.name + "：" + quotation.content )
            .end()
            .find( ".date" )
            .html( quotation.date )
            .end()
            .animate({
                top: 0,
                opacity: "show"
            }, {
                duration: 400,
                complete: function() {
                    quotationTimer = setTimeout( changeQuotation, quotationDelay );
                }
            });
    });
}

function strikeHead() {
    var icon = $( ".quotation-icon" );
    if ( icon.data( "playing" )) {
        return;
    }
    
    clearTimeout( quotationTimer );
    clearTimeout( strikeTimer );
    
    strikeCount++;
    
    if ( !striking ) {
        icon.one( "complete", function() {
            strikeTimer = setTimeout( function() {
                changeQuotation();
                striking = false;
            }, 500 );
        });
        
        icon.frameplayer( "options", {
            framenum: framenum + 1,
            reverse: false
        }).frameplayer( "play" );
        striking = true;
    } else {
        strikeTimer = setTimeout( function() {
            changeQuotation();
            striking = false;
        }, 500 );
    }
}

function changeQuotation() {
    
    var icon = $( ".quotation-icon" );
    if ( icon.data( "playing" )) {
        return;
    } 
    
    clearTimeout( quotationTimer );
    
    if ( strikeCount > 0 ) {
        var id = quotationData[quotationIndex].id;
        $.ajax({
            url: "http://mycolorway.com/index.php/twitter/strikeStats/" + id + "/" + strikeCount,
            dataType: "script",
            cache: true
        });
        strikeCount = 0;
    }
    
    quotationIndex++;
    if ( quotationIndex >= quotationData.length ) {
        quotationIndex = 0;
    }
    
    icon.one( "complete", function() {
        setTimeout( showQuotation, 200 );
    });
    
    icon.frameplayer( "options", {
        reverse: true
    }).frameplayer( "play" );
    
    $( ".quotation-content" ).animate({
        top: 10,
        opacity: "hide"
    }, {
        duration: 400
    });
}


    
})( jQuery );