﻿/**
* Copyright (c) 2010 BEIJING QIANYITONG ADVERTISING DESIGN CO.,LTD ( http://www.qytdesign.com/)
 * All rights reserved.
 * Open source under the BSD License. 
*/
var clickid = -1;//已点击id
var numberLink = -1; //左右箭头内图片链接数量
var countLink = -1; //图片链接总数量
var linkWidth = 0; //小图片的宽度
var navImgWidth = 0; //图片导航总宽度
var imgHoverWidth = 0;//hove图片宽度
$(document).ready(function() {
    //$('.imgshow img').eq(0).show();

    $('.prev').click(function() { prev(); });
    $('.next').click(function() { next(); });

    linkWidth = parseInt($('.link').css('width')) + parseInt($('.link').css('margin-right')) + parseInt($('.link').css('margin-left')); //小图片的宽度
    navImgWidth = $('.navImg').css('width');
    imgHoverWidth = $('.imgHover').css('width');
    numberLink = Math.floor(parseInt(navImgWidth) / linkWidth);

    //$('.link div').hover(function() { this.addClass('removeAlpha'); }, function() { this.removeClass('removeAlpha'); });
    $('.link div').addClass('addAlpha');

    countLink = $('.navImg .link').length;
    $('.navImg').scrollLeft(0);
    showPhoto(0);


    $('.play').hover(function() { $(this).addClass('playhover') }, function() { $(this).removeClass('playhover') });
    //    $('.play').click(function() {
    //        $('.imgshow img').eq(clickid).trigger('click');
    //    });
    $('.play').colorbox();
    $(".imgshow a").colorbox();
    //$("a[rel='media']").colorbox();
});
function showPhoto(id) {
    if (id == clickid)
        return; 
    animateClick(id);
    photoOut();
    photoIn(id);

    //变换导航图片透明度
    $('.link div').addClass('addAlpha');
    clickid = id;
    $('#inner .link').eq(clickid).children().toggleClass('addAlpha');
    
    //加入play按钮链接
    var CurrentLink=$(".imgshow a").eq(clickid);
    var href = CurrentLink.attr('href');
    $('.play').attr('href', href);
    
    //是否显示new
    if ($(".imgshow img").eq(id).hasClass('photo_New')) {
        $('.new_photo').fadeIn(200);
    }
    else
        $('.new_photo').fadeOut(200);

    //文字
    $('#round').text(CurrentLink.attr('title'));
}
function photoOut() {
    $('.imgshow img').eq(clickid).fadeOut(200);
}
function photoIn(id) {
    $('.imgshow img').eq(id).fadeIn(200);
    //alert(id);
}
function animateClick(id) {

    var page = Math.floor(id / numberLink);
    var currentPage = Math.floor(clickid / numberLink);

    var moveId = 0;
    if (page == currentPage) { moveId = id % numberLink; }

    var margin = parseInt(linkWidth * moveId) + parseInt(imgHoverWidth) + 'px';
    //alert(margin);
    $('.imgHover').animate({ "marginLeft": margin });
}
function prev() {
    var page = Math.floor(clickid / numberLink);
    if (page <= 0)
        return;
    page--;
    var id = page * numberLink;
    showPhoto(id);
    
    $('.navImg').animate({ scrollLeft: parseInt(navImgWidth) * page }, "slow");
}
function next() {
    var page = Math.floor(clickid / numberLink);
    var currentPage = Math.floor((countLink-1) / numberLink);

    if (page >= currentPage)
        return;
    page++;
    var id = page * numberLink;
    //alert(id);
    showPhoto(id);
    $('.navImg').animate({ scrollLeft: parseInt(navImgWidth) * page }, "slow");
}
