(function($) {
var myAgent = navigator.userAgent;
var whetherIE  = myAgent.indexOf('MSIE');
var whetherIE6 = myAgent.indexOf('MSIE 6');
var asIphone   = myAgent.indexOf('iPhone');
var asIpad     = myAgent.indexOf('iPad');

$.ajax({
	scriptCharset: 'utf-8',
	type: 'GET',
	url: '/photo/s_photolife.xml',
	dataType: 'xml',
	success: function(_xml){
		var _this ='', _id ='', _date = '', _comment = '', _imgURL = '', _linkURL = '';
		$(_xml).find('entry').each(function(i){
			_this    = $(this);
			_id      = _this.find('id').text();
			_date    = _this.find('date').text();
			_comment = _this.find('title').text();
			_imgURL  = _this.find('picture1').text();
			_linkURL = '/system/photolife/detail?id=' + _id;
		});
		$(document).ready(function(){
			var _target = $('section.issue>div.issuePhoto');
			
			var _eachDate = new Date(_date);
			var _setDate  = _eachDate.getFullYear()+'.'+(_eachDate.getMonth()+1)+'.'+_eachDate.getDate();

			var _figure = $('<figure></figure>');
			
			_figure.append($('<a></a>',{'href': '/photo/', 'class':'issuePhotoImg'})
							 .append($('<img >',{'src': '/images/images_overlay_issue_photo.png', 'class':'imageOverlay hide', 'alt':''}))
							 .append($('<img >',{'src': _imgURL, 'alt':''})));
							 
			_figure.append($('<figcaption></figcaption>').text(_comment).prepend($('<time></time>',{'datetime': _date}).text(_setDate)));
			
			
			_target.append(_figure);
			_target.append($('<h3></h3>')
							 .append($('<a></a>',{'href': '/photo/'}).text('BB Photo Life')
									   .prepend($('<img>',{'src':'images/icon_photolife_l.jpg','alt':'BB Photo Life'})
												)
									)
							);
		});
	}//,
	//error: function(XMLHttpRequest, textStatus, errorThrown){
	//	console.log(XMLHttpRequest, textStatus, errorThrown);
	//}
});

$(function(){
	$('ul.worksList>li>a').children('img.worksOverlay').hide();
	$('ul.worksList>li>a').live('hover', function(ev){
		var eventType = ev.type
		var _overlay = $(this).children('img.worksOverlay');
		if(eventType == 'mouseover' || eventType == 'mouseenter'){
			(whetherIE == -1) ? _overlay.stop(false,true).fadeIn(400, function(){_overlay.removeClass('hide');}):_overlay.show().removeClass('hide');
		}else if(eventType == 'mouseout' || eventType == 'mouseleave'){
			(whetherIE == -1) ? _overlay.stop(false,true).fadeOut(400, function(){_overlay.addClass('hide');}):_overlay.hide().addClass('hide');
		}
	});
	
	//Works New Mark Setting------------------------------------------------
	function newMarkClass(_launchdateISO){
		var nowDate      = new Date ();
		var comparesDate = new Date (_launchdateISO);
		
		var passedMilSec =  nowDate.getTime() - comparesDate.getTime();
		var passedDay = Math.floor(passedMilSec/(24*60*60*1000));
		
		return (passedDay <= 30) ? true: false;
	}
	
	$('ul.worksList>li>a>img.iconNew').hide();
	
	$('ul.worksList>li').each(function(i){
		var _date = $(this).attr('data-time');
		if(newMarkClass(_date)){
			
			if(whetherIE == -1){
				$(this).children('a').children('img.iconNew').fadeIn(1200);
			}else{
				$(this).children('a').children('img.iconNew').show();
			}
		}
	});
	//----------------------------------------------------------------------
	
	var overlayEventAttach = 'div.issuePhoto>figure>a.issuePhotoImg, div.issuePhoto>h3>a';
	$(overlayEventAttach).live('hover', function(ev){
		var eventType = ev.type
		var _overlay = $(this).parents('div.issuePhoto').find('img.imageOverlay');
		if(eventType == 'mouseover' || eventType == 'mouseenter'){
			(whetherIE == -1) ? _overlay.stop(false,true).fadeIn(400, function(){_overlay.removeClass('hide');}):_overlay.show().removeClass('hide');
		}else if(eventType == 'mouseout' || eventType == 'mouseleave'){
			(whetherIE == -1) ? _overlay.stop(false,true).fadeOut(400, function(){_overlay.addClass('hide');}):_overlay.hide().addClass('hide');
		}
	});
});
})(jQuery);
