//ドメイン取得
var varDomain=location.hostname;
//文字サイズ変更
var fontLFunction=function(){
	$$('body').setStyle('font-size','75%');
	var myCookie=Cookie.write('fontsize','Large',{domain:varDomain,path:'/'});
}
var fontMFunction=function(){
	if(Browser.Engine.trident  && Browser.Engine.version <5){
		$$('body').setStyle('font-size','62.6%');
	} else {
		$$('body').setStyle('font-size','62.5%');
	}
	var myCookie=Cookie.write('fontsize','Middle',{domain:varDomain,path:'/'});
}
var fontSFunction=function(){
	$$('body').setStyle('font-size','50%');
	var myCookie=Cookie.write('fontsize','Small',{domain:varDomain,path:'/'});
}
var fontChgFunction=function(){
	if($('fontLarge')){$('fontLarge').addEvent('click',fontLFunction);}
	if($('fontMiddle')){$('fontMiddle').addEvent('click',fontMFunction);}
	if($('fontSmall')){$('fontSmall').addEvent('click',fontSFunction);}
}
//初期フォントサイズ設定
var iniDefFontFunction=function(){
	if($('fontNav')){
		if(Cookie.read("fontsize")){
			var myCookie=Cookie.read("fontsize");
			if(myCookie=='Small'){fontSFunction();
			}else if(myCookie=='Large'){fontLFunction();
			}else{fontMFunction();}
		}else{fontMFunction();}
	}
}
//テーブルの偶数行を色づけ
var initTableStripe=function(){
	var tbl;
	if(tbl=$$('table.stripe')){
		tbl.each(function(e,i){
			tar=e.getElements('tr:odd');
			tar.setStyle('background-color','#F5F5F0');
		});
	}
}
//InputフィールドのHover時にスタイル付与 Bug Fix IE6
initInputColor=function(){
	var tar;
	if(tar=$$('input.input_text')){
		tar.each(function(e,i){
			e.addEvent('focus',function(){e.addClass('focus');});
			e.addEvent('blur',function(){e.removeClass('focus');});
		});
	}
	if(tar=$$('input.input_password')){
		tar.each(function(e,i){
			e.addEvent('focus',function(){e.addClass('focus');});
			e.addEvent('blur',function(){e.removeClass('focus');});
		});
	}
	if(tar=$$('textarea.input_textarea')){
		tar.each(function(e,i){
			e.addEvent('focus',function(){e.addClass('focus');});
			e.addEvent('blur',function(){e.removeClass('focus');});
		});
	}
}
//入力エラー時にエラーカラー付与
initErrorColor=function(){
	var error;
	if(error=$$('.err_msg')){
		error.each(function(e,i){
			var prevTar;
			var res=$try(prevTar=e.getPrevious());
			if(prevTar&&prevTar.match('input') || prevTar&&prevTar.match('textarea')){
				if(prevTar.match('input.tel')){
					var parTar=e.getParent().getChildren('input');
					parTar.setStyles({
						'border-color': '#f36',
						'background-color': '#FFEBF0'
					});
				}else{
					prevTar.setStyles({
						'border-color': '#f36',
						'background-color': '#FFEBF0'
					});
				}
			}else{
				var prev2Tar;
				if(prevTar){$try(prev2Tar=e.getPrevious().getPrevious());}
				if(prev2Tar&&prev2Tar.match('input') || prev2Tar&&prev2Tar.match('textarea')){
					if(prev2Tar.match('input.tel')){
						var parTar=e.getParent().getChildren('input');
						parTar.setStyles({
							'border-color': '#f36',
							'background-color': '#FFEBF0'
						});
					}else{
						prev2Tar.setStyles({
							'border-color': '#f36',
							'background-color': '#FFEBF0'
						});
					}
				}else{
					var parTar;
					$try(parTar=e.getParent().getChildren('input'));
					parTar.setStyles({
						'border-color': '#f36',
						'background-color': '#FFEBF0'
					});
				}
			}
		});
	}
}
//Roll Over
var btnRollFunction = function () {
	var thisType="";
	var thisImg = this.src;
	if(thisImg.contains("http://")){thisType="http://";}
	else if(thisImg.contains("file://")){thisType="file://";}
	thisImg = thisImg.replace(thisType,"");
	var thisPath = new Array();
	thisPath = thisImg.split("/");
	var thisFile = thisPath.pop();
	var tmpImg = thisFile.split(".");
	var baseName = tmpImg[0];
	if (!baseName.match("_on")) {
		baseName += '_on';
		var hoverName = baseName + '.' + tmpImg[1];
		var path = thisType+thisPath.join("/") + '/' + hoverName;
		this.src = path;
	}
}
var btnOutFunction = function () {
	var thisType="";
	var thisImg = this.src;
	var now = this.getParent().getParent().get('class');
	if(thisImg.contains("http://")){thisType="http://";}
	else if(thisImg.contains("file://")){thisType="file://";}
	thisImg = thisImg.replace(thisType,"");
	var thisPath = new Array();
	thisPath = thisImg.split("/");
	var thisFile = thisPath.pop();
	var tmpImg = thisFile.split(".");
	var baseName = tmpImg[0];
	if (!now.contains("current")) {
		baseName = baseName.replace("_on","");
		var hoverName = baseName + '.' + tmpImg[1];
		var path = thisType+thisPath.join("/") + '/' + hoverName;
		this.src = path;
	}
}
var btnHighlight = function (e) {
	var thisType="";
	var thisImg=e.get('src');
	if(thisImg.contains("http://")){thisType="http://";}
	else if(thisImg.contains("file://")){thisType="file://";}
	thisImg = thisImg.replace(thisType,"");
	var thisPath = new Array();
	thisPath = thisImg.split("/");
	var thisFile = thisPath.pop();
	var tmpImg = thisFile.split(".");
	var baseName = tmpImg[0];
	if (!baseName.match("_on")) {
		baseName += '_on';
		var hoverName = baseName + '.' + tmpImg[1];
		var path = thisType+thisPath.join("/") + '/' + hoverName;
		e.set('src', path);
		
	}
}
var fixRollOver = function () {
	$$('.btn').each(function(element) {
		var now = element.getParent().getParent().get('class');
		if (now.contains('current')){
			btnHighlight(element);
		} else {
			$$('.btn').addEvent('mouseover', btnRollFunction);
			$$('.btn').addEvent('mouseleave', btnOutFunction);
		}
	});
}

//getパラメータ受信
var retrieveGETqs = new Class({
	qsParm: new Array(),
	query: '',
	parms: '',
	initialize : function () {
		this.query = window.location.search.substring(1);
		this.parms = this.query.split('&');
		for (var i=0; i<this.parms.length; i++) {
			var pos = this.parms[i].indexOf('=');
			if (pos > 0) {
				var key = this.parms[i].substring(0,pos);
				var val = this.parms[i].substring(pos+1);
				this.qsParm[key] = val;
				this.getAd();
			}
		}
	},
	getAd : function() {
		return this.qsParm;
	}
});
//リンク置換え
var fixLink = new Class({
	q: '',
	tar: '',
	url: '',
	first: '',
	alt: '',
	target: '',
	rel: '',
	initialize : function (q) {
		this.q=q;
		if(!this.q.ad) { this.q.ad=''; }
		if(!this.q.ref) { this.q.ref=''; }
		this.tar=$(document.body).getElements('a');
		this.linkEvent();
	},
	linkEvent : function () {
		var self = this;
		self.tar.each(function(anc){
		    self.getUrl(anc);
		    self.getRel(anc);
		    if(self.url && !self.url.match(/\#[\w\-\_]+$/) && !self.url.match(/\#$/) && !self.url.match(/javascript/)){
		    	if(self.rel && self.rel.match(/external/)){
		    		//
		    		// Bug.1
		    		// Externalイベントが二重に起こる
		    		//
		    		self.getFirst(anc);

		    		//もし1つ目の子要素がimgなら
					if(self.first.length){
						self.getAlt();
						if(self.alt=='資料請求はこちら' || self.alt=='資料請求' || self.alt == 'ご利用申込みはこちら' || self.alt == 'ご利用申込み' || self.alt == 'コープデリ利用（加入）申込み'){
							anc.addEvent('click', function(e) {
								e.stop();
								self.getUrl(this);
								self.getTarget(anc);
// 10-09-10 Watabe
								self.q.ref = 3;
//
								var link = self.getParam(e);
								window.open(link);

							},this);
						}else{

							if (anc.getProperty('rel')) {
								if (anc.getProperty('rel').contains(',')) {
									var tempArr = anc.get('rel').split(',');
									tempArr.each(function (temp,i) {
										temp = temp.replace(' ', '');
										if(temp.contains(':')){
											var ta = temp.split(':');
											contentOptions[ta[0]] = ta[1];
										} else {
											if(temp == 'external') {
												contentOptions['linkType'] = 'external';
											}
										}
									},this);
									if (contentOptions['linkType'] == 'external') {
										if (contentOptions['width'] && contentOptions['height']) {
											if (contentOptions['width'] == 'max') {
												var winWidth = $(window).getSize();
												contentOptions['width'] = winWidth.x;
											}
											if (contentOptions['height'] == 'max') {
												var winWidth = screen.width;
												contentOptions['height'] = screen.height;
											}
											var option = 'scrollbars=yes, width='+contentOptions['width']+', height='+contentOptions['height'];
											anc.addEvent('click', function(e) { 
												e = new Event(e);
												e.stop();
												window.open(anc.getProperty('href'), 'newwin', option);
											});
										}
									}
								}
								else if (anc.getProperty('rel') == 'external') {
									anc.addEvent('click', function(e) { 
										e = new Event(e);
										e.stop();
										window.open(this.getProperty('href')); 
									}.bind(anc)); 
								}
							}
						}
					}
		    	} else {
					anc.addEvent('click', function(e) {
						e.stop();
						self.getUrl(this);
						self.getTarget(anc);
						var link = self.getParam(e);
						if(self.target=="_blank"){ window.open(link); }
						else{ location.href = link; }
					},this);
        		}
        	}
		});
	},
	getParam : function (e) {
		return this.url+'?ad='+this.q.ad+'\&ref='+this.q.ref;
	},
	getUrl : function(a) {
        this.url = a.get("href");
	},
	getFirst : function(a) {
        this.first = a.getElements('img');
	},
	getAlt : function() {
        this.alt = this.first.get('alt');
	},
    getTarget : function(a) {
        this.target = a.get("target");
    },
    getRel : function(a) {
        this.rel = a.get("rel");
    }
});

//External Link
var fixExternalLinks = function () {
    var contentOptions = {};
    $$('a').each(function(element) {
        if (element.getProperty('rel')) {
            if (element.getProperty('rel').contains(',')) {
                var tempArr = element.get('rel').split(',');
                tempArr.each(function (temp,i) {
                    temp = temp.replace(' ', '');
                    if(temp.contains(':')){
                        var ta = temp.split(':');
                        contentOptions[ta[0]] = ta[1];
                    } else {
                        if(temp == 'external') {
                            contentOptions['linkType'] = 'external';
                        }
                    }
                },this);
                if (contentOptions['linkType'] == 'external') {
                    if (contentOptions['width'] && contentOptions['height']) {
                        if (contentOptions['width'] == 'max') {
                            var winWidth = $(window).getSize();
                            contentOptions['width'] = winWidth.x;
                        }
                        if (contentOptions['height'] == 'max') {
                            var winWidth = screen.width;
                            contentOptions['height'] = screen.height;
                        }
                        var option = 'scrollbars=yes, width='+contentOptions['width']+', height='+contentOptions['height'];
                        element.addEvent('click', function(e) { 
                            e = new Event(e);
                            e.stop();
                            window.open(element.getProperty('href'), 'newwin', option);
                        });
                    }
                }
            }
            else if (element.getProperty('rel') == 'external') {
                element.addEvent('click', function(e) { 
                    e = new Event(e);
                    e.stop();
                    window.open(this.getProperty('href')); 
                }.bind(element)); 
            }
        }
    });
    var contentOptions = {};
    $$('area').each(function(element) {
        if (element.getProperty('rel')) {
            if (element.getProperty('rel').contains(',')) {
                var tempArr = element.get('rel').split(',');
                tempArr.each(function (temp,i) {
                    temp = temp.replace(' ', '');
                    if(temp.contains(':')){
                        var ta = temp.split(':');
                        contentOptions[ta[0]] = ta[1];
                    } else {
                        if(temp == 'external') {
                            contentOptions['linkType'] = 'external';
                        }
                    }
                },this);
                if (contentOptions['linkType'] == 'external') {
                    if (contentOptions['width'] && contentOptions['height']) {
                        var option = 'scrollbars=yes, width='+contentOptions['width']+', height='+contentOptions['height'];
                        element.addEvent('click', function(e) { 
                            e = new Event(e);
                            e.stop();
                            window.open(element.getProperty('href'), 'newwin', option);
                        });
                    }
                }
            }
            else if (element.getProperty('rel') == 'external') {
                element.addEvent('click', function(e) { 
                    e = new Event(e);
                    e.stop();
                    window.open(this.getProperty('href')); 
                }.bind(element)); 
            }
        }
    });
}
//Window Close Button
var closeBtnFunction = function () {
	if ($$('.nav_close')) {
		$$('.nav_close').each(function(e) {
			e.getElement('a').addEvent('click', function(ev){ ev.stop(); window.close(); });
		});
	}
}

window.addEvent('domready',function(){
	//Get Query
	var param = new retrieveGETqs();
	//Default Font Size
	iniDefFontFunction();
    //Edit Link
    var link_change = new fixLink(param.qsParm);
	//Font Size Change
	fontChgFunction();
    //External Link
//    fixExternalLinks();
	//Button Roll Over
	fixRollOver();
	//Close Button
	closeBtnFunction();
	//Table Stripe
	initTableStripe();
	//Input Hover Color
	initInputColor();
	//Input Error Color
	initErrorColor();
});
